C
cppaddict
Hi,
I know that C++ does not have an explicit super() constructor for
calling a Base class constructor from a Derived class's constructor,
but my understanding is that C++ implements this functionality via its
initialization list.
Say I have a Base class with a constructor that looks like:
Base::Base(int x) {
//does stuff #1
}
Now if I want my Derived class also to have a constructor that takes
an int, does the same stuff as Base(int), and then does some
additional stuff, can I simply do:
Derived:erived(int x) : Base(x) { //that call should do stuff #1
//does stuff #2
}
The above code (when filled in properly) compiles and runs, but I want
to make sure my understanding is correct. Is the initialization list,
as used above, essentially the C++ syntactic analog of Java's super()?
If not, what are the differences?
Thanks for any clarification,
cpp
I know that C++ does not have an explicit super() constructor for
calling a Base class constructor from a Derived class's constructor,
but my understanding is that C++ implements this functionality via its
initialization list.
Say I have a Base class with a constructor that looks like:
Base::Base(int x) {
//does stuff #1
}
Now if I want my Derived class also to have a constructor that takes
an int, does the same stuff as Base(int), and then does some
additional stuff, can I simply do:
Derived:erived(int x) : Base(x) { //that call should do stuff #1
//does stuff #2
}
The above code (when filled in properly) compiles and runs, but I want
to make sure my understanding is correct. Is the initialization list,
as used above, essentially the C++ syntactic analog of Java's super()?
If not, what are the differences?
Thanks for any clarification,
cpp