How to call constructor if function has the same name
If I have the following:
class T
{
public:
T(){}
};
void T()
{
}
int main()
{
T(); // this calls the function, how can I call the constructor T()?
}
I have no any issue with it, since I could be possible to rename it, but just curious how I could force it to call the constructor, and also I am asking to myself why the function call seems to have higher priority than the constructor. Additionally, why is there no warning message in regards of the duplicate name.
Besides what jaunchopanza said, you can qualify the call:
T::T();
With this version, you can create temporaries:
class T
{
public:
T(){}
};
void foo(T) {}
void T()
{
}
int main(){
foo(T::T());
}
基本上,没有名称冲突,基本上,有不同的名称空间
T() ==> namespace::T() It;s a function call not an object instantiation.
T a() ==> namespace ::T::T() It;s is instantiation of T class calling the constructor.
T a() ; a() ==>namespace ::T::T()::() It;s call the functor of the T Class (need to define the operator())
链接地址: http://www.djcxy.com/p/10720.html
上一篇: 什么是TREC格式?
下一篇: 如果函数具有相同的名称,如何调用构造函数