Solved

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

Posted on 1998-03-13
10
178 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

777 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