I was surprised to find that in VC++ 10, you can use a typedef to change the name of a class's constructor:
#include <iostream>
using namespace std;
class A
{
private:
typedef A alias;
public:
alias() { cout << "A ctor" << endl; }
};
int main()
{
A(); // prints "A ctor"
return 0;
}
Is this standard C++ or a Microsoft extension?
For explanation: Constructor name should be same as the class name.
It is therefore not realistic to create a base class object using the derived class constructor function. We cannot refer to the addresses of constructors since we only require the address of a function to call it and they can never be called directly.
No; constructors do not have a name. You cannot take the address of a constructor or pass a function pointer around, or even just call it like a normal function. The syntax A::A()
is just a special declarator syntax that allows you to declare and define the constructors, but it isn't a name.
That said, you cannot typedef objects (including function pointers) anyway, only types.
To comment on the MSVC behaviour, I quote from 12.1/3:
A typedef-name shall not be used [...] for a constructor declaration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With