In the following example:
class A
{
public:
class B
{
...
}
B Method(B argument);
}
A::B A::Method(B argument);
Why exactly is the scope required for return type, while not for argument type?
Syntax: object = return object_name; Example: In the above example we can see that the add function does not return any value since its return-type is void. In the following program the add function returns an object of type 'Example'(i.e., class name) whose value is stored in E3.
The return statement returns the flow of the execution to the function from where it is called. This statement does not mandatorily need any conditional statements. As soon as the statement is executed, the flow of the program stops immediately and returns the control from where it was called.
Yes, a class can have a method that returns an instance of itself.
When a function returns a reference, it returns an implicit pointer to its return value. This way, a function can be used on the left side of an assignment statement.
According to [basic.lookup.qual]/3,
In a declaration in which the declarator-id is a qualified-id, names used before the qualified-id being declared are looked up in the defining namespace scope; names following the qualified-id are looked up in the scope of the member’s class or namespace.
The return type comes before the qualified-id being declared (that is, A::Method
) whereas the parameter type comes after it, so the parameter type's name is automatically looked up in the scope of A
, while the return type's name is not. We can avoid the extra qualification using a trailing return type.
auto A::Method(B argument) -> B;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With