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.
-
Off the top of my head...make it static? I'm not sure.User1000547– User10005472012-12-17 20:57:57 +00:00Commented Dec 17, 2012 at 20:57
-
AFAIK, you can't declare a class to be static in C++.Qaz– Qaz2012-12-17 21:01:09 +00:00Commented 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.User1000547– User10005472012-12-17 21:06:43 +00:00Commented 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.Qaz– Qaz2012-12-17 21:07:26 +00:00Commented Dec 17, 2012 at 21:07
-
1Defining 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.aschepler– aschepler2012-12-17 21:15:26 +00:00Commented Dec 17, 2012 at 21:15
3 Answers
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.
1 Comment
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
decltype. But I agree that the implicitly declared default constructor is not necessarily implicitly defined.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.