Default constructors - I don't want em!

Jul 26, 2008 at 5:17pm
So, what do I do if I want an object to not have an available default constructor? Many of the objects I want to make should require that pointers to other objects be passed into it during creation. What is the best way of ensuring this?
Jul 26, 2008 at 7:00pm
Make it private.
Jul 26, 2008 at 8:47pm
Yes, declare it as private. you don't have to provide a body either, just the declaration will stop the compiler doing one for you.
Jul 27, 2008 at 5:21pm
When you provide your own constructor the default constructor is no longer created. This seems to be what you want. Making a constructor protected or private makes sense only in a few cases, e.g. when creating signletons.

Edit:
Of course, when you provide your own "normal" constructor(s), the default copy-constructor is still generated. You have to overwrite this one seperately, especially when dealing with pointers (depending on whether they represent associations or aggregations/compositions) one should not forget to do so.
Last edited on Jul 27, 2008 at 5:24pm
Topic archived. No new replies allowed.