how does C++ know which constructor to call if you don't specify one?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2009
Posts: 265
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

how does C++ know which constructor to call if you don't specify one?

 
0
  #1
Aug 6th, 2009
Please explain.
Does it automatically include the default constructor?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 111
Reputation: jesseb07 is on a distinguished road 
Solved Threads: 15
jesseb07's Avatar
jesseb07 jesseb07 is offline Offline
Junior Poster

Re: how does C++ know which constructor to call if you don't specify one?

 
0
  #2
Aug 6th, 2009
do you mean if you don't explicitly create a constructor in your class, or are you referring to constructor overloading?

if it's that you don't explicitly create your own, here's what happens
  1.  
  2. #include <iostream> //feel free to give this a test to see for yourself, it should compile ok
  3. class Test
  4. {
  5. public:
  6. int x;
  7. };
  8.  
  9. int main()
  10. {
  11. Test testObject;
  12. std::cout << testObject.x << std::endl; //this will print the compiler default for an int
  13. return 0;
  14. }

the object will be created with the default values for all fields.

If you are asking about constructor overloading, it's just like function overloading: the compiler looks at the function signature to choose it for you.

for more info, maybe check out: http://www.cplusplus.com/doc/tutorial/classes/

Hope that's helpful

~J
Ps. 121

Makes it easier on everyone: http://www.daniweb.com/forums/thread78223.html

AJAX, PHP, C#, C++, JAVA
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 34
Reputation: chiwawa10 is an unknown quantity at this point 
Solved Threads: 5
chiwawa10's Avatar
chiwawa10 chiwawa10 is offline Offline
Light Poster

Re: how does C++ know which constructor to call if you don't specify one?

 
0
  #3
Aug 7th, 2009
If you do not explicitly define a constructor for your class, the program will call the default constructor. This is not recommended as your variables will contain random value. Constructor and destructor are highly recommended to carefully handle memory (constructor - to initialize values of variable before they are used)
(destructor - to prevent memory leak)


Regards,
Nick
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,279
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 157
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: how does C++ know which constructor to call if you don't specify one?

 
0
  #4
Aug 7th, 2009
If not defined the compiler create a :

default constructor like so someClass() { }, that does nothing
default destructor like so ~someClass() {}, that does nothing
default assignment operator
default copy constructor
default address operator
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 265
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

Re: how does C++ know which constructor to call if you don't specify one?

 
0
  #5
Aug 7th, 2009
basically i am trying to use constructors with operator overloading. I think i understand. You can call these constructors in other methods.

EX: maybe a set_x() and set_y() in the constructor which accepts two double values. The set_x(double x). You can call the input method and call this particular constructor.

I am getting really confused on when to use operator overloading.
Can someone explain?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 483
Reputation: DangerDev has a spectacular aura about DangerDev has a spectacular aura about 
Solved Threads: 58
DangerDev's Avatar
DangerDev DangerDev is offline Offline
Posting Pro in Training

Re: how does C++ know which constructor to call if you don't specify one?

 
0
  #6
Aug 7th, 2009
when you want a operator to show other then default behavior you should use operator overloading.
for example you have created a class ComplexNumber.
by default + operator doesn't add two ComplexNumber, here you need to overload + operator.
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: how does C++ know which constructor to call if you don't specify one?

 
1
  #7
Aug 7th, 2009
@firstPerson the default Constructor & Destructor are not always trivial at the Compiler level, to users it might be...

Hi lotrsimp12345,

This usually irritates the programmer that why the compiler emits a default constructor.

Let me explain you a bit detail of how C++ standard dictate about default constructor and its semantics.

if your class contains primitive types, then your default constructor would be trivial i.e.
  1. class Foo {
  2. public:
  3. int a;
  4. }
if we consider the Foo class its default constructor would be trivial i.e. doesn't modify the state and probably member "a" contains the garbage (or some 0xCCCCCCCC etc). Basically in C++ its always the programmer's responsiblity to intialize the values. This is what standard tells us. Logically if we look with the eye of programmer it seems irritating for us to explicitly intialize the value, yes it is, but we have some other facilities (default argument values) in the language to acheive the same.

  1. class Foo {
  2. Bar b;
  3. }

In above code Bar is the subobject which must be initialized before the intialization of Foo in this case compiler synthesized the constructor which in turns calls the default constructor for Bar. same goes for inheritance as well and obviously destructor would release the memory by calling the destructor of subobjects.

if C++ default constructor initializes the memory to "ZERO values" for primitive types then the name of default constructor should be "ZERO Constructor" rather than default constructor.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 2
Reputation: packrisamy is an unknown quantity at this point 
Solved Threads: 0
packrisamy packrisamy is offline Offline
Newbie Poster

Re: how does C++ know which constructor to call if you don't specify one?

 
0
  #8
Aug 7th, 2009
If there is no user defined constructor in C++ classes then the compiler will automatically invoke the default constructor by itself.
It will applicable only if you dont have any constructor as well you need to try to create an object.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC