Re: call base class constructor from derived class constructor
On Nov 5, 7:01 pm, "Alf P. Steinbach" <al...@start.no> wrote:
* James Kanze:
On Nov 5, 1:47 pm, Ron Natalie <r...@spamcop.net> wrote:
Rahul wrote:
While working with Java, i came across super() which passes
values to base class constructor from derived class
constructor. I was wondering if this could be implemented
in c++ by any mechanism as super is not supported by c++,
atleast by MS vc++ 6.0.
C++ is not Java. Contructors are automatically called in the
language-defined sequence in C++. You can not alter that NOR
can you call them directly.
That's true in Java as well, with the restriction that 1) you
can only have a single base class, and 2) members are either
basic types or pointers, and don't have constructors. The only
difference is the syntax you use to pass arguments to the base
class constructor. In Java, since there's only one, the keyword
super() is sufficient,
In most cases super() would be sufficient in C++ too.
The way Java uses it? Only in the case of single inheritance.
A reasonable extension might be to provide a keyword super, with
semantics something like those of this, except that name lookup
after it ignores the current class (and only considers base
classes). Since it would define yet another context with its
own rules for name lookup, writing it up would require a good
deal of work; the benefits probably aren't worth it.
And Visual C++ provides that as a language extension,
'__super', relying on ordinary overload resolution, see e.g.
<url:http://msdn2.microsoft.com/en-us/library/94dw1w7x(VS.80).aspx>.
To some degree one may simulate the effect of '__super' by
typedef'ing a 'Base',
typedef MyBaseClass Base;
but in particular, when MyBaseClass is something like X<Y, Z,
...> it makes you repeat the whole shebang.
And in the case of multiple inheritance, it doesn't work at all.
'super' would have been a great addition to C++0x.
Especially since there's years and years of existing practice... ;-)
There was actually some discussion about it at the beginning of
the C++ standardization effort. Then someone pointed out the
typedef solution, and it was decided that since it was so easily
simulated, the effort wasn't worth it. Given the fact that the
typedef solution doesn't work with multiple inheritance, and is,
as you point out, very long winded (and fragile) when templated
base classes are involved, the issue could be reconsidered.
But I think it's a bit late for this round of standardization.
I don't think support for either super::something or
super->something would be that difficult, but there's a good
deal of text which would have to be added section 3.4, since
name lookup in such cases isn't exactly like any existing name
lookup. And other parts of the standard might be affected as
well: a priori, I would expect either form above to make the
name dependent, for example, so there would be modifications in
section 14 as well.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34