answersLogoWhite

0


Best Answer

Constructors cannot have return types. The main job of the constructor is to create new instance of its class and return that instance. So the default return type of all constructor is the object of its class.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

No. It would make no sense to return anything from a constructor (not even void) because constructors cannot be called directly from within your code.

When an object is constructed, whether statically allocated upon the stack at compile time or dynamically allocated upon the heap at runtime, memory must first be set aside for it and this mustoccur before the constructor is called. In the case of static variables, the memory is guaranteed to exist provided your program's data segment is large enough to accommodate an instance of your object (which can easily be determined at compile time, resulting in a compile time error upon failure). In the case of dynamic variables, however, there is no guarantee an allocation will succeed every time. Either way, if an allocation should fail for any reason, the constructor cannot be called because the object must exist in memory in order for the constructor to be able to initialise the memory allocated to it. Bear in mind that every instance of an object has an implicit this pointer that must exist at the point of construction. The only way that can be achieved is if memory is physically allocated to the object prior to the constructor being called. This allocation is transparent to the programmer because it is done automatically, Behind the Scenes.

Consider what happens when you instantiate an object dynamically. We'll use the following class declaration as an example:

class foo {

private: int m_data;

public: foo():m_data(0){}

};

To create an instance of this class dynamically, you would use the following line within your code:

foo* F = new foo();

Although it may look like you are calling the default constructor directly, you are actually calling the newoperator directly. The new operator expects you to pass the address of the required constructor as its operand, including any arguments required by that constructor. Thus you are effectively calling the constructor through the new operator.

The new operator first allocates enough memory to accommodate an instance of the class (including a v-table if it has one). In this case, the class only has one member variable of type int, thus sizeof(int) bytes are allocated to the entire class. If the class has more member variables, the allocated memory is divided up and assigned to each member variable, according to the order they were declared within the class declaration, including any padding for alignment purposes.

If the allocation fails for any reason, the new operator simply returns a NULL pointer which is then assigned to the F variable. Thus we need only ensure F is non-NULL to be assured we have a valid object. If F is NULL, then the object was never created and therefore the constructor was never called.

Assuming the allocation was successful, control immediately passes to the address that was passed to it: in this case the address of the default constructor. The default constructor subsequently initialises the member variable, foo::m_data by calling the int default constructor.

As an aside, initialising objects, including primitives, via a constructor's initialisation list is far more efficient than initialising them from the constructor body using assignment statements. This is because the default constructors of all member variables will be called implicitly unless you specify otherwise via the initialisation section. If you choose to initialise by assignment then you will effectively initialise the members twice: once via the default constructor and again via assignment. In the case of complex objects with many members to initialise, this can prove to be quite costly. Therefore get into the habit of always initialising though construction whenever possible. Note also that the initialisation section makes it possible to call specific base class constructors rather than their default constructors (thus a class' copy constructor can call its base class' copy constructor directly).

In this case the body has no code, so construction is complete once foo::m_data is initialised. The new operator then regains control and returns a pointer of type foo containing the starting address of our newly-constructed object, the value of which is subsequently assigned to the variable F.

If the constructor could return a value, then it would be completely lost to us by now. Remember that we didn't call the constructor directly, the new operator did it for us. Thus if the constructor could return a value, then it would have been received by the new operator, not our code.

Note that 100% OOP languages that do not support primitive pointers will throw exceptions during dynamic construction to indicate failures. But since C++ is not 100% OOP, we need only check the returned pointer is non-NULL before we use a dynamic object.

Statically allocated objects work exactly the same way, the only real difference being that memory is allocated and initialised at compile time rather than at runtime. Again, if the constructor could return a value, it would be completely lost to us because we never called the constructor directly, the compiler did it for us. The following is an example of static construction:

foo f; // statically allocated object constructed at compile time

Note that this is no different to declaring a static primitive such as int:

int i;

Although C++ allows int variables to be constructed using construction syntax, this is merely sugar-coating to allow primitives to behave in exactly the same way as objects:

int i = int(5);

In the above example, you are not actually calling a constructor since there is none to call. The compiler will simply replace the code with the equivalent primitive declaration:

int i = 5;

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

You can think of the constructor as returning a new object of its class type. So you can think of the constructor for the Point class to have a return type of Point.

Just keep in mind that constructors are special types of methods which follow some special rules.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Because, they are not methods to have a return type. The constructor by default creates an object of the class and that's all. It does not return any value, hence it has no return type.

The above hints at the reason why constructors don't have a declared return type, but let's make it explicit:

Constructor methods do have a return type. The return type is an Object of the type of the Class of the constructor.The reason this is not put in explicitly is because it CANNOT BE CHANGED - that is, a constructor, by definition, can only create an object of the class it is in. Thus, the return type is implied as the Class of the constructor method.

The purpose of a constructor is to create an instance of that Class, so letting a user change that return type is useless (and harmful). Since the return type cannot be changed, why require it to be provided (as that only allows for mistakes)?

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

No Way... The default return type of a constructor is the object of the same class. You cannot make a constructor explicitly return any other data type.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

No, constructors don't return anything.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why constructor does not have return type?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is return data type?

A Constructor in java cannot have a return type. It always creates and returns an object of the class for which it is the constructor. You cannot return a value from a constructor explicitly and if you try to do that, the compiler will give an error. The system knows that the purpose of the constructor is to create an object of the class and it will do the same irrespective of whether you declare a return type or not.


Why constructor in Java doesn't have any return type?

The constructor of a Java class is not an ordinary method. Its purpose is not to return any value. The purpose of the constructor is to instantiate the class which it does. Since, the purpose of a constructor is only to instantiate and initialize its class and not anything else, it does not have a return type. All it does is creates an object of that class.


What is the difference between the constructor to and destructor?

Functions and Constructors are similar in many ways. They can have arguments, they can have any amount of code, they can access the class's variables etc. the only difference is that a method in java needs to mandatorily have a return type but a Constructor in java cannot have a return type. It always creates and returns an object of the class for which it is the constructor. You cannot return a value from a constructor explicitly and if you try to do that, the compiler will give an error. The system knows that the purpose of the constructor is to create an object of the class and it will do the same irrespective of whether you declare a return type or not.


Why you don't use void in constructor function although it doesn't return any value?

Return a value in the constructor is senseless, because a constructor is used to initialize a object instance and not to perform a task or a operation. When we call a constructor we used a sentence like this: MyClass var = new MyClass(); Then when we execute the above line the constructor return ('create') a new object of the type 'MyClass'. If this call could return an other type, for example an Integer, the constructor is considered a normal method, and if there are not more constructors a empty default constructor for MyClass is defined by the java compiler. public class MyClass{ // The java compiler will insert a real constructor here public Integer MyClass(){ //This isn't a constructor, only a simple method return new Integer(1); } }


What are the similarities between constructor overloading and function overloading?

The only similarity is that both constructor and function overloads are distinguished by their signature -- the number and type of their arguments. Functions differ in that they also have a return type, which is also part of the signature, whereas constructors have no return type, not even void.

Related questions

What is return data type?

A Constructor in java cannot have a return type. It always creates and returns an object of the class for which it is the constructor. You cannot return a value from a constructor explicitly and if you try to do that, the compiler will give an error. The system knows that the purpose of the constructor is to create an object of the class and it will do the same irrespective of whether you declare a return type or not.


Why constructor in Java doesn't have any return type?

The constructor of a Java class is not an ordinary method. Its purpose is not to return any value. The purpose of the constructor is to instantiate the class which it does. Since, the purpose of a constructor is only to instantiate and initialize its class and not anything else, it does not have a return type. All it does is creates an object of that class.


What is the difference between the constructor to and destructor?

Functions and Constructors are similar in many ways. They can have arguments, they can have any amount of code, they can access the class's variables etc. the only difference is that a method in java needs to mandatorily have a return type but a Constructor in java cannot have a return type. It always creates and returns an object of the class for which it is the constructor. You cannot return a value from a constructor explicitly and if you try to do that, the compiler will give an error. The system knows that the purpose of the constructor is to create an object of the class and it will do the same irrespective of whether you declare a return type or not.


Why you don't use void in constructor function although it doesn't return any value?

Return a value in the constructor is senseless, because a constructor is used to initialize a object instance and not to perform a task or a operation. When we call a constructor we used a sentence like this: MyClass var = new MyClass(); Then when we execute the above line the constructor return ('create') a new object of the type 'MyClass'. If this call could return an other type, for example an Integer, the constructor is considered a normal method, and if there are not more constructors a empty default constructor for MyClass is defined by the java compiler. public class MyClass{ // The java compiler will insert a real constructor here public Integer MyClass(){ //This isn't a constructor, only a simple method return new Integer(1); } }


How can you recognize a constructor in a class?

A class's constructor will have the same name of the class and no return type (not even void): class Example(){ Example() {printf("This is the constructor\n");} ~Example(){printf("This is the destructor\n");} };


What are the similarities between constructor overloading and function overloading?

The only similarity is that both constructor and function overloads are distinguished by their signature -- the number and type of their arguments. Functions differ in that they also have a return type, which is also part of the signature, whereas constructors have no return type, not even void.


What is a nonparameterised constructor in java?

Every class, including abstract classes, MUST have a constructor. But just because a class must have one, doesn't mean the programmer has to type it. A constructor looks like this: class Car { Car() { } // The constructor for the Car class } You notice anything missing in the declaration above? There's no return type! Two key points to remember about constructors are that they have no return type and their names must exactly match the class name. The above car() is the non-parameterised or no-arg constructor for the class.


What are the rules on how to construct java constructor?

1. The constructor has to have the same name as the classthat it is in.2. It does not have a return type. If it has a return type, then it is a method (even though it is legal, it's not ideal to have name a method the same name as the class).3. It can use any access modifier (this includes private).4. The default constructor does not take arguments.5. The first statement in a constructor has to have a super() type or this() type. If this is not written, by default, it's super(). It's illegal to have it in any other line other than the first line.6. Constructors can only access static variables.7. Only constructors have access to another constructor.Remember that interfaces do not have a constructor.


How constructor is different from normal member function?

A constructor differs from a normal member function in three ways:A constructor never returns a result. The constructor's declaration reflects this by not even declaring the function as "void." The common design hypothesis is that a well-designed constructor cannot fail, other than maybe in an irrecoverable way (such as a fatal running out of memory).A constructor is never called explicitly except with the new operator.Constructors impose further restrictions. For example, they cannot be declared abstract or virtual, and may have visibility requirements. The common design practise is that at least the default constructor is declared public.


What is the difference between consructor and function in java?

Functions and Constructors are similar in many ways. They can have arguments, they can have any amount of code, they can access the class's variables etc. the only difference is that a method in java needs to mandatorily have a return type but a Constructor in java cannot have a return type. It always creates and returns an object of the class for which it is the constructor. You cannot return a value from a constructor explicitly and if you try to do that, the compiler will give an error. The system knows that the purpose of the constructor is to create an object of the class and it will do the same irrespective of whether you declare a return type or not.


True or False A C plus plus class constructor cannot return a function value?

True - A C++ constructor cannot return a value.


What are the properties of class in c plus plus?

A constructor is not a function. A function is a type, as specified by its return type, and must return a value of that type unless the type is void. A constructor does not return anything, not even void. The purpose of a constructor is to both allocate and initialise memory for an object of the type being constructed. If a valid object cannot be constructed for any reason, the constructor must throw an exception. If the object's class has no data members (attributes), the class does not require a constructor. This is typically the case for most abstract data types and base classes which are used purely as interfaces. Constructors differ from functions in that all constructors have an initialisation section that is used specifically to initialise non-static data members. The body of the constructor is rarely used except to perform initialisations that cannot be more easily performed by the initialisation section. A class may have more than one constructor to provide alternative methods of construction based upon the number and type of arguments supplied (if any). When no arguments are required or all arguments have default values then the constructor is known as the default constructor. If the constructor has only one argument the constructor is known as a conversion constructor (because the argument is converted to an object of the class). However, if the constructor argument is a constant reference to an object of the same class, then it is known as a copy constructor, and when the constructor argument is an rvalue reference, it is known as a move constructor. If copy and/or move constructors are provided for a class, the equivalent assignment operators should also be provided for that class. All other constructors are known as user-defined constructors.