| cantog (13) | |
|
Hi, I'm reading som examples of class implementations, and hope that someone can help me out with understanding the difference between: Classname classobject("Value")and Classname *classobject = new Classname("Value")Why do I want to do this? What are the pros and cons? Thanks in advance | |
|
Last edited on
|
|
| helios (9442) | |
|
The former constructs the object in the stack. It's automatically destructed as soon as it goes out of scope. The latter constructs it in the heap. It's not automatically destructed. | |
|
Last edited on
|
|
| cantog (13) | |
|
Thank you for the reply. I understand some of what you are saying, but can it be said more "novice-ish"? When do I want to choose the former or latter? What is best for speed or memory usage? | |
|
|
|
| Bazzy (6275) | |
|
The former way is just an object stored using the automatic memory, the latter can be dynamically allocated as an array of object and/or used to have polymorphism which is a powerful tool http://www.cplusplus.com/doc/tutorial/dynamic/ http://www.cplusplus.com/doc/tutorial/polymorphism/ | |
|
|
|
| tition (676) | |||||
A word of advice from a beginner like you: it is more safe to use Classname Classobject("Value"); because, as helios explained, the program calls the object destructor automatically, which saves you a lot of coding, and is less error-prone. Another advantage:
while you don't have to worry for such things when using the first style:
| |||||
|
Last edited on
|
|||||
| helios (9442) | |
Just a couple of corrections:delete Classobject;This doesn't cause the program to crash, but it does leave unfreed memory, so it causes a memory leak. Classname Classobject[10];This works only if Classname has a constructor with no parameters. | |
|
|
|
| tition (676) | |
| corrected what I wrote in accordance :) | |
|
|
|
| helios (9442) | |
Classname Classobject[10]();Now you've broken it ("error: declaration of `Classobject' as array of functions"). | |
|
|
|
| tition (676) | |
you are completely right... now a beginners question: why is Classname* Classobject= new Classname[10](); correct, and Classname Classobject[10](); incorrect?That is, I would like to have a "concept explanation" rather than "this is so because this is so". *Corrected the syntax in the previous post to be proper | |
|
Last edited on
|
|
| helios (9442) | |
| Because new Classname[10](); is unambiguously an allocation, but Classname Classobject[10](); could be an array declaration as well as a function declaration. | |
|
|
|
| Disch (7378) | |
They're both incorrect, kind of.... sort off.... not really.Classname* Classobject= new Classname[10](); compiles -- though I find that a little surprising myself. In any case it's redundant and unnecessary. As soon as you try to call anything but the default ctor with those parenthesis, it will error on you.Classname* Classobject = new Classname[10]; <---- typical.EDIT -- or listen to helios. His answer makes more sense. XD | |
|
Last edited on
|
|
| Hammurabi (399) | |
|
Note that there's no point in the empty parens after the new form. You don't need them and you're not allowed to pass any args anyway. This Classname Classobject[10]();is interpretted something like this Classname (*Classobject)();whereas a proper array of functions might be Classname (*Classobject[10]) (); | |
|
Last edited on
|
|
| tition (676) | |
|
Ok I get it... so you can only allocate arrays whenever you have a constructor that gets no arguments. Otherwise you can go outside and play hide... :) it's never late to learn something new! | |
|
Last edited on
|
|