Quantcast
Channel: What are the rules for calling the base class constructor? - Stack Overflow
Viewing all articles
Browse latest Browse all 22

Answer by luke for What are the rules for calling the base class constructor?

$
0
0

Base class constructors are automatically called for you if they have no argument. If you want to call a superclass constructor with an argument, you must use the subclass's constructor initialization list. Unlike Java, C++ supports multiple inheritance (for better or worse), so the base class must be referred to by name, rather than "super()".

class SuperClass{    public:        SuperClass(int foo)        {            // do something with foo        }};class SubClass : public SuperClass{    public:        SubClass(int foo, int bar)        : SuperClass(foo)    // Call the superclass constructor in the subclass' initialization list.        {            // do something with bar        }};

More info on the constructor's initialization list here and here.


Viewing all articles
Browse latest Browse all 22

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>