简体   繁体   English

C ++调用另一个子类的公共方法

[英]C++ call public method of another child class

is there an elegant version to access a method of an another child class? 是否有一个优雅的版本可以访问另一个子类的方法? For a better understanding please watch my c++ code. 为了更好的理解,请观看我的c ++代码。

Its really important that external functions / classes can access furthermore the addchild() method!! 外部函数/类可以进一步访问addchild()方法非常重要!

C++ Code: C ++代码:

class User {
    protected:
        int userid;
        std::string givenname;
        std::string surname;
        std::string birthday;
};

class Child: public User {
    public:
        int addchild() {
            //Doing the required stuff to add a new child
        }
};

class Parent: public User {
    public:
        int addparent() {
            //Here we have to add a new child among others
            addchild();
        }
};


int main() {
    Child child1;
    child1.addchild();

    Parent parent1;
    parent1.addparent();
}

UML: UML:

在此处输入图片说明

You could use CRTP for your parent class: https://www.fluentcpp.com/2017/05/12/curiously-recurring-template-pattern/ (more general: https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern ) 您可以将CRTP用于您的父类: https : //www.fluentcpp.com/2017/05/12/curiously-recurring-template-pattern/ (更通用: https : //en.wikipedia.org/wiki/Curiously_recurring_template_pattern

Because it got no attention I changed the original code sample so that it is using CRTP: 因为没有引起注意,所以我更改了原始代码示例,使其使用了CRTP:

#include <string>

class User {
    protected:
        int userid;
        std::string givenname;
        std::string surname;
        std::string birthday;
};

class Child: public User {
    public:
        int addchild() {
            //Doing the required stuff to add a new child
        }
};

template <typename BASE>
class Parent: public BASE {
    public:
        int addparent() {
            //Here we have to add a new child among others
            this->addchild();
        }
};


int main() {
    Child child1;
    child1.addchild();

    Parent<Child> parent1;
    parent1.addparent();
}

It cannot be done in the current design. 在当前设计中无法完成。 You either need to have an object of Child inside Parent class or pass an object of Child as a argument to addparent() function. 你要么需要有一个对象Child里面Parent类或传递的对象Child作为参数传递给addparent()函数。

class Parent: public User {
    private:
        Child child; //Have an instance of Child class here to be able 
                     //to use addchild() in this class 
    public:            
        int addparent() {
           //Here we have to add a new child among others
           child.addchild();
        }
};

Or passing a argument like this: 或传递这样的参数:

class Parent: public User {
    private:
        Child child; //Have an instance of Child class here to be able 
                     //to use addchild() in this class 
    public:            
        int addparent(Child & child) {
             child.addchild(); 
    }
};

I am not sure about how you are structuring your entire class and what you intend to do with them. 我不确定您如何安排整个班级以及打算如何与他们合作。

Explore design patterns at https://www.tutorialspoint.com/design_pattern/design_pattern_overview.htm 通过https://www.tutorialspoint.com/design_pattern/design_pattern_overview.htm探索设计模式

Also, look up the concept of composition in C++ at http://www.learncpp.com/cpp-tutorial/102-composition/ 另外,在http://www.learncpp.com/cpp-tutorial/102-composition/上查找C ++中的组合概念

This might help you come up with a better design. 这可以帮助您提出更好的设计。

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

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