c++ 中的对象切片

发布于 2024-10-13 11:36:32 字数 1307 浏览 3 评论 0原文

    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) 访问,但是从 类的虚拟显示方法 我可以即使它不在显示方法的本地范围内也可以访问。

现在我的问题是:

  1. 为什么我只能使用虚函数访问 iDerived 变量,这样正确吗?
  2. 如何避免对象切片。
    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:

  1. Why I am able to access the iDerived variable only with virtual function, is this proper ?
  2. How to avoid object slicing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

决绝 2024-10-20 11:36:32

您的示例代码根本不涉及切片。您所做的只是调用基本的多态性。通过将 Base::display() 声明为 virtual 并在 Base * 上调用 display(),您要求它动态调用所指向对象的实际类型的成员函数,即DerivedDerived 的成员变量在 Derived::display() 的范围内,因此这就是它编译和工作的原因。

但是,您只能通过指向Base的指针直接访问在Base中声明的成员变量(或函数)。这就是 varBase->iDerived 无法编译的原因。

切片通常涉及相当于:

Derived d;
Base b = (Base)d;

通过显式分配/初始化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() as virtual and by calling display() on a Base *, you have asked it to dynamically call the member function in the actual type of the object being pointed to, which is Derived. The member variables of Derived are within the scope of Derived::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 why varBase->iDerived does not compile.

Slicing normally involves something equivalent to:

Derived d;
Base b = (Base)d;

By explicitly assigning/initializing a Base object, all the Derived-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.

遗失的美好 2024-10-20 11:36:32

C++有虚函数,但没有虚数据。

您可以添加以下内容来模拟它:

class Base {
  // What you had before
  virtual int getAnInt() const = 0; // =0 means that Derived must implement this
};
class Derived {
  // What you had before
  virtual int getAnInt() const { return iDerived; }
};

对象切片完全不相关,并且在您的示例中不会发生。

C++ has virtual functions, but no virtual data.

You could add the following to simulate it:

class Base {
  // What you had before
  virtual int getAnInt() const = 0; // =0 means that Derived must implement this
};
class Derived {
  // What you had before
  virtual int getAnInt() const { return iDerived; }
};

Object slicing is entirely unrelated, and doesn't happen in your example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文