具有默认值和值的构造函数不同的构造函数
我想做这样的事情
class foo{
private:
double a,b;
public:
foo(double a=1, double b=2){
this.a=a;
this.b=b;
}
}
int main(){
foo first(a=1);
foo first(b=2);
}
这样的事情可能吗还是我需要创建两个新的构造函数?
2.问题来了:这两个构造函数有什么区别:
class foo{
private:
int a;
public:
foo(int in):a(in){}
}
或者
class foo{
private:
int a;
public:
foo(int in){a=in}
}
I would like to do something like this
class foo{
private:
double a,b;
public:
foo(double a=1, double b=2){
this.a=a;
this.b=b;
}
}
int main(){
foo first(a=1);
foo first(b=2);
}
Is such thing possible or do I need to create two new constructors?
Here comes the 2. question: What is the difference in those two constructors:
class foo{
private:
int a;
public:
foo(int in):a(in){}
}
or
class foo{
private:
int a;
public:
foo(int in){a=in}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 C++ 中你不可能真正拥有这个。它曾经被考虑标准化,但后来被放弃。 Boost.Parameter 尽可能地近似命名参数,请参阅
http://www.boost.org/doc /libs/1_47_0/libs/parameter/doc/html/index.html
第一个构造函数正在初始化 a,而第二个构造函数正在为其赋值。对于这个特殊情况(int)来说没有太大区别。
You cannot really have this in C++. It was once considered for standarization but then dropped. Boost.Parameter does as much as it can to approximate named parameters, see
http://www.boost.org/doc/libs/1_47_0/libs/parameter/doc/html/index.html
The first constructor is initializating a, while the second is assigning to it. For this particular case (int) there isn't much of a difference.
在 C++ 中,
this
指针是一个指针
,而不是对象
(在 java 中,您可以使用.
访问它) >)。也就是说,您需要一个取消引用箭头(即this->member
或(*this).member
)。无论如何,这是可以做到的。然而,C++ 中的参数化以相反的顺序工作,并且不能命名。例如,
int test(int a=2, int b=42)
和int test(int a, int b=42)
都是合法的。但是,int test(int a=2, b)
是不合法的。至于你的第二个问题,这个例子中的构造函数之间没有显着的区别。存在一些微小的(潜在的)速度差异,但在这种情况下它们很可能可以忽略不计。在第一个中,您使用初始化列表(继承和调用基类的构造函数(如果需要)所需),第二个只是显式设置变量的值(即使用operator=())。
In C++, the
this
pointer is apointer
not anobject
(in java, you would access it with the.
). That said, you would need a dereferencing arrow (i.e.this->member
or(*this).member
).In any case, this can be done. However, parameterization in C++ works in reverse order and cannot be named. For instance,
int test(int a=2, int b=42)
is legal as well asint test(int a, int b=42)
. However,int test(int a=2, b)
is not legal.As for your second question, there is no significant difference between the constructors in this example. There are some minor (potential) speed differences, but they are most-likely negligible in this case. In the first one you are using an initializer list (required for inheritance and calling the constructor of the base class if need be) and the second one is simply explicitly setting the value of your variable (i.e. using operator=()).
对于您的示例来说,这可能有点过分了,但您可能想了解 命名参数惯用语。
It's probably overkill for your example, but you might like to learn about the Named Parameter Idiom.
C++ 不支持命名参数,因此:
不合法。您还有多个非唯一标识符(例如
first
)。C++ doesn't support named parameters so this:
Is not legal. You also have multiple non-unique idenfiers (e.g.
first
).