CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2010
    Posts
    907

    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

  2. #2
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    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
  •  





Click Here to Expand Forum to Full Width

Featured