马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
上面的代码大家可以看看,编译一下,就知道了。safe_stack不安全,哈哈。求解决办法。
StackOverflow和Yahoo Ask上面的答案已经把我搞蒙了。书上的解释也好乱。求大神。
下面是书上的解释:
We have an array of a derived class called safe stack. In C++, you can use a base class pointer (stack*) to point to a derived class (safe_stack). The system will see only the base part of the object, but you can still point to it.
Now a pointer can point to a single instance of a class or an array of objects.
So we have the following two rules: From this, we can conclude: That's wrong. The problem is that an array of derived objects is not the same as an array of base objects.
So if we take a base pointer and point it a derived array, the memory layout will be wrong. 好啦,请注意!!! 我需要一个解决方案。建议使用dynamic_cast或者reinterpret_cast,不可以告诉我使用STL。 如果是要使用auto_ptr或者share_ptr一类的能解决也行。多谢多谢。
|