943,892 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 706
  • C++ RSS
Aug 6th, 2009
0

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

Expand Post »
Please explain.
Does it automatically include the default constructor?
Similar Threads
Reputation Points: 47
Solved Threads: 2
Posting Pro in Training
lotrsimp12345 is offline Offline
413 posts
since Jun 2009
Aug 6th, 2009
0

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

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
c++ Syntax (Toggle Plain Text)
  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
Reputation Points: 76
Solved Threads: 15
Junior Poster
jesseb07 is offline Offline
111 posts
since Dec 2006
Aug 7th, 2009
0

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

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
Reputation Points: 88
Solved Threads: 27
Junior Poster
chiwawa10 is offline Offline
156 posts
since Jul 2005
Aug 7th, 2009
0

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

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
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 7th, 2009
0

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

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?
Reputation Points: 47
Solved Threads: 2
Posting Pro in Training
lotrsimp12345 is offline Offline
413 posts
since Jun 2009
Aug 7th, 2009
0

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

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.
Reputation Points: 165
Solved Threads: 59
Posting Pro in Training
DangerDev is offline Offline
485 posts
since Jan 2008
Aug 7th, 2009
1

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

@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.
cpp Syntax (Toggle Plain Text)
  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.

cpp Syntax (Toggle Plain Text)
  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.
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Aug 7th, 2009
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
packrisamy is offline Offline
3 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Processing Numbers from a file
Next Thread in C++ Forum Timeline: Problems with function parameter...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC