Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call base class constructor without naming its class

Tags:

c++

c++11

c++14

class MyDerived: public Incredble<Difficult< And<Complicated, Long>>, And<Even, Longer>, BaseClass, Name>
{
public:
  MyDerived();
}


MyDerived::MyDerived
: ???(params)
{}

Is there any way to call a base constructor without writing its full name and without typedeffing it?

The reason is clearly to avoid code duplication and introducing multiple positions to change if a detail in the base class template params changes.

Level 2 of this:

template <uint32 C>
class MyDerived: public Incredble<Difficult< And<Complicated, Long>>, And<Even, Longer>, BaseClass, Name>
{
public:
  MyDerived();
}

template <uint32 C>
MyDerived::MyDerived<C> 
: ???(C)
{
}
like image 767
vlad_tepesch Avatar asked Sep 09 '16 13:09

vlad_tepesch


1 Answers

You could use injected-class-name. Incredible<...>::Incredible refers to itself, and since MyDerived isn't a class template, unqualified lookup will look in the scope of its base classes:

MyDerived::MyDerived
: Incredble(params)
{}

If Incredible is a dependent name, then you need to qualify it. You can actually simply use the derived type name to qualify the base class's injected-class-name (h/t Johannes Schaub-litb):

MyDerived::MyDerived
: MyDerived::Incredible(params)
{}

This will work in all cases.

like image 195
Barry Avatar answered Oct 13 '22 05:10

Barry