Solved

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

Posted on 1998-03-13
10
184 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:rock1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 1183411
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
 
LVL 32

Expert Comment

by:jhance
ID: 1183412
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
 
LVL 22

Accepted Solution

by:
nietod earned 10 total points
ID: 1183413
answer coming.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:jhance
ID: 1183414
Well, where is the answer?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183415
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
 
LVL 22

Expert Comment

by:nietod
ID: 1183416
Gosh, you are impatient.  I'm a programmer!  I type with 3 fingers, two on one hand and one on the other.  
0
 

Author Comment

by:rock1
ID: 1183417
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
 
LVL 22

Expert Comment

by:nietod
ID: 1183418
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
 
LVL 22

Expert Comment

by:nietod
ID: 1183419
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
 

Author Comment

by:rock1
ID: 1183420
Thanks I appreciate your help on this...

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question