2

So I know that after you write a constructor in a class the default constructor goes away, so you have to start initializing every object. However, is there a way to write a default constructor so that you don't have to do this?

Thanks.

5
  • 6
    = dafault.... Commented Jul 9, 2013 at 14:59
  • 4
    @LuchianGrigore *default Commented Jul 9, 2013 at 15:03
  • 2
    What dafault did I just read?!? Commented Jul 9, 2013 at 15:49
  • @LuchianGrigore must be from Chicago! Commented Jul 9, 2013 at 15:51
  • Pedantry: The compiler will not generate a trivial default constructor if a class has any user-defined constructors. Commented Jul 9, 2013 at 15:52

4 Answers 4

8

Before C++11

class MyClass
{
public:
    MyClass(int x, int y) {}
    MyClass() {}
};

or in C++11

class MyClass
{
public:
    MyClass(int x, int y) {}
    MyClass() = default;
};

You can write as many constructors as you like, but avoid making your class confusing to use.

Sign up to request clarification or add additional context in comments.

Comments

2

The implicit default constructor goes away. You could still write another default constructor, one that doesn't take any arguments or that takes only args with default values; it's just not done for you by the compiler.

There's no reason you couldn't just write an empty constructor for a class A and take the default for each values. Perhaps not the best idea, but it can be done.

A() {/* empty */}

As noted in the comment if you're using c++11 you could also use the new default keyword to give you what the compiler would have if it did the "default"

A() = default; 

Comments

1
class A
{
public:
    // Or use A()=default for C++11
    A(){}

    A(int v):m_value(v){}

private:
    int m_value;
};

int main()
{
    A a; // Without default ctor ==>> error C2512: 'A' : no appropriate default constructor available

    return 0;
}

Comments

1
struct S {
    S(int) {} // non-default constructor that suppresses the implicit default constructor

    S() = default; // bring the default constructor back
};

Note that there are two senses in which 'default' is used. There's a default constructor in the sense that it has a signature such that it will get used in "Default construction".

Secondly, there's 'default' in the sense that the implementation will match what the compiler automatically generates for an implicitly declared constructor.

You can make your class default constructible (default in the first sense) by giving your constructor default arguments.

struct S {
    S(int = 10) {}
};

While = default is used to explicitly ask for a default implementation (default in the second sense).

Assuming your compiler implements = default using that is usually superior to doing S() {} for a number of reasons. = default in the class definition produces a non-user-provided constructor which has several effects.

  • user-provided special member functions are never trivial
  • a user-provided constructor disqualifies a type from being an aggregate.
  • value initialization includes zero-initialization for types with a non-user-provided default constructor
  • default initialization of const objects is disallowed for types without a user-provided default constructor

Occasionally one might want a user-provided function due to one of these effects. It's still possible to use = default by using it outside the class definition:

struct S {
    S();
};

S::S() = default;

The above provides the compiler's default implementation but still counts as a user-provided default constructor. Additionally, this can be used to maintain ABI stability; at a later point = default can be replaced with another definition without necessarily requiring a recompile of all code constructing S objects, whereas it would be required with an inline defaulted default constructor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.