简体   繁体   中英

Class arguments inside Constructor arguments of another Class in C++

Could someone please help me with this one? It might sound simple but I'm having a hard time.

class Base { 
    int x;
    int y;

public:    
    Base(int a, int b) : x(a), y(b) {}

};

class Derived : public Base {
    Base a;
    Base b;
    Base c;
    std::string name;

public:
    Derived(Base a_, Base b_, Base c_, std::string name_): a(a_), b(b_), c(c_), name(name_) {}
};

Here's the problem. I can't seem to be able to initialize this, I've tried several ways and searched the web yet I can't find a correct answer for this.

int main() {
    Derived var1({1,2}, {3,4}, {5,6}, "TEST");
}

class1 constructor takes two parameters:

class1(int a, int b)

class2 inherits from class1 , however class2 's constructor fails to pass the two required parameters to its superclass.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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