Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Hi! A petty doubt before geeks!! (http://www.velocityreviews.com/forums/t459875-hi-a-petty-doubt-before-geeks.html)

nikki 01-17-2007 04:32 PM

Hi! A petty doubt before geeks!!
 
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????


Noah Roberts 01-17-2007 04:42 PM

Re: Hi! A petty doubt before geeks!!
 

nikki wrote:
> 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.


Victor Bazarov 01-17-2007 06:58 PM

Re: Hi! A petty doubt before geeks!!
 
nikki wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



medium.salsa@gmail.com 01-18-2007 02:25 AM

Re: Hi! A petty doubt before geeks!!
 
Victor Bazarov wrote:
> nikki wrote:
> > 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.


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


Alf P. Steinbach 01-18-2007 03:16 AM

Re: Hi! A petty doubt before geeks!!
 
* 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).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


All times are GMT. The time now is 06:00 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57