how to make sure only one instance of a class is created (dynamically)?

G

Guest

I have a series of objects of class A, some of this objects (depending on
the program execution) will need to create an object of class B (but only
one shared instance of B is needed). How would you implement this? If I had
a static pointer to B as a member of A, how to check if it is already
allocated? When to release its memory?
 
D

Daniel T.

I have a series of objects of class A, some of this objects (depending on
the program execution) will need to create an object of class B (but only
one shared instance of B is needed). How would you implement this? If I had
a static pointer to B as a member of A, how to check if it is already
allocated? When to release its memory?

You need something that monitors the lifetime of the B object. All the A
objects go to the monitor to get a B object rather than them making a B
object independently. Depending on the situation, the monitor can be (a)
static functions in the A class, (b) static functions in the B class, or
(c) an object that is shared by all A objects. (There may be other ways
to do it as well.)

Why is it an error to have more than one B object?
 
G

Guest

Why is it an error to have more than one B object?
Its not an error, simply a matter of wasting resources;)
 
Z

Zara

I have a series of objects of class A, some of this objects (depending on
the program execution) will need to create an object of class B (but only
one shared instance of B is needed). How would you implement this? If I had
a static pointer to B as a member of A, how to check if it is already
allocated? When to release its memory?

To create only one object, ever:

// Declaration, probably in header
class B {
friend class B& getOnlyB();
private:
/* ALL constructors here */
public:
/* Class interface here */
};


// Definition, probably in source, or you may inline it
class B& getOnlyB() {
static B b; /* Or however you construct it */
return b;
}


This will give you exactly ONE objetc of type B in all your
application. Now you may use it from your A instances

Zara
 
D

Daniel T.

I think maybe a static integer counter as a member of B will do the trick.

That, in and of itself, wouldn't do it. It would tell you how many B's
were created, but won't make sure there is only one.

I forgot to mention another method of doing it. Have a single global B
object.
 
D

Daniel T.

Its not an error, simply a matter of wasting resources;)

What resources would get wasted? If it isn't an error to have more than
one B object then the B class must not have much in the way of state...
 
N

Noah Roberts

Its not an error, simply a matter of wasting resources;)

That is usually not a good enough reason to enforce singleton
semantics.

If you need to though you can follow the singleton pattern. Look it up
on google or buy the GOF patterns book.
 
G

Guest

I forgot to mention another method of doing it. Have a single global B
object.

but I'm not sure I will need any B object - it has to be allocated
dynamically from the first A object that needs it, but I don't know whether
any A object is going to need it.
As far as resources are concerned - B has some member stuff that takes
memory.
 
D

Daniel T.

but I'm not sure I will need any B object - it has to be allocated
dynamically from the first A object that needs it, but I don't know
whether any A object is going to need it.

OK, then use one of the other suggestions I gave. Two people have
suggested the Singleton patter, which is to have static functions in the
B class monitor a B instance, but it may be the case that you need a B
object for all A's and maybe a different B object for all C's, in which
case putting the static member-functions somewhere else would be better,
or just making a global function.
As far as resources are concerned - B has some member stuff that
takes memory.

In that case, the behavior of your program should change based on wether
A's are sharing a single B or if each uses a different B. Is that the
case?
 
G

Guest

Daniel T. said:
OK, then use one of the other suggestions I gave. Two people have
suggested the Singleton patter, which is to have static functions in the
B class monitor a B instance, but it may be the case that you need a B
object for all A's and maybe a different B object for all C's, in which
case putting the static member-functions somewhere else would be better,
or just making a global function.


In that case, the behavior of your program should change based on wether
A's are sharing a single B or if each uses a different B. Is that the
case?

no, if I had to create many B objects - they would all be identical, so one
B object can be safely shared by A objects.
 
S

Steve Pope

but I'm not sure I will need any B object - it has to be allocated
dynamically from the first A object that needs it, but I don't know whether
any A object is going to need it.

Why does it have to be allocated from the A objects? Why can't
it be global?

In any event, if all member variables in B are static, it
doesn't really matter how many instances there are of it.

Steve
 
D

Daniel T.

no, if I had to create many B objects - they would all be identical,
so one B object can be safely shared by A objects.

So the B object never changes state? Just make all the member-functions
in B static then. What you really have is a collection of functions, not
an object.
 
A

Alan Johnson

I have a series of objects of class A, some of this objects (depending on
the program execution) will need to create an object of class B (but only
one shared instance of B is needed). How would you implement this? If I had
a static pointer to B as a member of A, how to check if it is already
allocated? When to release its memory?

Some have suggested using the Singleton design failure, but I don't
really think that is what you want. It sounds as if all your instances
of A need to (sometimes) share a single B. In this case, it is the
responsibility of the A class to manage a single B object. In the
Singleton pattern, it would be the responsibility of the B class to
manage a single instance of itself. Unless it really is the case that
there can only ever be at most one instance of B, then Singleton is
incorrect.

I recommend trying something like the following, if it is applicable to
your situation:

class A
{
static B & getB()
{
static B b ;
return b ;
}

// other stuff
} ;

Now, A would internally call getB() whenever it needed to access the
shared B object. The shared B object will be constructed the first
time getB() is called, and will never be constructed if getB() is never
called.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top