Why does it work to return an int
in the method B minus
if the method is supposed to return an object of type B
?
#include <iostream>
class B
{
public:
int a;
public:
B(int i=0)
{
a=i;
}
B minus()
{
return (1-a);
}
};
int main()
{
B x(18);
x = x.minus();
std::cout << x.a << '\n';
return 0;
}
It would mean make a copy and return it. The difference is that if you return pointer to objects internal variable that object state could be modified from outside. If you return copy that copy can be modified and the original object will not change.
Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do so.
What is a return statement in Java? In Java programming, the return statement is used for returning a value when the execution of the block is completed. The return statement inside a loop will cause the loop to break and further statements will be ignored by the compiler.
Java returngetArea(int x, int y) → The method getArea() takes two parameters x and y of type int. int getArea(int x, int y) → Writing int before the method name means that the method returns an integer value.
For instance, if the return type of some method is boolean, we can not return an integer. The variable receiving the value returned by a method must also be compatible with the return type specified for the method. The parameters can be passed in a sequence and they must be accepted by the method in the same sequence.
Importance of return type in Java? A return statement causes the program control to transfer back to the caller of a method. Every method in Java is declared with a return type and it is mandatory for all java methods. A return type may be a primitive type like i nt, float, double, a reference type or void type (returns nothing).
There are a few important things to understand about returning the values The type of data returned by a method must be compatible with the return type specified by the method. For instance, if the return type of some method is boolean, we can not return an integer.
A return type may be a primitive type like int, float, double, a reference type or void type(returns nothing). There are a few important things to understand about returning the values The type of data returned by a method must be compatible with the return type specified by the method.
A constructor with a single argument is considered a converting constructor. When an argument of type X is needed, and a type Y is given instead, the compiler will look for a converting constructor (or a type conversion operator) from Y to X. In this case it finds one, using your B(int)
constructor. In essence, your return (1-a);
is changed to return B(1-a);
.
As mentioned in several other (also correct) answers, if you do not wish the constructor to be considered a converting constructor, you should preface it with the explicit
keyword.
The line
return (1-a);
calls the implicit conversion constructor
B(int i=0)
{
a=i;
}
So it's the same as writing
return B(1-a);
Note that the copy constructor is still generated, unless you delete
it.
If you want to avoid that, write
explicit B(int i=0)
// ^^^^^^^^
{
a=i;
}
this will actually force a user to write
return B(1-a);
This is happening because single-argument constructor can be used as an implicit cast from argument type to object type.
In your case, you have a constructor which accepts an argument of type int
, so this constructor can be used to convert int
into B
.
To prevent those constructors to be used in conversion, you should mark constructor explicit
- and it's a good practice to do so for all single-argument constructors, since in practice those implicit conversions are more often undesired than desired.
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