Home > front end >  Does a constructor has a "type" in C since it is a special member function
Does a constructor has a "type" in C since it is a special member function

Time:04-03

I recently learnt that constructors do not have names in C and some other things about them. I am also aware that a function has a type in C called a function type. For example,

void func(int)
{
}

In the above snippet the func has the function type void (int).

Now, i want to know that since constructors are special member functions then do they also have a type like the one shown above. For example say we have:

struct Name
{ 
    Name(int)
    {
    }
};

Does the constructor shown above also has a function type just like ordinary functions or ordinary member functions. If yes, then how can we find that type. Like we can use decltype on ordinary functions, is it permitted to use decltype on constructors to find their type.

CodePudding user response:

is it permitted to use decltype on constructors to find their type

It's not permitted. Primarily because there is no way to name a constructor. A common misnomer is that an expression like Name(0) or new Name(0), calls the constructor. But that isn't the case like in func(0). A constructor is never called by us directly, but rather always indirectly by the language construct that requires a new object to come into being.

[class.ctor.general]

1 ... Constructors do not have names.

2 A constructor is used to initialize objects of its class type. Because constructors do not have names, they are never found during name lookup; however an explicit type conversion using the functional notation ([expr.type.conv]) will cause a constructor to be called to initialize an object. [Note 1: The syntax looks like an explicit call of the constructor. — end note]

Because we cannot name them, we cannot use introspection mechanisms like decltype to examine them. Therefore the standard doesn't specify a "type" for constructors, since there is no way for a strictly standard compliant program to examine said type.

A constructor also cannot possess a signature (as defined by the standard), since that by definition includes the function name (and constructors are, as mentioned, nameless).

[defns.signature.member] signature

⟨class member function⟩ name, parameter-type-list, class of which the function is a member, cv-qualifiers (if any), ref-qualifier (if any), and trailing requires-clause (if any)

CodePudding user response:

A classes constructor is never called explicitly. You instantiate an object using something like new Name(5), memory will be allocated and then some part of that memory may be initialized by the steps defined in the constructors body. Notice that a constructor has no return statement. What is returned by new Name(5) is a memory reference to the memory allocated by new.

This is given away by syntax like:

Name * foo = new Name(5)

foo is the pointer to whatever has be allocated and type checking can be done because Name referrs to a class, not to its constructor.

  • Related