Quote:Original post by Swarmer
myClass theObject = new myClass(constructor_params);
Illegal.
Quote:myClass* theObject = new myClass(constructor_params);
Dynamically allocates a myClass object. Stores the address of that object in a pointer called theObject.
Quote:myClass theObject(constructor_params);
Creates a locally scoped myClass object called theObject.
Quote:myClass* theObject(constructor_params);
Illegal.
Quote:myClass theObject = myClass(constructor_params);
Identical in effect to 2.
Quote:myClass* theObject = myClass(constructor_params);
Illegal.
Quote:4+?
Any other ways to do it?
Yes, one:
myClass theObject = constructor_param
Really, though, that's just a syntax thing. There are two ways to allocate objects: statically, as in 2, or dynamically, as in 1.5. That is the major distinction you need to learn.
This is what my understanding is:
Note that X.5 all ask the same question: make a pointer to the thing or not?
My understanding is that making big classes as pointers is faster? Because when you pass them as arguments you can just pass the pointer. Also, what counts as a "big" class?) Or is it better to not use pointers and pass by reference? Am I misunderstanding some big concepts? Also, arrows are cool (->).
Re: 1:
I know that with the new keyword, I also have to remember to use delete. (I don't have to (with a few exceptions, I assume) explicitly call destructors (with or without new), do I?) I see new used a lot with pointers. Is there something to that?
I know that new dynamically allocates memory. Does that mean that I should use it for classes that have the potential to use more memory? Should that property be obvious to me when I see it?
Re: 2:
How do I know when I can do this without problems?
Re: 3:
3 and 2 are similar; I read somewhere that 3 was a safer way to do it, as sometimes 2 could be initialized incorrectly if the constructor parameter was another class' constructor or something.
----
So when should each way be used? Should I be mixing them up? I know I asked a lot of questions, but any help is appreciated. Thanks!