Search code examples
c++constructorheader

Constructors with default parameters in Header files


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?


Solution

  • .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.