简体   繁体   English

c++ inheritance指针

[英]c++ inheritance pointer

it's pretty diffecult for me to describe my problem.我很难描述我的问题。 I have two classes, I would say Base_A and Derived_A .我有两个课程,我会说Base_ADerived_A You can see from the names, the class Derived_A is derived from Base_A .从名称可以看出,class Derived_A是从Base_A派生的。 Also in my program I have other two classes Base_B and Derived_B (also with inheritance).同样在我的程序中,我还有另外两个类Base_BDerived_B (也具有继承性)。 The class Base_A contains the object of Base_B , and the class Derived_A contains the object of Derived_B . class Base_A包含 Base_B 的Base_B ,class Derived_A包含 Derived_A 的Derived_B .

class Base_A {
public:
    Base_A() {}
    virtual ~Base_A() {}

    Base_B b_;
    Base_B* pointer_;

    void init() {
        b_ = Base_B();
        pointer_ = &b_;
        pointer_->setValue(1);
    }

    void print() {
        pointer_->getValue();

    }
};

class Derived_A: public Base_A {
public:
    Derived_A() {}
    virtual ~Derived_A() {}

    Derived_B b_;
    Derived_B* pointer_;

    void init() {
        b_ = Derived_B();
        pointer_ = &b_;
        pointer_->setValue(2);
        pointer_->increaseValue();
    }
};

class Base_B {
public:
    Base_B() {}
    virtual ~Base_B() {}

    int value_;

    void setValue(int value) {
        value_ = value;
    }

    void getValue() {
        cout << "Base_B: " << value_ << endl;
    }
};

class Derived_B: public Base_B {
public:
    Derived_B() {}
    virtual ~Derived_B() {}

    void increaseValue() {
        value_++;
    }
};

int main() {  
    Derived_A derived_A = Derived_A();
    derived_A.init();
    derived_A.print();

    return 0;
}

How you can see every class of A has one object of class B and pointer to this object.如何查看 A 的每个 class 有一个 object B 和 class B 的 ZA8CFDE6331BD59EB2666F8911C4 指针。 My problem is, when I call the function print() , it does not take Derived_B* pointer_ , but try to access Base_B* pointer_ , which is not exist.我的问题是,当我调用 function print()时,它不需要Derived_B* pointer_ ,而是尝试访问不存在的Base_B* pointer_ How I can say in my program, that it should take the pointer according to the class?我怎么能在我的程序中说它应该根据 class 获取指针? Or do I need to declarate the Base_B* pointer_ inside the Derived_A class like:或者我是否需要在Derived_A class 中声明Base_B* pointer_指针_,例如:

Base::pointer_ = pointer_;

Maybe is there other method or algorithm for my problem?也许我的问题还有其他方法或算法吗?

Thank you a lot.十分感谢。

"but try to access Base_B* pointer_, which is not exist" “但尝试访问不存在的Base_B*

If DerivedA does not properly initialise BaseA , then DerivedA does not meet the "isA" rule for inheritance and the design needs changed.如果DerivedA未正确初始化BaseA ,则DerivedA不符合 inheritance 的“isA”规则,并且设计需求发生了变化。 On the face of things:表面上看:

  1. Don't re-use names in the derived class such as b_ , pointer_ .不要在派生的 class 中重复使用名称,例如b_pointer_ Its just confusing and you gain no value.它只是令人困惑,您没有任何价值。
  2. Make init() virtual.使 init() 成为虚拟的。
  3. Have DerivedA::init() call BaseA::init() explicitly.让 DerivedA::init() 显式调用 BaseA::init()。
  4. Make pointer_ a virtual method.使 pointer_ 成为虚拟方法。

Note the use of "covariant return types" for the virtual methods.请注意对虚拟方法使用“协变返回类型”。

class BaseA
    {
    public:
       virtual BaseB* pointer() { return &b_; }
       // etc.
    };

class DerivedA : public BaseA
    {
    public:
       virtual DerivedB* pointer() { return &b_; }
       // etc.
    };

wouldn't Base_A have a pointer to Base_B if Base_A::init() was ever called?如果 Base_A::init() 曾经被调用过,Base_A 不会有一个指向 Base_B 的指针吗? why wouldn't you init the base class?为什么不初始化基础 class?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM