Automatically converting object to double

J

Joe Van Dyk

I have a class that is a wrapper around a double. Is there a way to
automatically convert an object of that class to a double whenever a
double is wanted?

Joe
 
L

Luc The Perverse

Joe Van Dyk said:
I have a class that is a wrapper around a double. Is there a way to
automatically convert an object of that class to a double whenever a
double is wanted?

Joe

operator double();

;)
 
F

Frederick Gotham

Joe Van Dyk posted:
I have a class that is a wrapper around a double. Is there a way to
automatically convert an object of that class to a double whenever a
double is wanted?

Joe

class MyDouble {
private:

double obj;

public:

MyDouble(double const arg = 0) : obj(arg) {}

operator double &() { return obj; }

operator double const &() const { return obj; }
};

void FuncWantsDoubleByVal(double) {}
void FuncWantsDoubleByRef(double&) {}

int main()
{
MyDouble a;

MyDouble b = 56.3;

a += b;

b *= a;

a = 73.4;

b = 4;

FuncWantsDoubleByVal(a);

FuncWantsDoubleByRef(b);
}
 
J

Joe Van Dyk

I have a class that is a wrapper around a double. Is there a way to
automatically convert an object of that class to a double whenever a
double is wanted?

Joe

Aha.

MyClass::eek:perator double()

Sort of weird.

Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top