简体   繁体   中英

Calling superclass from within subclass

In the following situation, how can I make it such that I can call the parameterized constructor of SuperClass from within the constructor of SubClass ?

class SuperClass {

    int superMember;

    SuperClass( int superMember ) {
        this->superMember = superMember;
    };

    virtual void doStuff() = 0;
};

class SubClass : public SuperClass {

    int subMember;

    SubClass( int superMember, int subMember ) {
        this->subMember = subMember;
        SuperClass( superMember );
    };

    void doStuff() { 
        //... 
    };
};

Should there even be a constructor in SuperClass (given that it's abstract), or should I move the member-initialisation code into a separate function, say init( ... ) in SuperClass ? In that case, how would I then call this function from within SubClass ?

Initialization of a base class should be done like that:

SubClass( int superMember, int subMember ) :  SuperClass( superMember) {
        this->subMember = subMember;       
    }

you even use an initialization list for members too

SubClass( int superMember, int subMember_ ) :  
   SuperClass( superMember), subMember(subMember_){}   

(notice you don't need semicolon after } )

But before that you need to inherit from the Superclass like that

class SubClass : public SuperClass {/*...*/}

(if I understand your intentions right)

and after that you need specify access level to your methods and constructors, I believe they should be public .

Call the SuperClass constructor in the initializer list of SubClass :

class SubClass : public SuperClass {
    int subMember;

// Made constructor 'public'.
public:
    SubClass(int a_superMember, int a_subMember) : SuperClass(a_superMember), 
                                                   subMember(a_subMember) {}
};

Even though SuperClass is abstract it still requires a constructor, in this case to initialize superMember .

In SuperClass the constructor must be visible to SubClass : either public or protected :

class SuperClass {

    int superMember;

public: // or protected:
    SuperClass( int superMember ) {
        this->superMember = superMember;
    };

    virtual void doStuff() = 0;
};

i have written below the code to call the parameterized constructor. it is always better to do the initialization in the parameterized constructor itself. One more thing you should keep in mind is that if your inheritance is private then you cannot use the init() functions.

 class SuperClass {

        int superMember;

        SuperClass( int superMember ) {
            this->superMember = superMember;
        };

        virtual void doStuff() = 0;
    };

    class SubClass :public/private/protected SuperClass

{

        int subMember;

        SubClass( int superMember, int subMember ) :superClass(superMember)

{ this->subMember = subMember;

        };

        void doStuff() { 
            //... 
        };
    };

hope this will help you.

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