简体   繁体   English

为类对象指定“返回类型”

[英]Specify “return type” for class object

I have the following class, which is supposed to represent an 8 bit signed char. 我有以下课程,应该代表一个8位带符号的char。

class S8
{
private:
    signed char val;
public:
    S8 & operator=(const signed char other)
    {
        if ((void*)this != (void*)&other)
        {
            val = other;
        }
        return *this;
    }
    operator signed char() {signed char i; i = (signed char) val; return i;}
    void write (OutputArray & w)
    {
        /* This function is the whole purpose of this class, but not this question */
    }
};

However, when I assign a negative number to one of its objects, 但是,当我为其对象之一分配负数时,

S8 s;
char c;

s = -4;
c = -4;

printf("Results: %d, %s\n",s,c);

I get "Results: 252, -4" from the printf. 我从printf得到“结果:252,-4”。 Is there any way to modify the class such that cases like this will see the behaviour of a signed char, rather than the unsigned char behaviour I am getting? 有什么方法可以修改该类,以便此类情况将看到签名字符的行为,而不是我得到的未签名字符行为?

Thanks! 谢谢!

What you want is an implicit conversion from a signed char to an S8 object; 您想要的是从签名的字符到S8对象的隐式转换。 and this is accomplished with a non-default copy-constructor. 这是通过非默认的复制构造函数完成的。 If a copy-constructor taking a signed char is defined, then compilers will use this for implicit conversions (assuming the copy-constructor is not defined as "explicit"). 如果定义了一个带签名char的复制构造函数,则编译器将使用它进行隐式转换(假定该复制构造函数未定义为“显式”)。 So, for your example: 因此,以您的示例为例:

class S8
{
private:
    signed char val;
public:
    //default constructor
    S8() : val(0) {}

    //default copy-constructor
    S8(const S8& rhs) : val(rhs.val) {}

    //allow implicit conversions (non-default copy constructor)
    S8(const signed char rhs) : val(rhs) {}

    //allow implicit conversions
    operator signed char() { return val; }
};    

int main()
{
  S8 s;
  signed char c;

  s = -4;
  c = -4;

  std::cout << (int) s << std::endl;
  std::cout << (int) c << std::endl;

  return 0;
}

Your code makes no sense at all, while the behavior you experience is completely arbitrary as printf must not be used that way. 您的代码完全没有意义,而您遇到的行为是完全任意的,因为绝对不能以这种方式使用printf。 (did you at least turn on warnings in your compiler? Many would flag your error!) (您是否至少在编译器中打开了警告?许多警告会标记您的错误!)

Besides that, your in and out converters did what you were after, just phrased badly. 除此之外,您的输入和输出转换器所做的就是您所追求的,只是措辞不好。 In op= the rhs is char, it can not possibly be on the same address of your class so you can just go ahead and storing it. 在op = rhs是char中,它不可能与您的课程位于同一地址,因此您可以继续进行存储。

The implicit conversion op also needs no fuss just return the member. 隐式转换op也无需大惊小怪,只需返回成员即可。

Why you need this is unclear, possibly you should read up on implicit conversions and custom op= stuff. 为什么需要这样做尚不清楚,可能您应该阅读隐式转换和自定义op =内容。 As it holds all kinds of danger, and should only be used by those clearly know when the conversions will apply, that the client code is such to welcome that, and the client code is unlikely to fall in a pit with the conversion present. 由于存在各种危险,只有明确知道何时应用转换的人才能使用它,因此客户代码非常受欢迎,并且客户代码不太可能陷入存在转换的困境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 返回类的类型而不是对象的类型 - return the type of a class instead of an object 一个类的对象作为另一个类的方法的返回类型 - Object of a class as return type of method of another class 类中的C ++方法定义是否必须指定返回类型? - Does a C++ method definition in a class have to specify the return type? 如果 function 在 class 的主体内定义,我是否需要在成员 function 的返回类型中指定 typename? - Do I need to specify typename in the return type of member function if the function is defined inside the body of the class? 如何使用自定义Object类为JNI方法定义返回类型? - How to define a return type for a JNI method with a custom Object class? 如何将类成员函数的返回类型设置为私有结构的对象 - How to set the Return Type of a Class Member Function as the object of a Private Struct 使用类成员函数对象推导成员函数返回类型 - Deduce member function return type using a class member function object 函数返回类型为父类时如何返回子类的对象? - How to return child class's object when function return type is parent class? 如何为operator []指定返回类型? - How can I specify a return type for operator[]? 根据其他模板参数指定返回类型 - Specify return type based on other template argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM