Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does it work to return an int in a method which should return an object?

Tags:

c++

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;
}
like image 240
Toma Radu-Petrescu Avatar asked Jun 06 '16 17:06

Toma Radu-Petrescu


People also ask

What is meant by returning an object from a method?

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.

What is the return type of a method that does not return any value?

Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do so.

How does return work in Java?

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.

How do you return an integer from a method in Java?

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.

Can We return an integer from a method?

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.

What is the importance of return type in Java?

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).

What are the rules for returning values from a method?

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.

What is a return type in C++?

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.


3 Answers

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.

like image 157
md5i Avatar answered Oct 16 '22 18:10

md5i


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);
like image 33
πάντα ῥεῖ Avatar answered Oct 16 '22 19:10

πάντα ῥεῖ


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.

like image 41
SergeyA Avatar answered Oct 16 '22 17:10

SergeyA