如何将用户定义的类型转换为原始类型?

时间:2021-10-03 12:59:28

Let's say we have a class called Complex which represents a complex number. I want to convert this object to a double object.

假设我们有一个叫做Complex的类,它代表一个复数。我想把这个对象转换成一个双对象。

The other way around i can do by implementing a copy ctor in Complex:
Complex(const double &d);

另一种方法是在Complex: Complex(const double &d)中实现一个copy ctor;

However, i can't implement i copy ctor in double which will receive a Complex.

但是,我不能实现双重复制ctor,这会收到一个复杂的。

How do i do this? I know there is a way with operator overloading, but i couldn't find how.
Eventually i want the this line will compile:
Complex c;
(double)c;

我该怎么做呢?我知道有一种操作符重载的方法,但是我找不到方法。最终我希望这行代码可以编译:Complex c;(双)c;

Thanks!!

谢谢! !

6 个解决方案

#1


10  

Implement a conversion operator on your Complex class:

在您的复杂类上实现转换操作符:

class Complex
{
 // ...
  operator double() const
  { 
    double ret = // magic happens here
    return ret;
  }
};

If for whatever reason you don't want to muck about with this, you can provide a global conversion function:

如果出于某种原因,你不想在这个问题上胡闹,你可以提供一个全局转换函数:

double convert_to_double(const Complex& rhs)
{ 
  double ret = // magic happens
  return ret;
}

#2


3  

You mean you want to do this

你是说你想这么做

Complex c;
double d =  c; ?

You can write a conversion operator for that purpose

您可以为此目的编写一个转换操作符

struct Complex{
   double double_val;

   operator double() const {
       return double_val;
   }
};

#3


2  

The proper way of doing this is adding a conversion operator to your class.

正确的做法是向类添加转换操作符。

class myclass {    
public:    
    operator double() const
    {
        return _mydouble;
    }
...
};

and used like this:

和使用:

myclass c;
double d = c; // invokes operator double

#4


2  

The rub with a Complex class is that complex numbers are a superset of real numbers, i.e. while all real numbers are also complex numbers, not all complex numbers are real numbers.

复杂类的难点在于复数是实数的超集,也就是说,虽然所有的实数也是复数,但并不是所有的复数都是实数。

So while, as others pointed out, you need to define a typecast operator in Complex for double, the real work is what you put into that function. The safest thing would be to throw an exception if the number isn't real:

因此,正如其他人指出的,您需要在Complex中为double定义一个typecast运算符,而真正的工作是您在该函数中输入的内容。如果这个数字不是真实的,最安全的方法就是抛出一个异常:

#include <exception>

struct Complex
{
    double real;
    double imag;

    Complex(double real_, double imag_ = 0.0): real(real_), imag(imag_) {)

    // ...

    class not_real: public exception
    {
        virtual const char* what() const throw()
        {
            return "cannot cast non-real complex number to double";
        }
    };

    operator double() const {
        if(imag != 0.0)
            throw not_real();
        return real;
    }
};

#5


0  

operator double is what you need

你需要的是接线员的双重电话

class Complex
{
   operator double() const {....}
}

#6


0  

You can create a custom cast operator: - http://msdn.microsoft.com/en-us/library/ts48df3y%28v=VS.100%29.aspx - Operator-Overloading/doublecastoperator.htm">http://www.java2s.com/Tutorial/Cpp/0200_Operator-Overloading/doublecastoperator.htm

您可以创建一个自定义强制转换操作符:- http://msdn.microsoft.com/en-us/library/ts48df3y%28v=VS.100%29.aspx -操作符重载/doublecastoperator.htm

So your code would look something like this (within your Complex class): operator double () { return /* whatever you want to do to the complex */ }

所以你的代码看起来是这样的(在你的复杂类中):操作符double () {return /*你想对复杂的*/}做什么

#1


10  

Implement a conversion operator on your Complex class:

在您的复杂类上实现转换操作符:

class Complex
{
 // ...
  operator double() const
  { 
    double ret = // magic happens here
    return ret;
  }
};

If for whatever reason you don't want to muck about with this, you can provide a global conversion function:

如果出于某种原因,你不想在这个问题上胡闹,你可以提供一个全局转换函数:

double convert_to_double(const Complex& rhs)
{ 
  double ret = // magic happens
  return ret;
}

#2


3  

You mean you want to do this

你是说你想这么做

Complex c;
double d =  c; ?

You can write a conversion operator for that purpose

您可以为此目的编写一个转换操作符

struct Complex{
   double double_val;

   operator double() const {
       return double_val;
   }
};

#3


2  

The proper way of doing this is adding a conversion operator to your class.

正确的做法是向类添加转换操作符。

class myclass {    
public:    
    operator double() const
    {
        return _mydouble;
    }
...
};

and used like this:

和使用:

myclass c;
double d = c; // invokes operator double

#4


2  

The rub with a Complex class is that complex numbers are a superset of real numbers, i.e. while all real numbers are also complex numbers, not all complex numbers are real numbers.

复杂类的难点在于复数是实数的超集,也就是说,虽然所有的实数也是复数,但并不是所有的复数都是实数。

So while, as others pointed out, you need to define a typecast operator in Complex for double, the real work is what you put into that function. The safest thing would be to throw an exception if the number isn't real:

因此,正如其他人指出的,您需要在Complex中为double定义一个typecast运算符,而真正的工作是您在该函数中输入的内容。如果这个数字不是真实的,最安全的方法就是抛出一个异常:

#include <exception>

struct Complex
{
    double real;
    double imag;

    Complex(double real_, double imag_ = 0.0): real(real_), imag(imag_) {)

    // ...

    class not_real: public exception
    {
        virtual const char* what() const throw()
        {
            return "cannot cast non-real complex number to double";
        }
    };

    operator double() const {
        if(imag != 0.0)
            throw not_real();
        return real;
    }
};

#5


0  

operator double is what you need

你需要的是接线员的双重电话

class Complex
{
   operator double() const {....}
}

#6


0  

You can create a custom cast operator: - http://msdn.microsoft.com/en-us/library/ts48df3y%28v=VS.100%29.aspx - Operator-Overloading/doublecastoperator.htm">http://www.java2s.com/Tutorial/Cpp/0200_Operator-Overloading/doublecastoperator.htm

您可以创建一个自定义强制转换操作符:- http://msdn.microsoft.com/en-us/library/ts48df3y%28v=VS.100%29.aspx -操作符重载/doublecastoperator.htm

So your code would look something like this (within your Complex class): operator double () { return /* whatever you want to do to the complex */ }

所以你的代码看起来是这样的(在你的复杂类中):操作符double () {return /*你想对复杂的*/}做什么