什么是成员变量列表后的冒号在构造函数中有好处? [英] What is the member variables list after the colon in a constructor good for?
问题描述
我正在阅读这个C ++开源代码,我来到一个构造函数,但我没有得到它(基本上是因为我不知道C ++:P)
我理解C和Java很好。
TransparentObject :: TransparentObject(int w,int x,int y,int z ):
_someMethod(0),
_someOtherMethod(0),
_someOtherOtherMethod(0),
_someMethodX(0)
{
int bla;
int bla;
}
到目前为止我可以deduce第一行只声明construtor名称, ::听起来像是属于我。而{}之间的代码是它的构造函数体。
我认为paremeter和第一个{是类似方法默认参数或某些东西后,但我没有找到一个合理的解释在网上。我在示例中发现的大多数C ++构造函数几乎与Java中的那些相同。
我在我的假设是正确的吗? ::就像属于,以及params和body之后的列表像默认参数或者什么?
UPDATE:
感谢您的答案。
那些被称为方法? (我猜测没有),并且在构造函数体中调用它们有什么区别
最常见的情况是: / p>
class foo {
private:
int x;
int y;
public:
foo(int _x,int _y):x(_x),y(_y){}
}
这将设置 x
和 y
在构造函数参数中的 _x
和 _y
中给出。这通常是构造任何被声明为数据成员的对象的最好方法。
这也可能是你在构造函数链接:
class foo:public bar {
foo(int x,int y):bar(x,y){}
} ;
在这个实例中,类的构造函数将调用其基类的构造函数,并传递值 x
和 y
。
TransparentObject :: TransparentObject(int w,int x,int y,int z):
_someMethod ,
_someOtherMethod(0),
_someOtherOtherMethod(0),
_someMethodX(0)
{
int bla;
int bla;
}
::
操作符称为作用域解析操作符。它基本上只是表示 TransparentObject
是 TransparentObject
的成员。第二,你假设构造函数的主体出现在大括号中是正确的。
更新:感谢您的答案。那些被称为方法? (我猜没有),并在构造函数体内调用它们有什么区别
有关这个主题的更多信息比我可能会向您此处。您必须使用初始化列表的最常见区域是初始化引用或 const
,因为这些变量必须在创建后立即赋予值。
I'm reading this C++ open source code and I came to a constructor but I don't get it ( basically because I don't know C++ :P )
I understand C and Java very well.
TransparentObject::TransparentObject( int w, int x, int y, int z ) :
_someMethod( 0 ),
_someOtherMethod( 0 ),
_someOtherOtherMethod( 0 ),
_someMethodX( 0 )
{
int bla;
int bla;
}
As far I can "deduce" The first line only declares the construtor name, the "::" sounds like "belongs to" to me. And the code between {} is the constructor body it self.
I "think" what's after the paremeters and the first "{" are like methods default parameters or something, but I don't find a reasonable explanation on the web. Most of the C++ constructors that I found in the examples are almost identical to those in Java.
I'm I right in my assumptions? "::" is like belongs to, and the list after params and body are like "default args" or something?
UPDATE:
Thanks for the answers.
May those be called methods? ( I guess no ) and what is the difference of call them within the constructor body
解决方案 The most common case is this:
class foo{
private:
int x;
int y;
public:
foo(int _x, int _y) : x(_x), y(_y) {}
}
This will set x
and y
to the values that are given in _x
and _y
in the constructor parameters. This is often the best way to construct any objects that are declared as data members.
It is also possible that you were looking at constructor chaining:
class foo : public bar{
foo(int x, int y) : bar(x, y) {}
};
In this instance, the class's constructor will call the constructor of its base class and pass the values x
and y
.
To dissect the function even further:
TransparentObject::TransparentObject( int w, int x, int y, int z ) :
_someMethod( 0 ),
_someOtherMethod( 0 ),
_someOtherOtherMethod( 0 ),
_someMethodX( 0 )
{
int bla;
int bla;
}
The ::
-operator is called the scope resolution operator. It basically just indicates that TransparentObject
is a member of TransparentObject
. Secondly, you are correct in assuming that the body of the constructor occurs in the curly braces.
UPDATE: Thanks for the answers. May those be called methods? ( I guess no ) and what is the difference of call them within the constructor body
There is much more information on this subject than I could possibly ever give you here. The most common area where you have to use initializer lists is when you're initializing a reference or a const
as these variables must be given a value immediately upon creation.
这篇关于什么是成员变量列表后的冒号在构造函数中有好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!