Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 511
codymanix Avatar asked May 07 '12 14:05

codymanix


1 Answers

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());
}
like image 92
Luchian Grigore Avatar answered Oct 06 '22 00:10

Luchian Grigore