Solved

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

Posted on 1998-03-13
10
173 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
  • 5
  • 3
  • 2
10 Comments
 
LVL 32

Expert Comment

by:jhance
Comment Utility
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
Comment Utility
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
Comment Utility
answer coming.
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
Well, where is the answer?
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks I appreciate your help on this...

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now