Using super in C

0 votes

The following phrase is part of my coding approach:

class Derived : public Base
{
   public :
      typedef Base super; // note that it could be hidden in
                          // protected/private section, instead

      // Etc.
} ;

This lets me to use "super" in constructors as an alias for Base, for instance:

Derived(int i, int j)
   : super(i), J(j)
{
}

even when the method is used within the overridden version of the base class:

void Derived::foo()
{
   super::foo() ;

   // ... And then, do something else
}

It can even be chained (although I haven't yet figured out how to utilise that):

class DerivedDerived : public Derived
{
   public :
      typedef Derived super; // note that it could be hidden in
                             // protected/private section, instead

      // Etc.
} ;

void DerivedDerived::bar()
{
   super::bar() ; // will call Derived::bar
   super::super::bar ; // will call Base::bar

   // ... And then, do something else
}

In any case, I find "typedef super" to be really helpful, for instance, when Base is either verbose or template-based.

Super is really implemented in both Java and C#, where it is named "base" unless I'm mistaken. 

But this keyword is absent from C++.

Consequently, I have the following inquiries:

Is this typedef use extremely frequent, uncommon, or never encountered in the code you deal with?

Is this usage of typedef perfectly acceptable (that is, can you see any good or mediocre arguments against it)?

Should the term "super" be encouraged? Should it be somewhat standardised in C++, or is its current use through a typedef sufficien?

Jul 13 in C++ by Nicholas
• 5,020 points
18 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In C++

0 votes
1 answer

Using getline() in C++

If you use getline() after cin >> anything, you must first flush the newline character from the buffer.  You can achieve this by using the cin.ignore() It would be something like this: string messageVar; cout ...READ MORE

answered Jun 1 in C++ by Damon
• 4,960 points
68 views
0 votes
1 answer

Using :: (scope resolution operator) in C++

You're mostly correct regarding cout and cin. ...READ MORE

answered Jun 27 in C++ by Damon
• 4,960 points
20 views
0 votes
0 answers

take string input using char* in C and C++

I'm having trouble accepting a string (actually a character array) as input.  Consider the following declaration: char* s; I have to input a string ...READ MORE

Jul 1 in C++ by Nicholas
• 5,020 points
12 views
+1 vote
0 answers

Parse (split) a string in C++ using string delimiter (standard C++)

In C++, I'm processing a string like follows: using namespace std; string parsed,input="text to be parsed"; stringstream ...READ MORE

Jul 4 in C++ by Nicholas
• 5,020 points
25 views
0 votes
0 answers

How do I declare a 2d array in C++ using new?

How do I declare a two-dimensional array using new? For example, for a "typical" array, I would:      int* ary = new int[Size] but int** ...READ MORE

Jul 15 in C++ by Nicholas
• 5,020 points
23 views
+1 vote
0 answers

Using push_back vs at in a vector in C++

I'm not sure how to use vectors in C++.  It has to do with the vector's push back technique.  I used push back to insert entries into the vector in the first programme.  I used at() to insert entries into the vector in the second application. #include <iostream> #include <vector> #include <string> using namespace std; int main ...READ MORE

Jul 22 in C++ by Nicholas
• 5,020 points
13 views
0 votes
0 answers

const int *p vs. int const *p - Is const after the type acceptable?

My coworker is 0 for 2 on questions inspired by him (1, 2), so I decided to give him a chance to catch up. Our most recent argument is about the placement of "const" on declarations. He believes it should be placed either in front of the type or after the pointer.  The explanation is that this is what everyone else does, and different styles are likely to be confusing.  Thus, a constant pointer to int and a constant pointer to int would be: const int *i; ...READ MORE

Jul 22 in C++ by Nicholas
• 5,020 points
19 views
0 votes
1 answer

Python `if x is not None` or `if not x is None`?

There's no performance difference, as they compile ...READ MORE

answered Sep 3, 2018 in Python by Priyaj
• 58,080 points
1,731 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,080 points
206 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,080 points
1,416 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP