简体   繁体   English

C++ 派生 class 和基本复制构造函数作为指针

[英]C++ derived class and base copy constructor as pointer

I have a base class aaa and two derived classes bbb and ccc .我有一个基础 class aaa和两个派生类bbbccc

Is it possible to declare the base class first and then reference it to its derived classes, to make them share the same reference of it?是否可以先声明基础 class 然后将其引用到其派生类,以使它们共享相同的引用?

To show what I mean I wrote this small code:为了说明我的意思,我写了这个小代码:

#include <iostream>
#include <functional>
#include <memory>

class aaa
{
    protected:

        int a;

    public:

        aaa(int _a)
        {
            a = _a;
        }

        aaa(aaa &_aaa) = default;
        aaa(aaa &&_aaa) = default;

        int get_a(void)
        {
            return a;
        }

        void set_a(int _a)
        {
            a = _a;
        }

        int *get_a_addr(void)
        {
            return &a;
        }
};

class bbb : public aaa
{   
    public:

        bbb(aaa &_aaa) : aaa(_aaa)
        {
            ;
        }

        bbb(aaa &&_aaa) : aaa(_aaa)
        {
            ;
        }
};

class ccc : public aaa
{
    public:

        ccc(aaa &_aaa) : aaa(_aaa)
        {
            ;
        }

        ccc(aaa &&_aaa) : aaa(_aaa)
        {
            ;
        }
};

int main(void)
{   
    aaa a(10);
    bbb b(std::ref(a));
    ccc c(std::ref(a));

    std::cout << "address of a" << std::endl;
    std::cout << a.get_a_addr() << std::endl;
    std::cout << b.get_a_addr() << std::endl;
    std::cout << c.get_a_addr() << std::endl;

    return 0;
}

But b and c clearly don't have access to the same memory area of a::a .但是bc显然无法访问a::a的相同 memory 区域。

As far as I know the only way to accomplish this is using nested classes.据我所知,实现这一点的唯一方法是使用嵌套类。 Isn't possible the way I am trying to?不可能像我尝试的那样吗?

All three objects have a different instance of the a member.所有三个对象都有不同的 a 成员实例。 And since you return the address of the ˋaˋ member in ˋget_a_addressˋ and not its content, the return value differs for each object.而且由于您在ˋget_a_addressˋ中返回ˋaˋ成员的地址而不是其内容,因此每个object的返回值不同。 I assume you want to return the same value (the content of the ˋaˋ object) for all these calls.我假设您希望为所有这些调用返回相同的值(ˋaˋ 对象的内容)。

No, each base class subobject is independent of all other subobjects.不,每个基础 class 子对象都独立于所有其他子对象。 What you can do is have a fake base class, whose only job is to reference the real thing.你能做的是有一个假基地 class,它唯一的工作就是参考真实的东西。

class realA
{
  void foo() { ... }
  void bar() { ... }
};

class a
{
   ...
   void foo() { impl->foo(); }
   void bar() { impl->bar(); }
   std::shared_ptr<realA> impl;
};

a behaves just like realA because it has all the same methods as realA , doing the same thing. a行为与realA一样,因为它具有与realA相同的方法,做同样的事情。

Your b and c still have separate a subobjects, but now they may share the same realA object, which is the class that does the real work.您的bc仍然有单独a子对象,但现在它们可能共享相同的realA object,即真正起作用的 class。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 派生类的副本构造函数(C ++)的初始化列表上的基类 - Base class on the initialisation list of a derived class' copy constructor (C++) 在 C++ 中复制只有基类指针的派生类 - Copy a derived class with only a base class pointer in C++ 从基本指针复制派生类的构造函数 - Copy constructor for derived class from base pointer C++ Unable to assign base class “this” pointer the derived class object inside base class constructor - C++ Unable to assign base class “this” pointer the derived class object inside base class constructor 指向C ++中派生类的基类指针 - the base class pointer to the derived class in c++ 派生类的 C++ 复制构造函数 - C++ Copy Constructor for a Derived Class C ++通过抽象基类在未知派生类上调用复制构造函数 - C++ Calling a copy constructor on an unknown derived class through an abstract base class 为什么在调用基类复制构造函数(C ++)时将派生类类型作为参数传递? - Why passing derived class type as parameter when calling base class copy constructor (C++)? 在没有动态内存分配的情况下,C ++中是否有一种机制可以从基类指针生成派生类的完整副本? - Is there a mechanism in C++ to make a full copy of a derived class from a base class pointer without dynamic memory allocation? 基类和派生类中的复制构造函数 - copy constructor in base and derived class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM