Reputation: 7491
I have a function defined as
ClassType f(int i) {
if (...) return NULL;
...
ClassType obj;
return obj;
}
I am curious why a "NULL" could be legally accepted when a class type is required as return type? I think NULL is usually defined by Macro as a synonym of '0'. Could someone explain the rationale behind this? Thanks!
update:
The class is defined as:
class ClassType {
public:
ClassType() {};
ClassType(char* s) {...};
...
}
One more question, Does c++ do implicit conversion when return by value?
Thanks!
Upvotes: 1
Views: 150
Reputation: 55395
class ClassType {
public:
ClassType() {};
ClassType(char* s) {...}; // <<-----
}
The constructor that I marked is used when you say return NULL;
. The value is implicitly convertible to char*
(NULL
is usualy defined as 0
and that's a valid pointer value).
One more question, Does c++ do implicit conversion when returning a value?
Yes, as it has to construct an object of suitable type.
Upvotes: 3
Reputation: 39089
Not accurately an answer to your question, but in general, to indicate optional return values, consider the following.
For cases where an optional return value makes sense, you might look into boost::optional
, and C++14 will possibly define std::optional
(based on the boost-library).
Additional return values in diguise, like bool
s indicating success, often come at the burden that you cannot initialize an object only in case you can, and if you want to return an optional value for which there is no default construction, you either can't, or you have to pass around reference-pointers, and then have to check pointer at the called site, etc.:
std::optional<Intensity> sample_spectrum (float); // clean and concise
-------------------------------------------------------------------------
bool sample_spectrum (Intensity &out); // either not possible or you
// have a wasted initialization
-------------------------------------------------------------------------
bool sample_spectrum (Intensity *& out) { // now you have two problems
if (!out) throw std::logic_error(""); // I
....
}
....
try { .... } catch (...) { .... } // II
-------------------------------------------------------------------------
Intensity *sample_spectrum(float) ; // please no
-------------------------------------------------------------------------
std::shared_ptr<Intensity>
sample_spectrum(float); // just use std/boost::optional
Upvotes: 0
Reputation: 6505
Null is a macro that is defined with 0
which is an integer value. If the ClassType
has a constructor that accepts integers,or pointers then the compiler will automatically convert the integer 0 to ClassType
:
class ClassType
{
public:
ClassType(const int);
};
If the class doens't have a constructor that can take 0 then the compiler will throw an error.
Upvotes: 1
Reputation: 409166
To begin with, in this case you can return NULL
, as the compiler will then use the ClassType(char* s)
constructor with NULL
passed as the constructor argument.
But generally, for cases like these you might want to pass the object as a reference argument to the function, and then return boolean true
or false
is the function was successful or not.
Upvotes: 1