hi all,
why is that name of constructor is same as class name?
thank you in advance
hi all,
why is that name of constructor is same as class name?
thank you in advance
Because that's what the language specification says. In some languages like Python it's not.
That is just a matter of definition of the language. The compiler knows that the methods with the same name are the constructor. This also makes the making of objects of that class very clear, anyone without knowing the class knows that if the name equals this is the constructor.
In the original "C with classes" it actually wasn't - it was named "new", IIRC. Anyway for most "why" questions about C++, you can find answers in the "Design and Evolution of C++" book.
According to Stroustrup, because the alternative of a new-function "had been a source of confusion". See The Design & Evolution of C++, section 3.11.2, although that is the full justification I quoted.
Edit: As people have pointed out, there are a number of alternative solutions. Smalltalk for example, does this:
myclass new
sending a "new" message to the myclass class object. Obviously, a C++ solution here would be a bit silly:
myclass myclass
is not obviously sensible.
Delphi, OTOH, allows any named function to be a constructor by tagging it as such:
constructor Create;
constructor FooBar;
would both be OK constructor names for any class. As with Smalltalk, you need to call them on the class object:
myclass.Create;
Of all of these solutions, I think the C++ one the most elegant, and I can see why it has been almost universally adopted by successor languages.
If you wanted a constructor to call the constructor of a base class as an initializer, you would need to specify its constructor name. This would be difficult if they were all named the same. e.g.
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
}
The C++ language standard:
12.1. Constructors do not have names.
Your question is based on a confusion. Constructors use special declaration syntax, where class name is used again in place of the member name. I don't see anything wrong with that syntax, so I can't even imagine what would trigger your question (which makes it difficult, if not impossible to answer).
Nevertheless, constructors in C++ have no names. They simply don't need names, because by design there no context in C++ where you would have to reference a constructor.
Actually, it isn't the same. 12.1/1:
Constructors do not have names
The only way to call a constructor is using specific object construction/conversion syntax: it's not found by function name lookup, and you can't take an address of a constructor. This is probably a good thing.
I guess that the syntax for declaring a constructor could have looked something like this:
struct Foo {
int a;
constructor(int a) : a(a) { }
};
But that would have required an extra reserved word, constructor
. It would have meant that the code to declare a constructor looks less like the code to construct an object. The only "benefit" would be to free up "Foo" for use as a member function name. That doesn't sound terribly useful to me, especially since you'd lose "constructor" as a member function name. If there had been any outcry for Foo as a member function name, I guess it could have been achieved differently, for example by using slightly different syntax do declare a constructor (+Foo, to go with ~Foo, maybe?). So I guess that there wasn't.
I can't immediately see any point in having "user-named constructors" in C++. If you want a static member function of Foo which takes certain parameters and returns a Foo, you can declare it like this:
struct Foo {
static Foo bar(int);
};
and "use it as a constructor" like this:
Foo f = Foo::bar(12);
What are the alternatives? Perhaps:
new()
).Both these options seem less desirable than the actuality.
I'm assuming that you are fairly new to C++ and maybe even Object Oriented programming. So, first, I hope you understand what a class constructor is.
Most types will have a constructor of some sort, used to initialize itself. Even when talking about int
s primitives or other types of variable, everything can be initialized². So, when crafting classes, you are asked to provide a initializer, called constructor. Constructors can have parameters which might be obligatory or not. They can also be overloaded, which mean that a class can have many constructors.
Since constructors are initializers the call to them is implicit: a class constructor will be called every time you create a object of that class, be it using the new
keyword or by declaring it on the stack:
CMyObject obj;
Now, they must be declared inside your class. They are methods, after all... so, what should be its name? Python, for example, takes a different approach and uses a keyword, __init__
to do so; C++'s designer decided that its would be the name of the class.
It makes sense since, after all, having a member-method with the name of the class might cause name clashes (ambiguity) along the system. (Even though this series of articles is about C#, it clarifies why using a name to a member of a certain scope which has that same name is bad)
² But sometimes they aren't in order to reduce execution time cost.
The constructor is called every time an object is created and this is usually happens automatically i.e the compiler calls the constructor which is a function call that has no return data type. Therefore, C++ language decided to name this function as the class's name.