7

Keyword __super is Microsoft specific. It is used to access virtual methods of parent class. Do you know alternative keywords for borland c++ / delphi compilers?

class MyBaseClass
{
    virtual void DoSomething();
};

class MyDerivedClass : public MyBaseClass
{
    virtual void DoSomething();
};

void MyBaseClass::DoSomething()
{
    // some code
}

void MyDerivedClass::DoSomething()
{
    __super::DoSomething();  // calls implementation of base class - no need to know name of base class

    // implementation specific to derived class adding new functionality
}
pnuts
  • 58,317
  • 11
  • 87
  • 139
truthseeker
  • 1,220
  • 4
  • 25
  • 58

3 Answers3

9

The equivalent in Delphi is inherited. So far as I know, there is no equivalent in C++ Builder and of course __super is a non-standard MS extension.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Still no __super in C++ 2011 standard. You have to know your parent class name. Which sucks, IMHO. I like the idea of "non propagation of variable details". So I guess, in C++ 2011 you'd typedef once, and avoid repetition of the parent classname throughout the unit. – Warren P Nov 30 '11 at 14:27
  • @WarrenP In the face of multiple inheritance, `__super` is tricky to implement. I'm sure there are good reasons for C++ being the way it is. – David Heffernan Nov 30 '11 at 14:32
  • 1
    Did you know? super you can put on the first line only , while inherited can be called from any line in the procedure/function? body. – none Nov 30 '11 at 15:42
  • This related question show an alternative to `__super` (along with a mention of the great book "The Design and Evolution of C++", which I used to own but my dog ate.): http://stackoverflow.com/questions/180601/using-super-in-c – Leonardo Herrera Dec 01 '11 at 10:32
  • @LeonardoHerrera I have that book on my desk right now. Your dog has good taste!! – David Heffernan Dec 01 '11 at 10:49
8
  • Delphi: inherited MyMethod(MyParam); or shortened inherited;
  • C++Builder: MyBaseClass::MyMethod(MyParam);
Ondrej Kelle
  • 36,941
  • 2
  • 65
  • 128
  • `MyBaseClass` isn't quite the same as `__super`. Is there an equivalent in C++Builder? – David Heffernan Nov 30 '11 at 13:09
  • I agree, it's not quite the same. I'm not aware of an equivalent in C++Builder, but you could [declare a `typedef`](http://stackoverflow.com/questions/180601/using-super-in-c). – Ondrej Kelle Nov 30 '11 at 13:14
  • 1
    A per unit `typedef TMyParentClassName __super` is still less repetition than repeating a TMyParentClassName a dozen times in your .cpp file. – Warren P Nov 30 '11 at 14:30
  • @DavidHeffernan: how is calling `MyBaseClass::DoSomething()` different than calling `__super::DoSomething()`? – Remy Lebeau Dec 01 '11 at 19:09
  • @WarrenP, the typedef would have to be per-class, not per-unit. – Remy Lebeau Dec 01 '11 at 19:09
  • 1
    @remy Because you violate DRY. Imagine inserting another class in between MyBaseClass and MyDerivedClass. Inherited or __would find it, but the hard coded C++ code would not. – David Heffernan Dec 01 '11 at 19:19
  • @DavidHeffernan: I don't know what `DRY` is. In any case, if `MyDerivedClass` derives from `MyBaseClass`, then calling `MyBaseClass::DoSomething()` works. If `MyDerivedClass` is later changed to derive from something else, then `MyBaseClass::` references have to be updated accordingly. Using a typedef inside of `MyDerivedClass`, ie: `class MyDerivedClass : MyBaseClass { typedef MyBaseClass inherited; ... };` and calling `inherited::DoSomething()`, lessens that blow. Then you only have to update one line when changing the ancestor of `MyDerivedClass`. – Remy Lebeau Dec 01 '11 at 19:51
  • @Remy DRY is Don't Repeat Yourself and the comment above perfectly describes the different between inherited/__super and standard C++ – David Heffernan Dec 01 '11 at 20:21
3

In Delphi, the equivalent is inherited. You can see examples of it in use in the RTL and VCL sources.

Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477