Bjarne Stroustrup mentions in Design and Evolution of C++ that super as a keyword was considered by the ISO C++ Standards committee the first time C++ was standardized.
Dag Bruck proposed this extension, calling the base class "inherited." The proposal mentioned the multiple inheritance issue, and would have flagged ambiguous uses. Even Stroustrup was convinced.
After discussion, Dag Bruck (yes, the same person making the proposal) wrote that the proposal was implementable, technically sound, and free of major flaws, and handled multiple inheritance. On the other hand, there wasn't enough bang for the buck, and the committee should handle a thornier problem.
Michael Tiemann arrived late, and then showed that a typedef'ed super would work just fine, using the same technique that was asked about in this post.
So, no, this will probably never get standardized.
If you don't have a copy, Design and Evolution is well worth the cover price. Used copies can be had for about $10.
Answer from Max Lybbert on Stack OverflowBjarne Stroustrup mentions in Design and Evolution of C++ that super as a keyword was considered by the ISO C++ Standards committee the first time C++ was standardized.
Dag Bruck proposed this extension, calling the base class "inherited." The proposal mentioned the multiple inheritance issue, and would have flagged ambiguous uses. Even Stroustrup was convinced.
After discussion, Dag Bruck (yes, the same person making the proposal) wrote that the proposal was implementable, technically sound, and free of major flaws, and handled multiple inheritance. On the other hand, there wasn't enough bang for the buck, and the committee should handle a thornier problem.
Michael Tiemann arrived late, and then showed that a typedef'ed super would work just fine, using the same technique that was asked about in this post.
So, no, this will probably never get standardized.
If you don't have a copy, Design and Evolution is well worth the cover price. Used copies can be had for about $10.
I've always used "inherited" rather than super. (Probably due to a Delphi background), and I always make it private, to avoid the problem when the 'inherited' is erroneously omitted from a class but a subclass tries to use it.
class MyClass : public MyBase
{
private: // Prevents erroneous use by other classes.
typedef MyBase inherited;
...
My standard 'code template' for creating new classes includes the typedef, so I have little opportunity to accidentally omit it.
I don't think the chained "super::super" suggestion is a good idea- If you're doing that, you're probably tied in very hard to a particular hierarchy, and changing it will likely break stuff badly.
Videos
Was doing java all this while, now i'm slowly learning c++. Got to OOP and now i'm confused how to replace super the right way.
In addition to all the above, consider the following scenario:
You start out with these two classes.
class A { int i = 20; A() { System.out.println("I am in constructor of the parent class"); } }class B extends A { int i = 30; B() { System.out.println("I am in constructor of the subclass"); } }You compile both classes, it creates two class files:
A.classandB.classNow you change your mind and rewrite A:
class A { int i = 10; A() { System.out.println("I am in constructor of the parent class"); } }You recompile A, so you get a new
A.class, which you replace the oldA.classwith
Now the question is: how would B know that As initialisation code has changed? You haven't recompiled B, nor should you be required to do so. After all, B may already be packaged up in a different jar, maybe even in a different product you have no control over.*
The only reasonable way to support this case is for Bs constructor to start with calling As constructor and let A initialise itself.
*This may sound far-fetched at first, but this is exactly what happens when internal implementation details of JDK classes are changed. All the previously compiled Java applications in the world should run seamlessly with the new JDK.
Much of this is spelled out in the JLS section 15.11:
15.11.2 Accessing Superclass Members using
super
The form super.Identifier refers to the field named Identifier of the current object, but with the current object viewed as an instance of the superclass of the current class.
The key word there is of the current object. If there is no current object, such as the case with a static method, you can't user the super method.
When it comes to the constructor, the super class is responsible for initializing its fields. This is necessary because there may be private fields within the super class that a subclass has no access to.
Furthermore, requiring the subclass to instantiate the state of the super class may require the subclass to have more information than necessary and also requires a tighter coupling between superclass and subclass than is desirable - any change to the fields in the superclass would require a change in all the subclasses. This is a bad thing.
Thus, you prevent this bad thing from happening by having the subclass call the constructor of the superclass when it is created.