|
can anyone tell me why constructors don't return any value
|
|
|
There's would be no way to read a return value if a contructor was allowed to return a value.
A constructor is either implicitly called when you declare a static class object, or called as a result of "new" operatation. In either case, there's no way to receive a return value.
If you need to return a value, you should consider passing in a pointer to a variable which will receive the result, and assigning the value before you exit the contructor. This method will only work for dynamically created objects.
Example:
class MyClass{
public:
MyClass(int *ReturnValue);
~MyClass();
//Other class methods and properties
....
....
....
};
MyClass::MyClass(int *ReturnValue)
{
//Do some stuff
....
....
....
....
*ReturnValue = <some value>
}
//in main code
MyClass *A;
int Result;
A = new MyClass(&Result);
|
|
|
|
|
|
|
|