如果函数具有相同的名称,如何调用构造函数
How to call constructor if function has the same name
如果我有以下内容:
class T
{
public:
T(){}
};
void T()
{
}
int main()
{
T(); // this calls the function, how can I call the constructor T()?
}
我对它没有任何问题,因为我可以重命名它,但只是好奇我如何强制它调用构造函数,而且我也在问自己为什么函数调用似乎比构造函数具有更高的优先级。此外,为什么没有关于重复名称的警告消息。
除了jaunchopanza所说的之外,您还可以限定呼叫:
T::T();
使用此版本,您可以创建临时文件:
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())
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 函数何时会在c++中包含stack_Unwind_Resume调用
- Singleton模式中的手动析构函数调用:调用多次
- 从同一父调用调用不同的子类函数
- 我可以通过CPP程序中的数据库调用调用Java程序
- C++17标准对在nullptr上调用delete有何规定
- NSIS系统::调用-调用方法失败
- 参数构造函数调用调用
- 当 int 方法工作正常时,void 方法有何不同,或者为什么我不能调用 void 方法?
- 让非托管 C++ 代码调用调用 C# 代码的托管 C++ 代码
- 关于在矢量上调用clear如何改变容量,标准有何规定
- 如何让函数调用调用它的函数
- 如何p/调用调用函数只使用,但c++需要.a/.lib/.o文件
- C#DLL可以调用调用本机C++静态库的C++/CLI托管包装器吗