473,385 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Inheritance query

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

Jul 23 '05 #1
7 1604

"Mahesh" <sr*************@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr 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

Jul 23 '05 #2
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
Jul 23 '05 #3
Howard wrote:
"Mahesh" <sr*************@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr 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.
Jul 23 '05 #4
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.

Jul 23 '05 #5
Thanks guys....

Jul 23 '05 #6

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:yY*******************@newsread1.mlpsca01.us.t o.verio.net...
Howard wrote:
"Mahesh" <sr*************@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr 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

Jul 23 '05 #7
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.
Jul 23 '05 #8

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

Similar topics

6
by: Martin | last post by:
Hi, Suppose I have 3 interfaces : IStats IEngine ICreateCar each derives from IUnknown
5
by: ma740988 | last post by:
Prefer composition to inheritance (can't recall which text I stole that line from) is one of the fundamental tenets thats engrained in my mind. Having said that inheritance requires careful...
0
by: apb18 | last post by:
A bit of query plan strangeness. Suppose you have an inheritance tree such that the columns 'ID' and 'field' appear in the top level table, call that table XXX. tables YYY and ZZZ both inherit...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
12
by: Steve Jorgensen | last post by:
The classing Visual Basic and VBA support for polymorphism, let's face it, is a bit on the weak side, and built-in support for inheritance is non-existent. This little essay is about some patterns...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
4
by: John Holmes | last post by:
With ASP we have setup a pretty good method for maintaining a consistent look and feel with minimal effort. The method has it's pros and cons, but the maintenance is minimal. The method consists of...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
2
by: traineeirishprogrammer | last post by:
I am starting to develop applications in the form of 3 tier really (4 tier) architecture at the moment. I am still learning and at the moment I am developing a simple guest book. With the power...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.