Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Hi! A petty doubt before geeks!!

Reply
Thread Tools

Hi! A petty doubt before geeks!!

 
 
nikki
Guest
Posts: n/a
 
      01-17-2007
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????

 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      01-17-2007

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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-17-2007
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


 
Reply With Quote
 
medium.salsa@gmail.com
Guest
Posts: n/a
 
      01-18-2007
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!

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-18-2007
* 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?
 
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
dotnet doubt can any body clarify my doubt challa462@gmail.com ASP .Net 0 08-22-2012 06:02 AM
doubt about doubt Bob Nelson C Programming 11 07-30-2006 08:17 PM
tom petty video George Computer Support 4 10-02-2005 11:31 AM
Before Sunset & Before Sunrise matt r DVD Video 2 11-25-2004 08:37 AM
Coding statements before declaration-a doubt on standard!!! s.subbarayan C Programming 2 09-10-2004 11:28 AM



Advertisments
 



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