sergs
sergs

Reputation: 137

Does C++ have a keyword which allows to refer to a base type from a derived class?

For an example:

class some_base
{
 ... // some valid code
};

class derived : public some_base
{
  ...

  derived& operator=( const derived& that )
  {
    some_base::operator=( that );

    ...

    return *this;
}; 

It'd be good if we were able to use some keyword in a derived's assign operator instead of a some_base qualifier. Compiler knows a type we inherit from so it's not a problem, in my opinion.

So the question is does С++ provides an auxiliary keyword to simplify the coder's life ?

Upvotes: 3

Views: 3000

Answers (2)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145279

Some compilers provide a base class keyword as an extension. It seems that variants of the name super are common, e.g. Visual C++'s __super (I would expect that as an intended plug-in replacement, Intel's compiler also supports __super). But standard C++ provides no such thing, although there was once a proposal.

To achieve about the same, simply define a type alias Base in every class.

In another thread, user Roddy argues that such an alias should be private “to avoid the problem when the 'inherited' is erroneously omitted from a class but a subclass tries to use it”.

Upvotes: 1

Jaa-c
Jaa-c

Reputation: 5137

Compiler knows a type we inherit from so it's not a problem

Unless you derive from multiple base classes - in such case, how would compiler decide which one it is?

So no, there is no such keyword, all you can do is put one into your coding standard.

Upvotes: 4

Related Questions