Hi! A petty doubt before geeks!!

N

nikki

Hi all!

I have alittle question.
What is the code like to make sure that if we create an object of a
class dynamically , it is allocated memory but not when a static object
of the same class????
 
N

Noah Roberts

nikki said:
Hi all!

I have alittle question.
What is the code like to make sure that if we create an object of a
class dynamically , it is allocated memory but not when a static object
of the same class????

Privatize the constructor and create a factory function.
 
V

Victor Bazarov

nikki said:
What is the code like to make sure that if we create an object of a
class dynamically , it is allocated memory but not when a static
object of the same class????

Not sure what you mean here, but if you want to require the objects
of your class always created dynamically, you should provide a factory
method (which would return a pointer) and make constructors private.

I hope it's OK that I responded. I am not a geek, I just hang out
here.

V
 
M

medium.salsa

Victor said:
Not sure what you mean here, but if you want to require the objects
of your class always created dynamically, you should provide a factory
method (which would return a pointer) and make constructors private.

I hope it's OK that I responded. I am not a geek, I just hang out
here.

No, you're not a geek! You're even worse! You're the C++ language
police! Aaaaah!
 
A

Alf P. Steinbach

* nikki:
Hi all!

I have alittle question.
What is the code like to make sure that if we create an object of a
class dynamically , it is allocated memory but not when a static object
of the same class????

If you mean, "how do I ensure that objects of my class are dynamically
allocated?":

Make the destructor 'private' or 'protected', and provide an accessible
function that calls 'delete'. This can be an ordinary member function
(simple) or e.g. a friend template function (general).

This way you don't have to provide one factory function per constructor,
which is less work and better for maintenance (adding, removing or
changing constructors requires no parallell maintenance of factory
functions, and deriving from the class can be easier).
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top