c++ 中的对象切片
class Base
{
int iBase;
public:
virtual void display()
{
cout<<"I am a Base Class"<<endl;
}
};
class Derived : public Base
{
int iDerived;
public:
Derived()
{
cout<<"In Derived Default Constructor"<<endl;
iDerived=10;
}
void display()
{
cout<<"I am in Derived Class"<<endl;
cout<<"value of iDerived :"<<iDerived<<endl;
iDerived=100;
cout<<"value of iDerived :"<<iDerived<<endl;
}
};
在主干中:
Base *varBase;
Derived varDerived;
varBase = &varDerived;
varBase->display();
varBase->iDerived=10; // Error: iDerived is not a member of Base: ?????
大家好,
我正在尝试理解对象切片并尝试一些 示例程序。
我在某处读到过指针引用 Objcet Slicing 不会 发生。
但在下面的示例中,我注意到 iDerived
无法从 Base 指针(varBase)
访问,但是从 类的虚拟显示方法
我可以即使它不在显示方法的本地范围内也可以访问。
现在我的问题是:
- 为什么我只能使用虚函数访问 iDerived 变量,这样正确吗?
- 如何避免对象切片。
class Base
{
int iBase;
public:
virtual void display()
{
cout<<"I am a Base Class"<<endl;
}
};
class Derived : public Base
{
int iDerived;
public:
Derived()
{
cout<<"In Derived Default Constructor"<<endl;
iDerived=10;
}
void display()
{
cout<<"I am in Derived Class"<<endl;
cout<<"value of iDerived :"<<iDerived<<endl;
iDerived=100;
cout<<"value of iDerived :"<<iDerived<<endl;
}
};
In MAIN:
Base *varBase;
Derived varDerived;
varBase = &varDerived;
varBase->display();
varBase->iDerived=10; // Error: iDerived is not a member of Base: ?????
Hi all,
I am trying to understand the Object Slicing and trying with some
sample programs.
I read somewhere with the pointer reference Objcet Slicing will not
happen.
But with below example I am noticing that iDerived
is not accessible from Base pointer(varBase)
, But from the virtual display method of class
I can access even though it's not in the local scope of display method.
Now my question is:
- Why I am able to access the iDerived variable only with virtual function, is this proper ?
- How to avoid object slicing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的示例代码根本不涉及切片。您所做的只是调用基本的多态性。通过将
Base::display()
声明为virtual
并在Base *
上调用display()
,您要求它动态调用所指向对象的实际类型的成员函数,即Derived
。Derived
的成员变量在Derived::display()
的范围内,因此这就是它编译和工作的原因。但是,您只能通过指向
Base
的指针直接访问在Base中声明的成员变量(或函数)。这就是varBase->iDerived
无法编译的原因。切片通常涉及相当于:
通过显式分配/初始化
Base
对象,所有Derived
特定的成员都将丢失(即它们已被“切片”掉) 。这个东西是比较基础的;我建议挑选一本关于 C++ 的不错的书。这里有一个不错的列表:权威的 C++ 书籍指南和列表。
Your example code doesn't involve slicing at all. All you have done is invoke basic polymorphism. By declaring
Base::display()
asvirtual
and by callingdisplay()
on aBase *
, you have asked it to dynamically call the member function in the actual type of the object being pointed to, which isDerived
. The member variables ofDerived
are within the scope ofDerived::display()
, so that is why it compiles and works.However, you can only directly access member variables (or functions) declared in Base via a pointer-to-
Base
. That is whyvarBase->iDerived
does not compile.Slicing normally involves something equivalent to:
By explicitly assigning/initializing a
Base
object, all theDerived
-specific members will have been lost (i.e. they have been "sliced" away).This stuff is relatively fundamental; I would suggest picking up a decent book on C++. There is a list of good ones here: The Definitive C++ Book Guide and List.
C++有虚函数,但没有虚数据。
您可以添加以下内容来模拟它:
对象切片完全不相关,并且在您的示例中不会发生。
C++ has virtual functions, but no virtual data.
You could add the following to simulate it:
Object slicing is entirely unrelated, and doesn't happen in your example.