views:

136

answers:

4
class foo {
public:
   int a;
   int b;
   foo(int a_, int b_) : a(a_), b(b_) {}
};

int main() {
  foo f;
}

when I try to compile the above code snippet, I got error message as below:

foo.cc: In function 'int main()'

foo.cc:12: error: no matching function for call to 'main()::foo::foo()'

foo.cc:10: note: candidates are: main()::foo::foo(int, int)

foo.cc:6:  note:                 main()::foo::foo(const main()::foo&)

but if I comment the file of explicit constructor with two integer prarmeters, then the code can be compiled. I guess the rule behind the magic is that when you explicite declare constructor with parameters, c++ compilor will not atuomatically generate a default constructor with no parameters for you.

Am I right? If am right, why does c++ has such behaviour? thanks in advance.

+2  A: 

Yes, you are right. If you declare a constructor it doesn't declare any implicit ones. As for why, I'm unsure.

Joshua Weinberg
This is quite obvious -- you should have some way to disable it for classes in which it makes no sense.
mbq
@mbq Well, you could declare it as private like you do for copy constructors and assignment operators
Michael Mrozek
There is a subtle error here. [In present-day C++,] There are in fact _two_ implicitly declared constructors: the default constructor and the copy constructor. The implicitly declared default constructor is suppressed if there are any user-declared constructors. The implicitly declared copy constructor is suppressed only if a user-declared copy constructor is present.
James McNellis
@Michael This way it would be still accessible by the class itself, and people have strange ideas.
mbq
+8  A: 

Compiler generates default constructor only if there're no user defined constructors.

C++ Standard 12.1/5:

A default constructor for a class X is a constructor of class X that can be called without an argument. If there is no user-declared constructor for class X, a default constructor is implicitly declared.

Kirill V. Lyadvinsky
+1 but perhaps it should be noted that, for the compiler to succeed in generating a default ctor, the members must also provide a default ctor (or be capable of being in an uninitialized state). Storing a reference, for instance, prevents the compiler from being able to do so regardless of the presence of user-defined ctors.
+1  A: 
class foo {
public:
    foo(int a_ = 0, int b_ = 0) : a(a_), b(b_) {}
    int a;
    int b;
};

The C language only generates a default ctor if you do not specify one yourself. You could specify default arguments to your ctor though

aCuria
+1  A: 

Basically, if you don't have an explicit constructor, C++ tries to be compatible with C structs and other plain old data types by supplying a default constructor, so that its objects can be defined and used normally. But if you do have an explicit constructor, C++ no longer provides this default, since you should be in complete control of how objects of your classes can be used (like, how they should be constructed). So, if you don't specify a default constructor, you can have objects that are not constructible without parameters, which is often useful.

Savui