473,666 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 { };

Sep 18 '08 #1
6 1364
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.

Sep 18 '08 #2
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_destruc tor : public T {
public:

virtual_destruc tor ( void ) :
T ()
{}

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

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

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

template < typename A, typename B, typename C,
typename D >
virtual_destruc tor ( 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_destruc tor ( 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_destruc tor ( A a, B b, C c, D d, E e, F f ) :
T ( a, b, c, d, e, f )
{}

virtual ~virtual_destru ctor ( void ) {}

}; // virtual_destruc tor<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
Sep 18 '08 #3
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.
Sep 18 '08 #4
On 2008-09-18 09:31:03 -0700, Kai-Uwe Bux <jk********@gmx .netsaid:
>
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.
It also becomes unnecessary, because C++0x directly supports inheriting
constructors. <g>

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Sep 18 '08 #5
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::Base Class;" 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).
Sep 19 '08 #6
On 2008-09-19 03:19:27 -0700, Juha Nieminen <no****@thanks. invalidsaid:
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::Base Class;" line in your derived class. The
constructor inheritance is not automatic.
Of course not. Automatic inheritance of constructors would be a
disaster. Nevertheless, inherited constructors will be supported in
C++0x.
>
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).
Indeed; that's how inheritance works.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Sep 19 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
21382
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have an idea that we can have private constructors and destructors but am not able to find a situation where they can be used... Regards RVG rajeshgarg@opussoft.com
7
13624
by: Beach Potato | last post by:
I guess I've been out of C++ for a while, since right now I don't seem to get a simple solution for overriding inherited constrictors. What worked in Borland C++ & Pascal (and Java, if I remember correctly) as: ------------------------------------------- class Base { public: Base(); }; Base::Base() { printf("Base"); } class Derived : Base { public: Derived(); override; }; Derived::Derived() { /*inherited;*/ printf("Derived"); }
8
1894
by: Ernst Murnleitner | last post by:
Hello Readers, Is there a way that only one class can construct a class A and its inherited classes A2, A3 etc.? I want to construct a class A (and the inherited classes A2, A3 etc.) from a (factory) class Fa. I wanted to make that only F can call new A
4
4612
by: Xavier | last post by:
Hi, I have a question, in a "dreaded diamond" situation, regarding the following code: ---- Begin code #include <iostream> using namespace std;
13
7714
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. They don't actually protect any encapsulation or anything, and for all the actual protection they offer, they might as well be public. For example: class B { protected:
14
33262
by: Dave Booker | last post by:
It looks like the language is trying to prevent me from doing this sort of thing. Nevertheless, the following compiles, and I'd like to know why it doesn't work the way it should: public class ComponentA { static string s_name = "I am the root class."; public string Name { get {return s_name;} } }
3
17964
by: Goran | last post by:
Hi @ all! Is it possible to overload a member variable? Example: class ClassA_t {
8
1137
by: not_a_commie | last post by:
It seems I need one more type of member protection in my C# classes. I run into this regularly. I've got a class X in assembly A. Class Y in assembly B inherits from X and has a constructor that takes an instance of X. The only way class Y can access data on the instance of X coming into its constructor is if I make it public on X. Public is too strong in this situation if data is only used in constructors of derived classes. I want the...
9
2308
by: Anthony Williams | last post by:
Hi, Should the following compile, and what should it print? #include <memory> #include <iostream> void foo(std::auto_ptr<intx) { std::cout<<"copy"<<std::endl;
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8550
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7385
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6192
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4366
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.