![]() |
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???? |
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. |
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 |
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! |
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.