6

I have been using C++ and Java for several years now. One thing which I can't seem to understand is that why do we need to provide constructors of a class a name? For instance, if I have to define a class FOO in C++/Java, I'll be forced to provide FOO as the constructor name. However, since constructor is never explicitly called, what is the sense in compiler forcing me to provide it a name after all.

The abstraction paradigm dictates, we hide unnecessary details from programmers. This is the reason, constructors don't have a return type, since it's already well-defined what a constructor has to return. In the same spirit, why can't we just give a generic name to constructors of all classes - for instance anything meaningful, like initialize() or maybe just nothing and just arguments ( [arg [,arg]] )

I hope, I'm able to express myself. If someone have any definitive answers, kindly let me know.

4

8 回答 8

3

In C++ constructors do not have names(C++03 12.1), however since constructors are essentially defined as functions, it was logical to name them in some way.
Naming them anything other than the class name would have added new keywords and hence eventually they were named same as the class name.
In short, It was a logical decision which avoided new keywords and at the same time ensured intuitiveness.

于 2012-06-05T05:17:49.420 回答
2

From the C++ standard (12.1) (emphasis mine):

Constructors do not have names. A special declarator syntax is used to declare or define the constructor. The syntax uses:

  • an optional decl-specifier-seq in which each decl-specifier is either a function-specifier or constexpr,
  • the constructor’s class name, and
  • a parameter list

In C++, you are not providing a name, you are writing special syntax which was decided by the language creators to declare a constructor.

于 2012-06-05T05:11:05.140 回答
1

it's a standard for the language, if you want to have a generic name for constructor then try to learn another language or maybe create your own language. =p

于 2012-06-05T04:58:17.093 回答
1

For Java and C++, the constructor name must be the same as the class name. Besides, you know it is a constructor, because it does not declare a return type. This way to declare a constructor helps you to declare a method which name is __init__, initialize, or __constructor. This is not possible in Python, for example, because it uses __init__ as constructor name.

于 2012-06-05T05:26:01.757 回答
0

Constructors are special member function and constructor always returns the memory address created by 'new' operator. Since one function returns only one thing at a time so no other value can return from constructor. It is the java standard to define a constructor name same as the class name.

于 2012-06-05T05:04:24.723 回答
0

First, you don't have to write a constructor. if you don't write one - the compiler will create a default constructor for you.

second, sometimes you do have to use one (for example, if you have final class variables they have to be initialized upon creation of the object). In this case you have to write a constructor and you will use it when you instantiate the object.

public class Foo {
    final int a;

    public Foo (int val){
        this.a = val
    }
}

Foo foo = new Foo (23);
于 2012-06-05T05:11:38.657 回答
0

You are right about your line of thought. The syntax Java adopted is just a historical artifact.

In newer languages that support OOP, this redundancy is generally absent. Scala calls it this*, F# calls it new*, Python calls it __init__, Ruby calls it initialize etc.


* Auxiliary constructors anyway. The primary constructors in these languages are class arguments themselves.

于 2012-06-05T06:10:31.797 回答
-2

since you can't call the constructor explicitly, what's the purpose of giving a special name to it? and about constuctors having a name same with the class: constructing can only be performed in a function so constructor is a function. and since it's a special function, among other class functions it should have a special signature. language designers wanted them to be named same with the class.

于 2012-06-05T04:54:29.417 回答