Is the possible to have all the public constructors of the publicbase class as the constructors of a

P

Peng Yu

Hi,

I want B has all the constructors that A has. Obviously, the code
below would not work. I could define a corresponding B's constructor
for each A's constructor. But if A has many constructors, it would be
inconvenient. I'm wondering if there is any way to inherent all A's
constructor implicitly.

Thanks,
Peng

class A{
public:
A(int x) : _x(x) { }
private:
int _x;
};

class B : public A { };
 
R

Rolf Magnus

Peng said:
Hi,

I want B has all the constructors that A has. Obviously, the code
below would not work. I could define a corresponding B's constructor
for each A's constructor. But if A has many constructors, it would be
inconvenient. I'm wondering if there is any way to inherent all A's
constructor implicitly.

There is no way. You have to define them yourself.
 
K

Kai-Uwe Bux

Peng said:
Hi,

I want B has all the constructors that A has. Obviously, the code
below would not work. I could define a corresponding B's constructor
for each A's constructor. But if A has many constructors, it would be
inconvenient. I'm wondering if there is any way to inherent all A's
constructor implicitly.

There is no way to inherit constructors. One reason is that a derived class
may add more data members, which would need initialization.
class A{
public:
A(int x) : _x(x) { }
private:
int _x;
};

class B : public A { };

That said, you can forward constructors using templates. E.g., the following
adds a virtual destructor:


template < typename T >
class virtual_destructor : public T {
public:

virtual_destructor ( void ) :
T ()
{}

template < typename A >
virtual_destructor ( A a ) :
T ( a )
{}

template < typename A, typename B >
virtual_destructor ( A a, B b ) :
T ( a, b )
{}

template < typename A, typename B, typename C >
virtual_destructor ( A a, B b, C c ) :
T ( a, b, c )
{}

template < typename A, typename B, typename C,
typename D >
virtual_destructor ( A a, B b, C c, D d ) :
T ( a, b, c, d )
{}

template < typename A, typename B, typename C,
typename D, typename E >
virtual_destructor ( A a, B b, C c, D d, E e ) :
T ( a, b, c, d, e )
{}

template < typename A, typename B, typename C,
typename D, typename E, typename F >
virtual_destructor ( A a, B b, C c, D d, E e, F f ) :
T ( a, b, c, d, e, f )
{}

virtual ~virtual_destructor ( void ) {}

}; // virtual_destructor<T>


This requires some knowledge/guess about the maximum number of arguments in
a constructor. Also, it does not handle reference parameters in
constructors nicely.

It is quite possible that this becomes simpler with variadic templates in
the next standard.



Best

Kai-Uwe Bux
 
J

Juha Nieminen

Kai-Uwe Bux said:
It is quite possible that this becomes simpler with variadic templates in
the next standard.

I don't think it's just possible, but actually one of the main reasons
why variadic templates are being added to the next standard: To allow
perfect parameter forwarding.
 
A

Alexander Dong Back Kim

Hi,

I want B has all the constructors that A has. Obviously, the code
below would not work. I could define a corresponding B's constructor
for each A's constructor. But if A has many constructors, it would be
inconvenient. I'm wondering if there is any way to inherent all A's
constructor implicitly.

Thanks,
Peng

class A{
public:
A(int x) : _x(x) { }
private:
int _x;

};

class B : public A { };

Hi Peng,

I think constructors and destructors are not inheritable. Therefore,
you can't override base class's constructors.

cheers,
Alex Kim
 
J

Juha Nieminen

Pete said:
It also becomes unnecessary, because C++0x directly supports inheriting
constructors. <g>

Well, sort of. You still have to explicitly write a
"using BaseClass::BaseClass;" line in your derived class. The
constructor inheritance is not automatic.

Still better than having to explicitly write derived class
constructors which call the equivalent base class constructors,
though (because the "using" line will inherit *all* the constructors
of the base class at once).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top