0

I would like to convert object to double value. It looks following -> ClassA clsA(1,2,3);

double result = clsA;

I wrote sth like that in my header file:

const double operator= (const ClassA&);

And implemented in cpp file:

const double ClassA::operator= (const ClassA& a) {
    /* here I made some math calculation on instance 'a' and the result is double value *\
   return doubleValue; 
}

But It won't work, I don't know if it's good idea to make it, also I use operator = to assign one object to another example -> objA = objB, so I don't know if it can argue together with above implementation.

Thanks for help!

lory
  • 11
  • 2
  • `ClassA::operator=(const ClassA&)` is used to assign a (const-reference) `ClassA` to a `ClassA`, the return type should be `ClassA&`, e.g. `ClassA a, b; a = b;`. What you want is a conversion operator to double, so something like `operator double() const;` inside `ClassA`. See e.g. http://en.cppreference.com/w/cpp/language/cast_operator. – Holt Dec 25 '17 at 22:50
  • @Holt Can you write here in comment possible implementation ? I don't know how to start it ? – lory Dec 25 '17 at 22:53
  • @Holt and how to 'tell' this to take object which is after '=' and value which returns assign to double value = objA; ? – lory Dec 25 '17 at 22:56
  • You don't need to overload the assignment operator, the assignment operator can only overload for the type assigned to, and thus you cannot overload it for `double`. If you provide a conversion operator to `double` (see the link in my previous comment), your object of type `ClassA` will be converted to a `double` before being assigned to one. – Holt Dec 25 '17 at 22:58
  • @Holt I still don't know where to provide some math logic in function which return double value and takes object parameters. Can you write simple code how to implement it , pls ? – lory Dec 25 '17 at 23:07
  • And in result only what I can is to -> double resultValue = objA; – lory Dec 25 '17 at 23:09
  • Strongly recommend against putting sith in your header. You can't trust those darn Darths. Also recommend giving [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) a read for some good advice on how to do operator overloads of just about every flavour. – user4581301 Dec 25 '17 at 23:17

1 Answers1

2

You want to write a conversion (or cast) operator:

// integer division/multiplication
// computes (num_*fac_)/div_
struct A
{
    int num_, fac_, div_;
    A (int num, int fac, int div) : num_(num), fac_(fac), div_(div) {};

    // convert to double 
    operator double() const { return double(num_ * fac_) / double(div_); }
};

int main()
{
    A a(1, 2, 3);
    volatile double f = a;
    return f;
}

If you want to force the caller to make an explicit cast to double (to reduce the risk of accidental implicit casts to double, make the operator explicit.

explicit operator double() const { return double(num_ * fac_) / double(div_); 

To force the cast, use static_cast

double f = static_cast<double>(a);
Michaël Roy
  • 6,338
  • 1
  • 15
  • 19
  • Ok, works but why if I have two objecta assigned -> objA = objB , it gives me an error: use of overloaded operator '*' is ambigous (with operand types 'ClassA' and 'int') obj3 = obj1 * 2; built-in candidate operator*(double, int) – lory Dec 25 '17 at 23:25
  • `objA = objB` has nothing to do with floats. Do not define anything, and use the default copy constructor and operator the compiler provides for free, unless you have hand-managed resources. – Michaël Roy Dec 25 '17 at 23:30
  • If I added explicit to operator it gives me -> error: explicit conversation functions are a C++11 extension – lory Dec 25 '17 at 23:31
  • Then you can't use explicit yet. – Michaël Roy Dec 25 '17 at 23:32
  • Errors on another compiler -> main.cpp:18: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second: ClassA.h:35: note: candidate 1: const ClassA operator*(const ClassA&, float) main.cpp:18: note: candidate 2: operator*(double, int) – lory Dec 25 '17 at 23:35
  • That can't be for this line of code: `double result = clsA;` – Michaël Roy Dec 25 '17 at 23:36
  • Even without using explicit, you can use static_cast to eliminate ambiguities. – Michaël Roy Dec 25 '17 at 23:37
  • Yes, but this error occurs only when I add this conversion type which you write for me – lory Dec 25 '17 at 23:38
  • Nobody sees your code, including me. The code in the answer compiles and runs fine. – Michaël Roy Dec 25 '17 at 23:42
  • ok, but only how do you know this error -> ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second: ClassA.h:35: note: candidate 1: const ClassA operator*(const ClassA&, float) main.cpp:18: note: candidate 2: operator*(double, int) – lory Dec 25 '17 at 23:45
  • 2 is _not_ a double. it is an int. That's where the compiler loses you. – Michaël Roy Dec 25 '17 at 23:48
  • if i remove operator double() const; it works fine, but if I add it, it shows error – lory Dec 25 '17 at 23:49
  • Like I said. I cannot guess what your code looks like. – Michaël Roy Dec 25 '17 at 23:52
  • Seems you have operator * defined for ClassA and float (the first candidate). If so, remove it (either that or provide another one for int). And it has nothing to do with the conversion operator, it is only failing when you add one because of another completely different mistake in your code. – EmDroid Dec 26 '17 at 01:14