Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Specifically asking about the default constructor

Given that the constructor initializes all the data for an object, if I create a class that can't be used without proper initialization, is it not the case that the default constructor is useless? Consider:

// A class for handling lines in a CSV file
class CSV_Entry {
    unsigned num_entries;
    std::string string_version;
    std::vector<std::string> vector_version;
    CSV_Entry(const std::string& src_line);

    // returns a vector copy of the original entry
    std::vector<std::string> get_vector_snapshot();

int main( void ) {

    CSV_Entry example = CSV_Entry();
    std::vector<std::string> current_entry = example.get_vector_snapshot();


That variable current_entry is essentially useless no? If someone tries to process it later on, they would likely get errors; then they'd create code to handle such errors...

To mitigate such additional, unnecessary code: why not make the default constructor unusable? Like so,


CSV_Entry() {
    throw Verbose_Exception( "CSV_Entry: do not use the default constructor" );


PS: on a side note, if it is fine to just make the default constructor unusable, is it fine to put that throw in the header, since no other implementation details are revealed anyway?

share|improve this question

1 Answer 1

up vote 33 down vote accepted

Yes, it's fine (actually, it's good) to make the default constructor unusable if there's no sensible way to initialize the object without any arguments. But don't "disable" it by throwing an exception. Make it private instead. Ideally your interface won't contain any methods or constructors people "aren't supposed to" call.

share|improve this answer
So, by making it private, the user trying to use the default constructor will get an error at compile time? –  user2738698 Mar 26 '14 at 18:07
@user2738698 Correct. –  Doval Mar 26 '14 at 18:10
If you can use C++11, then explicitly mark it as deleted: CSV_Entry() = delete;. –  bstamour Mar 26 '14 at 18:29
Actually, isn't it even easier than this? If any non-default constructors are defined, the compiler will not implicitly create a default constructor. This class has a non-default constructor defined (which I'd recommend be explicit, BTW). So if you just don't define it, it won't exist. –  Fred Larson Mar 26 '14 at 20:39
@FredLarson Explicitly deleting it expresses the intent of deleting it so no one thinks it was a mistake. –  Darkhogg Mar 26 '14 at 20:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.