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?

1 Answer 1


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.

  • 1
    So, by making it private, the user trying to use the default constructor will get an error at compile time? Mar 26, 2014 at 18:07
  • @user2738698 Correct.
    – Doval
    Mar 26, 2014 at 18:10
  • 8
    If you can use C++11, then explicitly mark it as deleted: CSV_Entry() = delete;.
    – bstamour
    Mar 26, 2014 at 18:29
  • 13
    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. Mar 26, 2014 at 20:39
  • 7
    @FredLarson Explicitly deleting it expresses the intent of deleting it so no one thinks it was a mistake.
    – Darkhogg
    Mar 26, 2014 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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