Constructors in C ++

Why does this constructor name match the class name?

+6
c ++ constructor language-design
source share
10 answers

According to Straustrap, because the alternative to the new feature "was a source of confusion." See the “Design and Evolution of C ++” section, section 3.11.2, although this is a complete excuse that I quoted.

Edit: As people point out, there are a number of alternative solutions. For example, Smalltalk does the following:

myclass new 

sending a "new" message to an object of class myclass. Obviously, a C ++ solution here would be a bit silly:

 myclass myclass 

not manifestly reasonable.

Delphi, OTOH, allows any named function to be a constructor, placing it as such:

 constructor Create; constructor FooBar; 

both will be OK constructor names for any class. As with Smalltalk, you need to call them on the class object:

 myclass.Create; 

Of all these solutions, I believe that C ++ is one of the most elegant, and I understand why it was almost universally accepted in successor languages.

+10
source share

C ++ language standard:

12.1. Constructors have no names.

Your question is based on confusion. Constructors use special declaration syntax, where instead of the participant name, the class name is again used. I don’t see anything wrong with this syntax, so I can’t even imagine what your question will bring up (which makes it difficult, if not impossible).

However, constructors in C ++ have no names. They just do not need names, because by design in C ++ there is no context where you will need to refer to the constructor.

+9
source share

In the original “C with classes” it was not actually - it was called “new”, IIRC. In any case, for most of the "why" questions about C ++, you can find the answers in the book Design and Evolution of C ++.

+5
source share

Actually, this is not the same thing. 12.1 / 1:

Constructors have no names

The only way to call the constructor is to use the special syntax for constructing / transforming the object: it was not found when searching for the name of the function, and you cannot take the address of the constructor. This is probably good.

I guess the constructor declaration syntax might look something like this:

 struct Foo { int a; constructor(int a) : a(a) { } }; 

But this would require an additional reserved word constructor . This would mean that the code for declaring the constructor is less like the code for constructing an object. The only "advantage" is to free "Foo" to use as the name of a member function. This doesn’t sound very useful to me, especially since you lose the “constructor” as the name of a member function. If there was some protest for Foo as a member function name, I guess it could be achieved differently, for example, using a slightly different syntax, declare a constructor (+ Foo to go with ~ Foo, maybe?). Therefore, I suppose that was not.

I can’t immediately see that in C ++ there are "username constructors". If you need a static member function Foo that takes certain parameters and returns Foo, you can declare it like this:

 struct Foo { static Foo bar(int); }; 

and "use it as constructor" as follows:

 Foo f = Foo::bar(12); 
+3
source share

If you want the constructor to call the constructor of the base class as an initializer, you need to specify its constructor name. It would be difficult if they were all called the same. eg.

 class Animal { public: Animal(); }; class Dog { public: Dog(); }; Animal::Animal() { // Base class constructor } Dog::Dog() : Animal() { // Derived class constructor, calling the base constructor as an initializer } 
+2
source share

Because it speaks the language specification. In some languages, such as Python, this is not the case.

+1
source share

It is simply a matter of language definition. The compiler knows that methods with the same name are the constructor. It also makes creating objects of this class very clear, anyone without knowing that the class knows that if the name equals this, it is a constructor.

+1
source share

What are the alternatives? Maybe:

  • Come up with a new keyword to specify the constructor as such
  • Have one name for all constructors (e.g. new() ).

Both of these parameters seem less desirable than reality.

0
source share

I assume you are fairly new to C ++ and possibly even object oriented programming. So, firstly, I hope you understand what a class constructor is.

Most types will have some kind of constructor used for initialization. Even when it comes to int primitives or other types of variables, everything can be initialized2. So, when creating classes, you are prompted to provide an initializer called a constructor. Constructors may have parameters that may or may not be required. They can also be overloaded, which means that a class can have many constructors.

Since constructors are initializers, the call is not implicit to them: the class constructor will be called every time you create an object of this class, whether using the new keyword or by declaring it on the stack:

 CMyObject obj; 

They should now be declared inside your class. They are methods, after all ... so what should be his name? For example, Python takes a different approach and uses the __init__ keyword to do this; The C ++ developer decided that this would be the name of the class.

This makes sense, because in the end, having a member method with a class name can cause name conflicts (ambiguity) along the system. (Although this series of articles is about C #, it explains why using a name for a member of a specific area with the same name is bad)

² But sometimes they are not intended to reduce lead time.

0
source share

The constructor is called every time an object is created, and this usually happens automatically, that is, the compiler calls the constructor, which is a function call that has no return type. Therefore, C ++ decided to call this function the name of the class.

0
source share

All Articles