构造临时对象时不允许成员函数重新声明 [英] Member function redeclaration not allowed when constructing temporary object

查看:136
本文介绍了构造临时对象时不允许成员函数重新声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我对此代码有一个奇怪的编译器行为:


----代码片段的开头 - -


类基地

{

public:

static unsigned int ClassId();


};


class Derived

{

public:

static unsigned int ClassId(){return 2; };


};


班级考试

{

public:

测试(unsigned int p1){};


};


int main(int argc,char * argv [])

{

//这个工作

测试a(Derived :: ClassId());


//这次崩溃

测试(Derived :: ClassId());


返回0;

}


----代码片段结尾----





Test(Derived :: ClassId());


使用gcc 3.3.5和Visual Studio 8进行编译。使用gcc我得到

以下错误消息:


test.cpp:在函数`int main(int,char **)'':

test.cpp :24:错误:`Test Derived :: ClassId()''的原型没有

匹配任何

类'衍生''

test.cpp:11:错误:候选者是:static unsigned int

派生:: ClassId()

test.cpp:24:错误:`Test Derived ::班级号() '和`static unsigned int

Derived :: ClassId()''不能超载

test.cpp:24:错误:`Test Derived :: ClassId的原型( )''没有

匹配任何

类`Derived''

test.cpp:11:错误:候选者是:static unsigned int

派生:: ClassId()

test.cpp:24:错误:`Test Derived :: ClassId()''和`static unsigned int

派生:: ClassId()''不能超载

test.cpp:24:错误:声明`Test Derived :: ClassId()''



不是定义


那么为什么它以这种方式失败了一个临时对象呢?如果

编译器不能处理相当于非临时情况的情况吗?

您是否对此行为有任何提示?

谢谢和致以最好的问候


Marco

解决方案

Marco Wedekind写道:

大家好,

我对这段代码有一个奇怪的编译器行为:

----代码片段的开头----

班级基地
{
公开:
static unsigned int ClassId();

};

class Derived
{
public:
static unsigned int ClassId(){return 2; };

};

上课测试
{
公开:
测试(unsigned int p1){};

};

int main(int argc,char * argv [])
//
//测试a(Derived :: ClassId ());

//崩溃
测试(Derived :: ClassId());

返回0;

}

----代码片段结束----

测试行(Derived :: ClassId());
test.cpp:在函数`int main( int,char **)'':
test.cpp:24:错误:`Test Derived :: ClassId()''的原型不匹配'Derived'类中的任何
'
test.cpp:11:错误:候选者是:static unsigned int
Derived :: ClassId()
test.cpp:24:错误:`Test Derived :: ClassId()' '和`static unsigned int
Derived :: ClassId()''不能超载
test.cpp:24:错误:原型对于`Test Derived :: ClassId()''没有匹配任何类别`Derived''
test.cpp:11:错误:候选者是:static unsigned int
Derived :: ClassId()
test.cpp:24:错误:`Test Derived :: ClassId()''和`static unsigned int
Derived :: ClassId()''不能重载
test.cpp:24:错误:
类之外的`Test Derived :: ClassId()'声明是不是定义

那么为什么它会失败用这样的临时对象?如果
编译器不能处理相当于非临时情况的情况吗?
您是否对此行为有任何提示?

谢谢和最好的问候
Marco




编译器将第二行解释为函数原型

而不是临时对象实例化(请注意它在错误消息中删除了

括号)。规则是,如果声明可以将
解释为原型,它将是。


干杯! --M


您好,


感谢您的回复!


你对如何规避这个问题有什么建议

语法上?什么是最优雅的解决方案?


最好的问候


Marco

我还有一点不明白的是:为什么它会掉落

括号?如果编译器会保留它们,我看不到

行如何:


Test(Derived :: ClassId());


可以解释为函数原型...


祝你好运


Marco


Hello all,

I have a strange compiler behaviour with this code:

---- Begin of code snippet ----

class Base
{
public:
static unsigned int ClassId();

};

class Derived
{
public:
static unsigned int ClassId() { return 2; };

};

class Test
{
public:
Test(unsigned int p1) {};

};

int main(int argc, char * argv[])
{
// This works
Test a(Derived::ClassId());

// This crashes
Test(Derived::ClassId());

return 0;

}

---- End of code snippet ----

The line

Test(Derived::ClassId());

failes to compile with gcc 3.3.5 and Visual Studio 8. With gcc I get
the following error message:

test.cpp: In function `int main(int, char**)'':
test.cpp:24: error: prototype for `Test Derived::ClassId()'' does not
match any
in class `Derived''
test.cpp:11: error: candidate is: static unsigned int
Derived::ClassId()
test.cpp:24: error: `Test Derived::ClassId()'' and `static unsigned int
Derived::ClassId()'' cannot be overloaded
test.cpp:24: error: prototype for `Test Derived::ClassId()'' does not
match any
in class `Derived''
test.cpp:11: error: candidate is: static unsigned int
Derived::ClassId()
test.cpp:24: error: `Test Derived::ClassId()'' and `static unsigned int
Derived::ClassId()'' cannot be overloaded
test.cpp:24: error: declaration of `Test Derived::ClassId()'' outside of
class
is not definition

So why does it fail with a temporary object in such a way? Should the
compiler not handle this case equivalent to the "non-temporary case"?
Do you have any hints on this behaviour?

Thanks and best regards

Marco

解决方案

Marco Wedekind wrote:

Hello all,

I have a strange compiler behaviour with this code:

---- Begin of code snippet ----

class Base
{
public:
static unsigned int ClassId();

};

class Derived
{
public:
static unsigned int ClassId() { return 2; };

};

class Test
{
public:
Test(unsigned int p1) {};

};

int main(int argc, char * argv[])
{
// This works
Test a(Derived::ClassId());

// This crashes
Test(Derived::ClassId());

return 0;

}

---- End of code snippet ----

The line

Test(Derived::ClassId());

failes to compile with gcc 3.3.5 and Visual Studio 8. With gcc I get
the following error message:

test.cpp: In function `int main(int, char**)'':
test.cpp:24: error: prototype for `Test Derived::ClassId()'' does not
match any
in class `Derived''
test.cpp:11: error: candidate is: static unsigned int
Derived::ClassId()
test.cpp:24: error: `Test Derived::ClassId()'' and `static unsigned int
Derived::ClassId()'' cannot be overloaded
test.cpp:24: error: prototype for `Test Derived::ClassId()'' does not
match any
in class `Derived''
test.cpp:11: error: candidate is: static unsigned int
Derived::ClassId()
test.cpp:24: error: `Test Derived::ClassId()'' and `static unsigned int
Derived::ClassId()'' cannot be overloaded
test.cpp:24: error: declaration of `Test Derived::ClassId()'' outside of
class
is not definition

So why does it fail with a temporary object in such a way? Should the
compiler not handle this case equivalent to the "non-temporary case"?
Do you have any hints on this behaviour?

Thanks and best regards

Marco



The compiler is interpreting the second line as a function prototype
rather than a temporary object instantiation (note that it dropped the
parentheses in the error message). The rule is that if a statement can
be interpreted as a prototype, it will be.

Cheers! --M


Hello,

thanks for your reply!

Do you have any suggestion on how to circumvent this problem
syntactically? What would be the most elegant solution?

Best regards

Marco


One thing I still do not understand is: Why does it drop the
parentheses? If the compiler would keep them, I do not see how the
line:

Test(Derived::ClassId());

could be interpreted as a function prototype...

Best regards

Marco


这篇关于构造临时对象时不允许成员函数重新声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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