小编Gab*_*Caz的帖子

Base constructor calls derived constructor depending on input - Choose object sub-type at runtime

Let's say I have a Human class:

class Human {
public:
    bool isFemale;
    double height;
    Human(bool isFemale, double height) {
        this->isFemale = isFemale;
        this->height = height;
    }
};
Run Code Online (Sandbox Code Playgroud)

and derived classes, such as Female and Male, which implement their own methods. Do I have a way, in C++11, to determine at runtime, depending on the inputs into the Human constructor, which "sub-type" (Male or Female) Human should be? I am putting different behaviour for Male and Female in their respective …

c++ oop design-patterns factory c++11

1
推荐指数
1
解决办法
112
查看次数

标签 统计

c++ ×1

c++11 ×1

design-patterns ×1

factory ×1

oop ×1