Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 内置类型的运算符函数_C++_Operators - Fatal编程技术网

C++ 内置类型的运算符函数

C++ 内置类型的运算符函数,c++,operators,C++,Operators,如何为内置类型定义的运算符显式使用底层运算符函数?参见代码: struct Int { Int() = default; Int(int initial) { i = initial; } Int operator+(Int other) { return Int(i + other.i); } int i; }; std::ostream& operator<< (std:

如何为内置类型定义的运算符显式使用底层运算符函数?参见代码:

struct Int
{
    Int() = default;
    Int(int initial)
    {
        i = initial;
    }

    Int operator+(Int other)
    {
        return Int(i + other.i);
    }

    int i;
};

std::ostream& operator<< (std::ostream &out, const Int& data)
{
    out << data.i;
    return out;
}

int main()
{
    Int a, b = 5, c = 5;
    a = b + c;

    std::cout << a << std::endl;

    Int d, e = 5, f = 5;
    d.operator=(e.operator+(f));  // possible with user defined types

    std::cout << d << std::endl;

    int g, h = 5, j = 5;
    g.operator=(h.operator+(j));  // illegal

    return 0;
}
struct Int
{
Int()=默认值;
Int(Int首字母)
{
i=初始值;
}
整数运算符+(整数其他)
{
返回Int(i+other.i);
}
int i;
};
std::ostream和操作员
如何为内置类型定义的运算符显式使用底层运算符函数

你不能

如果您正在寻找标准的相关部分,则应为:

16.5重载运算符

6操作员功能应为非静态成员功能或 是至少具有一个类型为的参数的非成员函数 类、对类的引用、枚举或对 枚举

如何为内置类型定义的运算符显式使用底层运算符函数

你不能

如果您正在寻找标准的相关部分,则应为:

16.5重载运算符

6操作员功能应为非静态成员功能或 是至少具有一个类型为的参数的非成员函数 类、对类的引用、枚举或对 枚举


在标准文本:PNo中,这是不可能的。对于某些类型,没有任何“底层”声明。它们只是内置在编译器中。在这两种情况下都可以使用
x+y
,但只有当程序将操作符定义为成员时才能使用
x.operator+(y)
格式。在标准文本中:PNo,这是不可能的。某些类型没有任何“基础”声明。它们只是内置在编译器中。在这两种情况下都可以使用
x+y
,但只有当程序将操作符定义为成员时,才可以使用
x.operator+(y)
格式。