C ++初始化列表 [英] C++ Initialisation List
问题描述
许多从业者推荐使用初始化列表
来初始化类的数据成员。但是,在将参数/参数传递给类的构造函数时,我遇到了
这样的列表问题 -
请参阅下面的代码:
class Foo {
int number;
public:
Foo(int number): this-> number(number){}
};
问题在于这个指针以及
的参数这一事实传递的
与该类的数据成员具有相同的名称。当然,如果我将
重命名为参数,从而避免使用此指针,那么问题将会消失。但是,如果我无法避免
更改参数的名称,是否有解决方案?
任何帮助表示赞赏
问候,
A
Hi,
It is recommened by many practitioners to always use an initialisation list
to initialise data members of a class. However, I am having problems with
such a list when passing parameters/arguments to a constructor of a class -
see code below:
class Foo{
int number;
public:
Foo(int number): this->number(number) {}
};
The problem lies with the this pointer and the fact that the arguments that
is passed has the same name as a data member of the class. Of course if i
renamed the argument and thus avoid the need of the this pointer the problem
will go away. However, is there a solution to this if i cannot avoid
changing the name of the argument?
Any help appreciated
Regards,
A
推荐答案
" A" < A@iprimus.com.au>在消息中写道
新闻:3f ******** @ news.iprimus.com.au ...
"A" <A@iprimus.com.au> wrote in message
news:3f********@news.iprimus.com.au...
>许多从业者建议始终使用初始化
列表来初始化类的数据成员。但是,在将参数/参数传递给
类的构造函数时,我遇到了这样的列表问题 - 请参阅下面的代码:
class Foo {
int number;
public:
Foo(int number):this-> number(number){}
};
问题在于this指针以及传递的参数
与该类的数据成员具有相同名称的事实。当然,如果我重新命名参数,从而避免需要这个指针,那么
问题就会消失。但是,如果我无法避免更改参数的名称,是否有解决方案?
任何帮助表示赞赏
问候,
A
Hi,
It is recommened by many practitioners to always use an initialisation list to initialise data members of a class. However, I am having problems with
such a list when passing parameters/arguments to a constructor of a class - see code below:
class Foo{
int number;
public:
Foo(int number): this->number(number) {}
};
The problem lies with the this pointer and the fact that the arguments that is passed has the same name as a data member of the class. Of course if i
renamed the argument and thus avoid the need of the this pointer the problem will go away. However, is there a solution to this if i cannot avoid
changing the name of the argument?
Any help appreciated
Regards,
A
查看Andrey Tarasevich最近的帖子(主题为Re:End-of-the-week fun)
表示参数和数据成员可能具有相同的名称
没有问题。只需删除此即可。从你的例子中......
See a recent post by Andrey Tarasevich (subject "Re: End-of-the-week fun")
that indicates the parameter and the data member may have the same name with
no problems. Just drop the "this" from your example...
A写道:
...
Foo {
int number;
public:
Foo(int number):this-> number(number){}
};
问题在于使用this指针以及传递
的参数与该类的数据成员具有相同名称的事实。当然,如果我重新命名参数,从而避免使用这个指针,问题就会消失。但是,如果我无法避免更改参数的名称,是否有解决方案?
...
...
class Foo{
int number;
public:
Foo(int number): this->number(number) {}
};
The problem lies with the this pointer and the fact that the arguments that
is passed has the same name as a data member of the class. Of course if i
renamed the argument and thus avoid the need of the this pointer the problem
will go away. However, is there a solution to this if i cannot avoid
changing the name of the argument?
...
只需删除''这个>''你完成了:
class Foo {
int number;
public:
Foo(int number):number(number){}
};
memeber名称在查询范围内查找class(''Foo :: number''
),初始化程序中使用的名称在构造函数的范围内查找
(参数''的数字''被找到了。)
但是,很多人会将这种方法视为成员/参数命名,这是一个糟糕的编码风格的例子。也许你应该为数据成员或构造函数参数创建一个不同的名称
。
-
祝你好运,
Andrey Tarasevich
Just drop ''this->'' and you are done:
class Foo{
int number;
public:
Foo(int number): number(number) {}
};
The memeber names are looked up in the scope of the class (''Foo::number''
is found), the names used in the initializer are looked up in the scope
of the constructor (parameter ''number'' is found).
However, many will regard this approach to member/parameter naming as an
example of bad coding style. Maybe you should invent a different name
for either the data member or the constructor parameter.
--
Best regards,
Andrey Tarasevich
" Andrey Tarasevich" <一个************** @ hotmail.com> schreef in bericht
新闻:P4 ******************** @ comcast.com ...
"Andrey Tarasevich" <an**************@hotmail.com> schreef in bericht
news:P4********************@comcast.com...
A写道:
...
类Foo {
int number;
公开:
Foo(int number) :this-> number(number){}
};
问题在于this指针以及传递的参数
具有相同的名称该类的数据成员。当然,如果
我重命名了参数,从而避免了这个指针的需要,那么
问题就会消失。但是,如果我无法避免更改参数的名称,是否有解决方案?
...
...
class Foo{
int number;
public:
Foo(int number): this->number(number) {}
};
The problem lies with the this pointer and the fact that the arguments that is passed has the same name as a data member of the class. Of course if i renamed the argument and thus avoid the need of the this pointer the problem will go away. However, is there a solution to this if i cannot avoid
changing the name of the argument?
...
只需删除''this-> ''你完成了:
类Foo {
int number;
public:
Foo(int number):number(number){}
),查找初始化程序中使用的名称构造函数的范围(参数''number''被找到)。
然而,许多人会将这种方法视为成员/参数命名的一个不良编码的例子样式。也许你应该为数据成员或构造函数参数创建一个不同的名称。
Just drop ''this->'' and you are done:
class Foo{
int number;
public:
Foo(int number): number(number) {}
};
The memeber names are looked up in the scope of the class (''Foo::number''
is found), the names used in the initializer are looked up in the scope
of the constructor (parameter ''number'' is found).
However, many will regard this approach to member/parameter naming as an
example of bad coding style. Maybe you should invent a different name
for either the data member or the constructor parameter.
我用一个下划线结束所有数据成员。
class Point
{
public:
Point(int x,int y);
私人:
int x_;
int y_;
};
I end all data members with an underscore.
class Point
{
public:
Point(int x, int y);
private:
int x_;
int y_;
};
这篇关于C ++初始化列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!