相关疑难解决方法(0)

为什么C++需要'对象切片'?为什么允许这样做?更多错误?

为什么C++标准允许对象切片?

请不要向我解释c ++对象切片概念,因为我知道这一点.

我只是想知道这个c ++功能(对象切片)设计背后的意图是什么?

为了获得新手更多的错误?

对于c ++来说,防止对象切片不是更安全吗?

以下是标准和基本切片示例:

class Base{
public:
       virtual void message()
       {
               MSG("Base ");
       }
private:
    int m_base;
};

class Derived : public Base{
public:
       void message()
       {
               MSG("Derived "); 
       }
private:
       int m_derive;
};

int main (void)
{
    Derived dObj;

    //dObj get the WELL KNOWN c++ slicing below
    //evilDerivedOjb is just a Base object that cannot access m_derive
    Base evilDerivedOjb = dObj;  //evilDerivedObj is type Base
    evilDerivedOjb.message();    //print "Baes" here of course just as c++ …
Run Code Online (Sandbox Code Playgroud)

c++ standards slice

11
推荐指数
3
解决办法
2213
查看次数

标签 统计

c++ ×1

slice ×1

standards ×1