头文件中带有默认参数的构造函数

2021-12-30 00:00:00 constructor header c++

我有一个像这样的 cpp 文件:

I have a cpp file like this:

#include Foo.h;
Foo::Foo(int a, int b=0)
{
    this->x = a;
    this->y = b;
}

我如何在 Foo.h 中引用它?

How do I refer to this in Foo.h?

推荐答案

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

相关文章