C++中的构造函数重载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7330296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Constructor Overloading in C++
提问by sdasdadas
My C++ overloading does not act as I assume it should:
我的 C++ 重载并不像我认为的那样:
#include "Node.h"
#include <iostream>
Node::Node()
{
cout << "1" << endl;
Node(Game(), 0.0);
}
Node::Node(double v)
{
cout << "2" << endl;
Node(Game(),v);
}
Node::Node(Game g, double v)
{
cout << "3" << endl;
numVisits = 0;
value = v;
game = g;
}
And the output from:
和输出:
Node n(16);
cout << n.value << endl;
is 0, when it should be 16.
是 0,而它应该是 16。
What am I doing incorrectly?
我做错了什么?
回答by Mu Qiao
Node(Game(),v);
in your constructor doesn't do what you expected. It just creates a temporary without using it and makes no effect. Then it immediately destructs that temporary when control flows over the ;.
Node(Game(),v);
在你的构造函数中没有做你期望的。它只是在不使用它的情况下创建一个临时文件并且没有任何效果。然后,当控制流过 ; 时,它会立即销毁临时文件。
The correct way is initializing the members in each constructor. You could extract their common code in a private init() member function and call it in each constructor like the following:
正确的方法是在每个构造函数中初始化成员。您可以在私有 init() 成员函数中提取它们的公共代码,并在每个构造函数中调用它,如下所示:
class Foo {
public:
Foo(char x);
Foo(char x, int y);
...
private:
void init(char x, int y);
};
Foo::Foo(char x)
{
init(x, int(x) + 3);
...
}
Foo::Foo(char x, int y)
{
init(x, y);
...
}
void Foo::init(char x, int y)
{
...
}
C++11 will allow constructors to call other peer constructors (known as delegation), however, most compilers haven't supported that yet.
C++11 将允许构造函数调用其他对等构造函数(称为委托),但是,大多数编译器尚不支持。
回答by Praetorian
The feature you're trying to use is called delegating constructors, which is part of C++0x. Using that syntax your second constructor becomes
您尝试使用的功能称为委托构造函数,它是 C++0x 的一部分。使用该语法,您的第二个构造函数变为
Node::Node(double v)
: Node(Game(),v)
{
cout << "2" << endl;
}
回答by Tim Meyer
You could do it like this, where init() is a private method:
你可以这样做,其中 init() 是一个私有方法:
#include "Node.h"
#include <iostream>
Node::Node()
{
cout << "1" << endl;
init(Game(), 0.0);
}
Node::Node(double v)
{
cout << "2" << endl;
init(Game(),v);
}
Node::Node(Game g, double v)
{
cout << "3" << endl;
init(g,v)
}
void Node::init(Game g, double v)
{
numVisits = 0;
value = v;
game = g;
}
回答by Mahesh
Node::Node(double v)
{
cout << "2" << endl;
Node(Game(),v); // 1
}
- Creates a nameless object, which does not persist beyond that expression. So, it is not affecting the original object's
value
upon which the single argument constructor is instantiated. You also need to understand that this temporary object is entirely different from the original constructing object.
- 创建一个无名对象,该对象不会在该表达式之外持续存在。因此,它不会影响
value
实例化单参数构造函数的原始对象。您还需要了解这个临时对象与原始构造对象完全不同。
However you can extend the life time of this temporary object by a constreference i.e.,
但是,您可以通过常量引用来延长此临时对象的生命周期,即,
Node::Node(double v)
{
cout << "2" << endl;
const Node& extendTemporay = Node(Game(),v);
value = extendTemporary.value ; // Just trivial example;
// You can simply do it by value = v;
}
回答by mister why
In a nutshell:
简而言之:
#include <iostream>
#include "Node.h"
Node::Node()
: game(Game()), value(0.), numVisits(0)
{
std::cout << "1" << std::endl;
}
Node::Node(double v)
: game(Game()), value(v), numVisits(0)
{
std::cout << "2" << std::endl;
}
Node::Node(Game g, double v)
: game(g), value(v), numVisits(0)
{
std::cout << "3" << std::endl;
}
As everyone said, you cannot call a constructor overload from a constructor. Delegation is an upcomming feature we'll meet with C++11. It's not much text to type, don't be lazy.
正如每个人所说,您不能从构造函数调用构造函数重载。委托是我们将在 C++11 中遇到的一个即将到来的特性。要输入的文字不多,不要偷懒。