0

I've stumbled across a statement on cppreference that says:

Constructors have no names and cannot be called directly.

Is that true? Up until now, I've thought that the NAME of a constructor is always the respective class name. However, now it makes me wonder if this is actually the RETURN TYPE as constructors do kind of return (initialized) instances of a class, don't they?

Furthermore, what is it about "cannot be called directly"?

Suppose I have the following code snippet:

class A
{
    // some members
};

A x = A();

A() is a direct call to the default constructor here, isn't it? Or am I having a big misconception about both these things?

I'd appreciate any help a lot!

Best regards, Ruperrrt

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Ruperrrt
  • 489
  • 2
  • 13
  • 7
    What they mean is you can't call `x.A();`, while you can call `x.~A();` if you want to. – m88 Mar 11 '21 at 15:06
  • 2
    some good reading on why you can't call the constructor: https://stackoverflow.com/questions/45142080/why-cant-constructors-be-explicitly-called-while-destructors-can – NathanOliver Mar 11 '21 at 15:06
  • for example you cannot get a member function pointer to a constructor and then call it. The constructor is special because you call it before you have the object. – 463035818_is_not_an_ai Mar 11 '21 at 15:06
  • 2
    _`A()` is a direct call to the default constructor here, isn't it?_ It isn't. It is a construction of a _value-initialized_ temporary of type `A`. This temporary is then used for _copy-intialization_ of `x` (which may or must be elided). – Daniel Langr Mar 11 '21 at 15:09
  • Ah, I see. Thank you all for those comments. That cleared up what they meant by "cannot be called directly." But what about has no name? Is it really the return type and NOTconstructor name = class name? But if so, then A() in my example would mean () which doesn't follow the usual () syntax. – Ruperrrt Mar 11 '21 at 15:11
  • The function call part is now clear (I've written the comment above before reading the Daniel Langr and rustyx). The last question that remains for me is the one about the name mentioned directly above. – Ruperrrt Mar 11 '21 at 15:17
  • https://eel.is/c++draft/class.ctor#general-1 The thing that "names" a constructor in its declaration syntax is a declarator (i.e. type), if that's what you're asking. You can't give it its own identifier/name. – HTNW Mar 11 '21 at 15:20
  • Well, I don't know about "constructors have no names". When you do `struct Derived : Base { using Base::Base; };` you are naming the constructor aren't you? Anyway it could indeed be a quirk about how the standard defines things. – bolov Mar 11 '21 at 15:21
  • I'm surprised that there is explicit statement "*Constructors do not have names.*" in the standard, but no such mention for destructor. Does that mean destructor has a name? – Yksisarvinen Mar 11 '21 at 15:24
  • @HTNW The first note on that link says: "Note 1: Because constructors do not have names, they are never found during unqualified 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. The syntax looks like an explicit call of the constructor. — end note]" So is A() actually an explicit type conversion using the functional notation instead of direct initialization as mentioned above? – Ruperrrt Mar 11 '21 at 15:41
  • @Yksisarvinen The statement is taken from cppreference's article about constructors and not the standard – Ruperrrt Mar 11 '21 at 16:01
  • Btw: How do I mark a question as answered, and how do I vote for useful comments? Thanks to everyone that has answered so far! – Ruperrrt Mar 11 '21 at 16:06
  • @Ruperrrt I was referring to the standard. Link provided by HTNW: https://eel.is/c++draft/class.ctor#general-1 has precisely that statement, whereas destructor paragraph doesn't have that: https://eel.is/c++draft/class.dtor . Although statement regarding declarator are the same as in constructor's case, so I guess destructor also doesn't have name, but it wasn't explicitly stated. – Yksisarvinen Mar 11 '21 at 16:07

0 Answers0