Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is the possible to have all the public constructors of the publicbase class as the constructors of a derived class?

Reply
Thread Tools

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

 
 
Peng Yu
Guest
Posts: n/a
 
      09-18-2008
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 { };

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      09-18-2008
Peng Yu wrote:

> 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.

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      09-18-2008
Peng Yu wrote:

> 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
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      09-18-2008
Kai-Uwe Bux wrote:
> 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.
 
Reply With Quote
 
Alexander Dong Back Kim
Guest
Posts: n/a
 
      09-19-2008
On Sep 19, 2:18 am, Peng Yu <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      09-19-2008
Pete Becker wrote:
> 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).
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola MCSE 4 11-15-2006 02:40 AM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola Microsoft Certification 3 11-14-2006 05:18 PM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola MCSD 3 11-14-2006 05:18 PM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd realexxams@yahoo.com Microsoft Certification 0 05-10-2006 02:35 PM
microsoft.public.dotnet.faqs,microsoft.public.dotnet.framework,microsoft.public.dotnet.framework.windowsforms,microsoft.public.dotnet.general,microsoft.public.dotnet.languages.vb Charles A. Lackman ASP .Net 1 12-08-2004 07:08 PM



Advertisments