在头文件中具有默认参数的构造函数 [英] Constructors with default parameters in Header files

查看:142
本文介绍了在头文件中具有默认参数的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的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.

这篇关于在头文件中具有默认参数的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆