-
August 12th, 2012, 05:41 AM
#1
Do I need a default constructor?
Code:
class CObjects
{
public:
// default constructor
CObjects() : mesh(0) { }
CObjects(Mesh *m) { mesh = m; }
~CObjects() { }
};
I don't understand if I include the default constructor but calling the wrong constructor accidentally, the program will collapse. But it won't compile if I don't include the default one. Could anyone shed some lights on this?
Thanks
Jack
-
August 12th, 2012, 06:37 AM
#2
Re: Do I need a default constructor?
It depends on the way you want to use your class. e.g. if you want to add objects of your class into a standard container then it is needed.
I include the default constructor but calling the wrong constructor accidentally, the program will collapse.
That has nothing to do with the existence of a default constructor. It must be a bug somewhere else. e.g. in your example the default constructor initializes ( the non existent ) member mesh to 0 but you're probably not checking for 0 somewhere else.
Kurt
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|