views:

113

answers:

3

I am a newbie to c++ and please don't get mad at me for asking something very basic. Here's my quesion:

For a class A, an integer conversion operator would look something like

operator int() //Here we don't specify any return type
{
    return intValue;
}

How does the above function is able to return something when its signature says that it doesnt return anything. Its not void i know, but then what does not specifying a return type mean?

+7  A: 

The return type of operator T() is always T. It's a special case of C++.

It does not use standard function prototype syntax T foo() because 2 functions with the same name differing only by the return type cannot coexist (e.g. int foo() conflicts with double foo()). If this syntax is used then you can only define 1 conversion operator overload, which is undesirable.

KennyTM
+1 for mentioning return type overloads
Idan K
+4  A: 

The return value of operator T() where T is a type is always T.

avakar
+2  A: 

The name of the conversion operator is its type. If this were not the case, you could define an int conversion operator (for example) that actually returned a double. A somewhat similar line of thinking applies to constructors, which also do not have a return type.

anon