Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Inheritance query

Reply
Thread Tools

Inheritance query

 
 
Mahesh
Guest
Posts: n/a
 
      03-11-2005
Hello,
I am having a query about inheritance.
I have a virtual base class that is derived by to other classes that
are hirarchially at the same level. But I still need to ensure that
only a single instance of the base class is created.
Is this even possible? If yes then how do I go about doing this?
Thanks,
Mahesh

 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      03-11-2005

"Mahesh" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hello,
> I am having a query about inheritance.
> I have a virtual base class that is derived by to other classes that
> are hirarchially at the same level. But I still need to ensure that
> only a single instance of the base class is created.
> Is this even possible? If yes then how do I go about doing this?
> Thanks,
> Mahesh
>


I'm not sure what you mean by a "virtual base class". How are your derived
classes derived? Are you using virtual inheritance? Or, are you actually
using public inheritance, and you really meant either just "base class", or
perhaps "abstract base class"?

If you're using public inheritance, then your derived class looks something
like this:

class MyDerivedClass : public MyBaseClass

In that case, every instance of a derived class must by definition include
everything that the base class defines. There is no base class instance to
which derived class instances refer to. An instance of the derived class IS
a base class object, PLUS anything additional that the derived class
defines.

In this case, if you want a single instance of some object to exist that all
the other objects can refer to, then what you need is containment, not
inheritance. For instance, you can declare a member of a common base class
as a pointer to a singleton class that all objects (whether of the base or
derived classes) can refer to.

If, on the other hand, you're using virtual inheritance, then you have
something like this:

class MyDerivedClass : virtual MyBaseClass

In that case, you're into territory I've never explored. Let others know if
that's what you mean, and they (or a good book) can give you details on what
happens in that case.

-Howard



 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-11-2005
Mahesh wrote:
> I am having a query about inheritance.
> I have a virtual base class that is derived by to other classes that
> are hirarchially at the same level. But I still need to ensure that
> only a single instance of the base class is created.
> Is this even possible? If yes then how do I go about doing this?


You cannot ensure that. IOW, it's not possible. A derived class will
have an instance of the base class in it. You cannot make derived
class instances share an instance of any base class.

That said, you can make objects share another object that would exist
as the only instance of its type in the program. That mechanism is
often called "a singleton" and you will need to share the singleton
by using a pointer or a reference to it in the classes who need it:

class MySingleton { }; // read about singletons and how to create them

class MyBase {
MySingleton *singleton;
public:
// whatever
};

class MyDerived_1 : public MyBase {
...
};

class MyDerived_2 : public MyBase {
...
};

Now, all the functionality and the state you need to be represented by
the singleton should be implemented in 'MySingleton' class and proxied
in 'MyDerived_X' classes by the 'MyBase' class, which itself does not
have any data except a pointer to your MySingleton object.

V
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-11-2005
Howard wrote:
> "Mahesh" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>
>>Hello,
>>I am having a query about inheritance.
>>I have a virtual base class that is derived by to other classes that
>>are hirarchially at the same level. But I still need to ensure that
>>only a single instance of the base class is created.
>>Is this even possible? If yes then how do I go about doing this?
>>Thanks,
>>Mahesh
>>

>
>
> I'm not sure what you mean by a "virtual base class". How are your derived
> classes derived? Are you using virtual inheritance? [...]


The term "virtual base class" is used to describe the base class from
which the derived classes inherit virtually, yes.

When a class is defined, there is nothing that can be said about it except
that it's abstract. It doesn't become "base" until some other class is
derived from it. There are two ways to derive: normal and virtual. Both
can be further complicated by access specifiers, but that doesn't change
how many instances there are and how the class instance[s] is[are] created
and initialised in the derived class[es]' instances.

> In that case, you're into territory I've never explored. [...]


Then now is your chance. Read before you write.
 
Reply With Quote
 
Mahesh
Guest
Posts: n/a
 
      03-11-2005
Howard,
I am using public inheritance. the base class is not virtual... I am
mistaken. ALso like you said I want an instance of the base class that
instances of the child classes can look at.
I also want it such that when the child classes are instantiated, only
one instance of the base class is created.

 
Reply With Quote
 
Mahesh
Guest
Posts: n/a
 
      03-11-2005
Thanks guys....

 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      03-11-2005

"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:yYkYd.52248$(E-Mail Removed)01.us.t o.verio.net...
> Howard wrote:
>> "Mahesh" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) oups.com...
>>
>>>Hello,
>>>I am having a query about inheritance.
>>>I have a virtual base class that is derived by to other classes that
>>>are hirarchially at the same level. But I still need to ensure that
>>>only a single instance of the base class is created.
>>>Is this even possible? If yes then how do I go about doing this?
>>>Thanks,
>>>Mahesh
>>>

>>
>>
>> I'm not sure what you mean by a "virtual base class". How are your
>> derived classes derived? Are you using virtual inheritance? [...]

>
> The term "virtual base class" is used to describe the base class from
> which the derived classes inherit virtually, yes.
>
> When a class is defined, there is nothing that can be said about it except
> that it's abstract. It doesn't become "base" until some other class is
> derived from it. There are two ways to derive: normal and virtual. Both
> can be further complicated by access specifiers, but that doesn't change
> how many instances there are and how the class instance[s] is[are] created
> and initialised in the derived class[es]' instances.
>
>> In that case, you're into territory I've never explored. [...]

>
> Then now is your chance. Read before you write.


Interesting, thanks. I guess I didn't remember that term because when I hit
the part about multiple inheritance and virtual derivation, my eyes kind of
glazed over and I didn't recover consciousness until I'd passed on to other
subjects.

Thanks for the info, and sorry for the sidetrack...

-Howard



 
Reply With Quote
 
Prog37
Guest
Posts: n/a
 
      03-12-2005
Mahesh wrote:
> Howard,
> I am using public inheritance. the base class is not virtual... I am
> mistaken. ALso like you said I want an instance of the base class that
> instances of the child classes can look at.
> I also want it such that when the child classes are instantiated, only
> one instance of the base class is created.
>

It sounds like you want to have a singleton of the base class
but allowing one and only instance from a set of derived classes.
Is that correct? Or are you asking for multiple instances of the derived
class to share a single instance of the base class.

The former is usually called the singleton pattern. The latter is not
even polymorphic and the derived classes should use the has-a pattern.

Assuming its the first case which is more common and makes more sense,
one way to accomplish your goal is to override the new operator in the
base class. The overrided new operator would maintain a counter that is
initialized to zero, it would only allocate memory when the counter is
zero. On every call after the first it would keep return NULL. This
would guarantee that only the first instance could be created. All
attempts after the first would fail.
 
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
C++ Struct inheritance against class inheritance johnsonlau C++ 1 07-21-2008 04:58 PM
Interface inheritance vs Implementation inheritance. Daniel Pitts Java 27 02-27-2008 01:37 AM
Private Inheritance and Publice Inheritance karthikbalaguru C++ 9 09-10-2007 01:05 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM



Advertisments