I have a cpp file like this:
#include Foo.h; Foo::Foo(int a, int b=0) { this->x = a; this->y = b; }
How do I refer to this in Foo.h?
In both cases the constructor is inline. The only correct way to make it a regular out-of-line function would be to define it in the implementation file, not in the header.
A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .
Default Constructors in C++ They are primarily useful for providing initial values for variables of the class. The two main types of constructors are default constructors and parameterized constructors. Default constructors do not take any parameters.
.h:
class Foo { int x, y; Foo(int a, int b=0); };
.cc:
#include "foo.h" Foo::Foo(int a,int b) : x(a), y(b) { }
You only add defaults to declaration, not implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With