2

As far as I know, you get an implicit default constructor if you do not declare any constructor yourself. As a job interview question I was asked for a situation where you do not declare a constructor but still do not get an implicit default constructor either. So you end up with a class without any constructors. It is supposed to be code that compiles, so the answer is not having a member variable that does itself not provide a default constructor. Any ideas? Searching through stack overflow and various C++ sites did not reveal anything. Also, as a hint the interviewer said it does not have to do with inheritance.

6
  • Off the top of my head...make it static? I'm not sure. Commented Dec 17, 2012 at 20:57
  • AFAIK, you can't declare a class to be static in C++. Commented Dec 17, 2012 at 21:01
  • @chris it's entirely possible that's the case, I haven't done any c++ developing in quite a while. Commented Dec 17, 2012 at 21:06
  • @mikeTheLiar, Well, C# et al. complicate things in that regard, as it's perfectly valid and does what you expect there. Commented Dec 17, 2012 at 21:07
  • 1
    Defining a class that has a base or member without default constructor does not cause a compilation error. Attempting to use that class's default constructor would. Commented Dec 17, 2012 at 21:15

3 Answers 3

3

If my reading of the standard is correct, if the default constructor is not used, it won't get implicitly defined.

C++11 12.1.6:

A default constructor that is defaulted and not defined as deleted is implicitly defined when it is odr-used (3.2) to create an object of its class type (1.8) or when it is explicitly defaulted after its first declaration.

Also, a default constructor can be defined as "deleted", C++11 12.1.5:

A defaulted default constructor for class X is defined as deleted if:

  • X is a union-like class that has a variant member with a non-trivial default constructor,
  • any non-static data member with no brace-or-equal-initializer is of reference type,
  • any non-variant non-static data member of const-qualified type (or array thereof) with no brace-orequal-initializer does not have a user-provided default constructor,
  • X is a union and all of its variant members are of const-qualified type (or array thereof),
  • X is a non-union class and all members of any anonymous union member are of const-qualified type (or array thereof), or
  • any direct or virtual base class, or non-static data member with no brace-or-equal-initializer, has class type M (or array thereof) and either M has no default constructor or overload resolution (13.3) as applied to M’s default constructor results in an ambiguity or in a function that is deleted or inaccessible from the defaulted default constructor.

For example, it would appear from the above that the following program is well-formed:

struct X {
  X(int) {}
};

struct Y {
  X x;
};

Here Y does not have an implicitly defined default constructor because it's both not used and defined as deleted.

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

1 Comment

Interesting quotation. That really hides all of the "It must have one if we can create objects" behind the fact that it doesn't until we do.
0

No, there is no such trick, unless it's a catch in the exact wording of the question.

12.1p5:

If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted.

The implicitly declared default constructor might be defined as deleted, but it is still a member of the class.

2 Comments

I wish the standard was written in a more accessible language. We somehow reached the opposite conclusions by reading the same text, and I have no idea who is right. :-(
@NPE: The constructor can be selected by overload resolution without being odr-used, e.g. via decltype. But I agree that the implicitly declared default constructor is not necessarily implicitly defined.
0

Effective C++, Item 5: "These functions [ctor, dtor, copy ctor, copy assignment operator] are generated only if they are needed [...]"

According to that, if you have a class that has no declared ctors, and you do not create any instances of that class, then your class will have no constructor(s) at all.

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.