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

Posted on 1998-03-13
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
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

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...

Question by:rock1
  • 5
  • 3
  • 2
LVL 32

Expert Comment

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.
LVL 32

Expert Comment

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.
LVL 22

Accepted Solution

nietod earned 10 total points
ID: 1183413
answer coming.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

LVL 32

Expert Comment

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

Expert Comment

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?
LVL 22

Expert Comment

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

Author Comment

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?


LVL 22

Expert Comment

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.  

LVL 22

Expert Comment

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.

Author Comment

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


Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
multi threaded debug dll option in visual studio 2 120
Cross compile release version of c++ program for linux 2 136
VS2015 Redefinition errors 4 68
How do i run a c++ file? 15 46
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

808 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