| | |
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 |
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
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
if it's that you don't explicitly create your own, here's what happens
c++ Syntax (Toggle Plain Text)
#include <iostream> //feel free to give this a test to see for yourself, it should compile ok class Test { public: int x; }; int main() { Test testObject; std::cout << testObject.x << std::endl; //this will print the compiler default for an int return 0; }
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
Makes it easier on everyone: http://www.daniweb.com/forums/thread78223.html
AJAX, PHP, C#, C++, JAVA
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
(destructor - to prevent memory leak)
Regards,
Nick
•
•
Join Date: Jun 2009
Posts: 265
Reputation:
Solved Threads: 0
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?
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?
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.
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/
Indian Developer
http://falaque.wordpress.com/
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
@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.
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.
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.
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)
class Foo { public: int a; }
cpp Syntax (Toggle Plain Text)
class Foo { Bar b; }
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. ![]() |
Similar Threads
- Can we Call Constructor Explicitly?? (C++)
- Unable to use constructor paramaters (Java)
- Need for default constructor (Java)
- Delphi Onjects (Pascal and Delphi)
- the common usage of a copy constructor (C++)
- copy constructor and 2 args constructor help (C)
- Playing with RMI in Tiger (Java)
- Basic help with Java class (Java)
- after a very unproductive day! - code bugs (C++)
- Java.io help!!! (Java)
Other Threads in the C++ Forum
- Previous Thread: Processing Numbers from a file
- Next Thread: Problems with function parameter...
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






