0

I am doing some workshop for C++ and I am at loss on cast operator conversion. I am trying to display an double value by using object's data.

class object {
    int x;
    int y;
  public:
    operator double();
}

the workshop is to take the result of x / y and display it as double. so if I were to display it by line like

std::cout << (double)A

assuming x is 3 and y is 2 the value should display as 1.5. I am not sure how to make the definition of this, what I have is something like this

Object::operator double() {
    double dbl = 0;
    dbl = this->x / this->y
    return dbl;
}

Now this is obviously wrong because my x and y values are random and not obtaining the values of the object A. I am not sure how to pass the data into the function. Any help would be appreciated

OnesQuared
  • 79
  • 6
  • I don't see why you think you need to pass data into the function; the function already has access to the members of the `object` instance it's operating on. (and, incidentally, you don't have to do `this->x` to reference it; you just need `x`) –  Feb 23 '17 at 23:15
  • 2
    (p.s. you want floating point division not integer division, so you have to convert to `double` before you divide, not after) –  Feb 23 '17 at 23:16
  • 1
    Just cast one of the values to a double in the division. This strikes me as a remarkably crappy exercise, so you may want to learn C++ from some other source, preferably a good textbook. –  Feb 23 '17 at 23:17
  • You should not be using a conversion type here. The conversion to double is ambiguous (looking at the code) since there are many operations you can perform with `x` and `y` that would result in a `double`. Use a more descriptive function, e.g. `ratio`. – Thomas Matthews Feb 23 '17 at 23:29
  • @Thomas: The problem is too generic to really say such a thing confidently; e.g. if this were a class representing a rational number, then the given conversion to `double` would be a very natural thing. –  Feb 23 '17 at 23:35
  • `my x and y values are random and not obtaining the values of the object A` We probably need to see your actual program to figure out why you are getting *random* values. Perhaps something is not initialized. – Fantastic Mr Fox Feb 24 '17 at 00:02
  • Please copy and paste real code - your `operator double` as posted won't compile – M.M Feb 24 '17 at 03:56
  • You are more likely to get a useful answer if you follow the guidelines in [ask] and post a [mcve]. Right now we can't see how you are declaring, initialising or changing the variable A before you call the double operator. I suspect your `x` and `y` values are random because of the way you create/initialise `A` not because of problems in your `operator double ()` but I can only guess because we can't see all of your code. – Frank Boyne Feb 24 '17 at 21:36
  • 1
    @TankorSmash `operator double ()` is a [user-defined conversion](http://en.cppreference.com/w/cpp/language/cast_operator) to the type `double`. – Frank Boyne Feb 24 '17 at 21:57

1 Answers1

0

the workshop is to take the result of x / y and display it as double. so if I were to display it by line like

std::cout << (double)A

assuming x is 3 and y is 2 the value should display as 1.5.

For that particular example, you should define a custom operator<<, eg:

std::ostream& operator<<(std::ostream &out, const object &value)
{
    out << (double) value;
    return out;
}

Then you can pass the object directly to cout (or any other ostream descendant):

std::cout << A;

I am not sure how to make the definition of this, what I have is something like this

Object::operator double() {
    double dbl = 0;
    dbl = this->x / this->y
    return dbl;
}

What you have is fine, as far as declaring a conversion operator. Though you should declare it as const, at least, since it does not modify any members of object. Just be sure to watch out for y=0, since divide by 0 is an error. And note that you are performing integer division instead of floating-point division, so you need to cast one of the values to double before dividing.

Now this is obviously wrong because my x and y values are random and not obtaining the values of the object A.

If you are getting random results, it means your input is random to begin with (ie, you are likely not initializing your x and y variables). In the object declaration you have shown, x and y are private, and you don't provide any constructor or setter method(s) to set their values.

Try something more like this instead:

class object {
    int x;
    int y;
public:
    object();
    object(int aX, int aY);

    int getX() const;
    void setX(int value);

    int getY() const;
    void setY(int value);

    operator double() const;
};

std::ostream& operator<<(std::ostream &out, const object &value);

#include <limits>

object::object()
    : x(0), y(0)
{
}

object::object(int aX, int aY)
    : x(aX), y(aY)
{
}

int object::getX() const
{
    return x;
}

void object::setX(int value)
{
    x = value;
}

int object::getY() const
{
    return y;
}

void object::setY(int value)
{
    y = value;
}

object::operator double() const
{
    if (y != 0)
        return (double)x / (double)y;
    return std::numeric_limits<double>::quiet_NaN();
}

std::ostream& operator<<(std::ostream &out, const object &value)
{
    out << (double) value;
    return out;
}

Then you can do things like this:

object A(3, 2);
std::cout << A;

object A;
A.setX(3);
A.setY(2);
std::cout << A;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • A custom `operator <<` makes things too complicated. The `operator double()` will convert an instance of the `A` class into a `double` and `std::basic_ostream` supports a [suitable `operator <<`](http://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt) to take a `double` parameter. – Frank Boyne Feb 24 '17 at 21:28