C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Two base classes and one derived class

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
sriya
Guest





PostPosted: Tue Nov 29, 2005 10:22 am    Post subject: Two base classes and one derived class Reply with quote



hi all,

Is there any method using which I can avoid the constructors of the
base classes to be called when the object of the derived class is
created??

I have two base classes and one derived class. Now, i want to avoid the
constructor in either of the base class or in both. can I do this??

pls help me.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ulrich Eckhardt
Guest





PostPosted: Tue Nov 29, 2005 2:00 pm    Post subject: Re: Two base classes and one derived class Reply with quote



sriya wrote:
Quote:
Is there any method using which I can avoid the constructors of the
base classes to be called when the object of the derived class is
created??

No, it is not possible.

Quote:
I have two base classes and one derived class. Now, i want to avoid the
constructor in either of the base class or in both. can I do this??

You should rather tell us what problems you want to solve so someone can
suggest a proper solution. The way you want to do it might be possible by
some hack or so but is otherwise too twisted and probably not suitable for
C++. It all depends on what exactly you want.

Uli


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Greg Herlihy
Guest





PostPosted: Tue Nov 29, 2005 2:08 pm    Post subject: Re: Two base classes and one derived class Reply with quote



sriya wrote:
Quote:
hi all,

Is there any method using which I can avoid the constructors of the
base classes to be called when the object of the derived class is
created??

I have two base classes and one derived class. Now, i want to avoid the
constructor in either of the base class or in both. can I do this??

pls help me.

Declare a new constructor in each of base classes that do not do
whatever it is that the existing constructors do in those classes that
you find so objectionable.

But otherwise, no, one of the constructors in each base class will be
called when instantiating a derived class.

Greg


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Thomas Richter
Guest





PostPosted: Tue Nov 29, 2005 2:09 pm    Post subject: Re: Two base classes and one derived class Reply with quote

Hi,

Quote:
Is there any method using which I can avoid the constructors of the
base classes to be called when the object of the derived class is
created??

Well, objects that are used have to be constructed, and thus *a*
constructor must be called. However, from within the derived class,
you may use *any* of the constructors the base class provides.

Specifically, you could provide a "dummy" constructor for the base
class, and call this from the derived class.

Quote:
I have two base classes and one derived class. Now, i want to avoid the
constructor in either of the base class or in both. can I do this??

It seems to me that you might also want to look into "virtual inheritance",
but without knowing more about the problem you're trying to solve,
it is hard to give concrete advices.

So long,
Thomas


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
news
Guest





PostPosted: Tue Nov 29, 2005 2:10 pm    Post subject: Re: Two base classes and one derived class Reply with quote

Hi there!

I'm no expert, but as I understand things, you can make the constructors for
the base classes 'virtual', then override those functions in the derived
class, making them empty if you wish.

However, if you dont have access to the base class code then you're probably
stuck.

=r=

"sriya" <ksadiraju (AT) gmail (DOT) com> wrote

Quote:
hi all,

Is there any method using which I can avoid the constructors of the
base classes to be called when the object of the derived class is
created??

I have two base classes and one derived class. Now, i want to avoid the
constructor in either of the base class or in both. can I do this??

pls help me.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Maxim Yegorushkin
Guest





PostPosted: Tue Nov 29, 2005 3:57 pm    Post subject: Re: Two base classes and one derived class Reply with quote


sriya wrote:


Quote:
Is there any method using which I can avoid the constructors of the
base classes to be called when the object of the derived class is
created??


Yes, do not derive those classes.


Quote:
I have two base classes and one derived class. Now, i want to avoid the
constructor in either of the base class or in both. can I do this??


You are trying to abuse c++. Reconsider your design.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
dbradley
Guest





PostPosted: Wed Nov 30, 2005 1:43 am    Post subject: Re: Two base classes and one derived class Reply with quote

Quote:
you can make the constructors for the base classes 'virtual'

Constructors can't be virtual unless C++ has recently changed in some
way I'm unaware of.

The OP cannot avoid constructing the base instances in some fashion as
others have pointed out.

As another pointed out, it would be interesting to know what problem is
trying to be solved here. It's likely that multiple inheritance isn't
the correct solution given what he's asking.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
werasm
Guest





PostPosted: Wed Nov 30, 2005 1:45 am    Post subject: Re: Two base classes and one derived class Reply with quote


news wrote:
Quote:
Hi there!

I'm no expert, but as I understand things, you can make the constructors for
the base classes 'virtual', then override those functions in the derived
class, making them empty if you wish.

And how do your propose to make the constructors virtual? - I'm not
sure I understand. Can you give us an example of your solution please?

Par 12.1/4 of the (c++9Cool states that a constructor cannot be virtual
or static Smile. Your solution therefore promises much.

Quote:
However, if you dont have access to the base class code then you're probably
stuck.

Yes, true.

Regards,

W


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Bob Hairgrove
Guest





PostPosted: Wed Nov 30, 2005 1:45 am    Post subject: Re: Two base classes and one derived class Reply with quote

[top-posting corrected]

On 29 Nov 2005 09:10:09 -0500, "news" <news (AT) news (DOT) astraweb.com> wrote:

Quote:
"sriya" <ksadiraju (AT) gmail (DOT) com> wrote in message
news:1133251322.810892.313670 (AT) g14g2000cwa (DOT) googlegroups.com...
hi all,

Is there any method using which I can avoid the constructors of the
base classes to be called when the object of the derived class is
created??

I have two base classes and one derived class. Now, i want to avoid the
constructor in either of the base class or in both. can I do this??

pls help me.

Hi there!

I'm no expert, but as I understand things, you can make the constructors for
the base classes 'virtual', then override those functions in the derived
class, making them empty if you wish.

No -- you can only declare the destructor (or any regular function) in
the base class virtual. Constructors cannot be declared virtual, nor
can they be overriden.

The solution is to provide appropriate constructors for the base class
which can be used in the derived class constructor's member
initialization list. If default construction is not an option, the
default constructor can be declared private and not implemented. This
will effectively force all inherited classes to explicitly initialize
the base class through the derived constructor's member initialization
list and provide the appropriate arguments.

Quote:
However, if you dont have access to the base class code then you're probably
stuck.

True. In a well-designed third-party library, though, this is rarely a
problem since in most cases there are appropriate constructors from
which to choose.

--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ulrich Eckhardt
Guest





PostPosted: Wed Nov 30, 2005 1:47 am    Post subject: Re: Two base classes and one derived class Reply with quote

news wrote:
Quote:
I'm no expert, but as I understand things, you can make the constructors
for the base classes 'virtual', then override those functions in the
derived class, making them empty if you wish.

This doesn't work: 'virtual' means dynamic dispatch at runtime. In other
words, the actual function that is called depends on the real type (the
so-called 'dynamic type') of the object, even if a pointer or reference to
the baseclass (the so-called 'static type') is used for calling it.

Now, getting back to constructors, this doesn't work because there is no
object yet, so you can't decide which function to call based on the dynamic
type.
Just to be complete, you also can't use a virtual function to initialise the
baseclass object. The idea would be that the ctor of the baseclass calls
this init function and a derived class is able to override this. This
doesn't work due to how C++ constructs objects: at first, it sets the
dynamic type of the object to 'baseclass', then invokes the ctor of the
baseclass. Therefore, inside the baseclass ctor its dynamic type is
baseclass, even while creating an object of a derived class. When the
baseclass ctor has finished, the dynamic type changes to 'derived' and the
ctor of the derived class is invoked.
The same applies in opposite order to the dtor, which is why a cleanup
function called by the dtor of the baseclass doesn't work as many would
expect to at first sight.

Now, the dtor is a different case though: you already have an existing
object and the dtor can therefore be virtual. In fact it has to be virtual
when you plan to invoke it on an object who's static type is 'baseclass',
otherwise you invoke undefined behaviour.

Uli


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Carl Barron
Guest





PostPosted: Wed Nov 30, 2005 10:56 am    Post subject: Re: Two base classes and one derived class Reply with quote

In article <1133251322.810892.313670 (AT) g14g2000cwa (DOT) googlegroups.com>,
sriya <ksadiraju (AT) gmail (DOT) com> wrote:

Quote:
hi all,

Is there any method using which I can avoid the constructors of the
base classes to be called when the object of the derived class is
created??

I have two base classes and one derived class. Now, i want to avoid the
constructor in either of the base class or in both. can I do this??

pls help me.


not in one class but you might be able to create a set of templates

to write code once if you do not use the base class functions explicitly
in your derived 'class' You are going to know what to construct at
compile time in any event.

// the base classes you have
class base1{};
class base2{};

template <bool B1,bool B2> struct inbetween:base1,base2{};
template <> struct inbetween<false,false>{};
template <> struct inbetween<true,false>:base1{};
template <> strict inbetween<false,true>:base2{};

template <bool Base1,bool Base2>
class derived_class:public in_between<Base1,Base2>
{
};

using empty as a base class to replace the base class[es] you wish
to not construct.

the first is easier to construct what ever is in common with the
various template instances, in inbetween used. in that instance.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
news
Guest





PostPosted: Wed Nov 30, 2005 11:48 am    Post subject: Re: Two base classes and one derived class Reply with quote

hehe, yeah turns out I'm an idiot. Oops! Thanks for the explanation though
Uli, very informative. Not only do I now know that you can't have virtual
constructors, I also know why!

=me=

ps. don't suppose anyone fancies a gander at my templates question "Template
class specialisation"? ;)

"Ulrich Eckhardt" <eckhardt (AT) satorlaser (DOT) com> wrote

Quote:
news wrote:
I'm no expert, but as I understand things, you can make the constructors
for the base classes 'virtual', then override those functions in the
derived class, making them empty if you wish.

This doesn't work: 'virtual' means dynamic dispatch at runtime. In other
words, the actual function that is called depends on the real type (the
so-called 'dynamic type') of the object, even if a pointer or reference to
the baseclass (the so-called 'static type') is used for calling it.

Now, getting back to constructors, this doesn't work because there is no
object yet, so you can't decide which function to call based on the
dynamic
type.
Just to be complete, you also can't use a virtual function to initialise
the
baseclass object. The idea would be that the ctor of the baseclass calls
this init function and a derived class is able to override this. This
doesn't work due to how C++ constructs objects: at first, it sets the
dynamic type of the object to 'baseclass', then invokes the ctor of the
baseclass. Therefore, inside the baseclass ctor its dynamic type is
baseclass, even while creating an object of a derived class. When the
baseclass ctor has finished, the dynamic type changes to 'derived' and the
ctor of the derived class is invoked.
The same applies in opposite order to the dtor, which is why a cleanup
function called by the dtor of the baseclass doesn't work as many would
expect to at first sight.

Now, the dtor is a different case though: you already have an existing
object and the dtor can therefore be virtual. In fact it has to be virtual
when you plan to invoke it on an object who's static type is 'baseclass',
otherwise you invoke undefined behaviour.

Uli

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.