Using the new operator and calling a class constructor on the same line

Today I stumbled upon something new and I was wondering if there are any
problems with doing this..
Assume I have a class called temp....

temp *PtrToClassTemp;
int input;

cin >> input;

PtrToClassTemp = new PtrToClassTemp(input);

this will now call the constructor of class temp passing input as an
argument..
I am used to seeing the use of new as follows

variable = new type[size];
I knew I could always allocate class objects with new, but passing a value
to the constructor where the size of the memory to be allocated is odd to
me.

Is there any problems with doing this?  Anything to be concerned about (is
this a bad technique)?  Should I just stay away from doing this?  I posted this to the newsgroups and I got a mixed response of answers.  Some said this was illegal and some said it was ok.  What is the final answer...


Thanks
rock1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jhanceCommented:
I'm surprised that you got mixed reactions from the news groups.  What you have done above is a perfectly legal and proper way to create an instance of a class.  The only thing to remember is to call delete on the object when you are done with it.
0
jhanceCommented:
BTW - If it were illegal it's very likely your compiler would flag it for you unless your compiler is "brain-dead".  I'd also suggest some validation of input either before calling your constructor, in the constructor, or both, just in case.
0
nietodCommented:
answer coming.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

jhanceCommented:
Well, where is the answer?
0
nietodCommented:
Everything jhance said is true, but I think he missed what your exact confussion is.

you are comparing

Class *Ptr1  = new Class(Count);
Class *Ptr1  = new Class[Count];

these look very similar, but are VERY different.

The first case uses the new operator (as opposed to "new []").  It creates one object and it initalizes (constructs) the object using the parameter or parameters passed in the parethesis.  If there were multiple parameters, like

Class::Class(int i, const char * ChrPtr) {};

You could do.

Class *Ptr1  = new Class(Count,"this has two parameters");

The second case is totalyl different.  It is constructing an array of objects.  The number of items in the array are specified by the "count" variable.  Each object in the array is default constructed.  That means that it must have a default constructor, you cannot specify parameters to the constructors or objects in an array.  (A safety mechanism).

Although the two lines look the same, the variable "count" is being used for differnet purposes and the compiler performs very different actions.

Make sense?
0
nietodCommented:
Gosh, you are impatient.  I'm a programmer!  I type with 3 fingers, two on one hand and one on the other.  
0
rock1Author Commented:
Jhance, Nietod:
Thank you for your help I appreciate it.  About the newsgroup most people replied that is was illegal according to the ansi draft....  I tried it in my compiler and it worked so I was confused on why it could be illegal.  I graded this question, because the answer was given to the original question...  However, another question arose out of this...  Please see below...
________END OF THANK YOU___________


Thanks It makes perfect sense.  So to sum this up, it is perfectly legal to call a constructor from the new line...

The solution to this problem brought up an interesting question are all objects of the same class the same size?  Lets assume that the class has three pointers to characters, and in the constructor (for one class object) allocates those three pointers each to point to 128 bytes,  While when I create another object of the same class and allocate the three pointers to point to 3 bytes..  Are these classes the same size?  does sizeof(object1) == sizeof(object2)?  I would be tempted to say yes, but somehow I think that sizeof(object1) and sizeof(object2) would be the same because I am comparing the pointer to the object not the actual sizeof the object?  Confusing, maybe even a little bit to me.

Also, when deleting an object I can use just regular delete object, but an array of objects delete[] object?  Correct?


Thanks

0
nietodCommented:
Yes, all object of a particular class are the same size.  This is critical not only to programmers using the langauge,  but to the implimnetatation of the langage as well!  

Imagine, If you had an array of objects and the objects could be different sizes it would be difficult to find any item other than the first item.

FYI The size of an object or a type can be found using "sizeof()"  This can come in handy sometimes.  

Note that if an object contains a pointer its size does not depend on the data that the pointer points to.  Pointers have a fixed size regardless of how big the data is that they point.  That is the main reason we use pointers.  

0
nietodCommented:
BTW As far as it being illegal in the ANSII draft, that is absurd.  It has been legal since the early days of C++.  It is a core feature of the language.
0
rock1Author Commented:
Thanks I appreciate your help on this...

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.