Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.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++_Operator Overloading - Fatal编程技术网

C++ 新类对象,运算符重载

C++ 新类对象,运算符重载,c++,operator-overloading,C++,Operator Overloading,我正在尝试创建一个操作符,它与构造函数做的事情相同。 我已经为这个类创建了重载输出操作符x;} }; std::ostream&operator您就快到了 它应该看起来像这样。内联评论: #include <iostream> #include <memory> class foo { private: int x; public: foo(int x) : x(x) { } // add a con

我正在尝试创建一个操作符,它与构造函数做的事情相同。 我已经为这个类创建了重载输出操作符
x;}
};
std::ostream&operator您就快到了

它应该看起来像这样。内联评论:

#include <iostream>
#include <memory>

class foo {
    private: 
        int x;
    public: 
        foo(int x) : x(x) { }

        // add a const modifier because this function does not modify foo
        int getX() const { return this->x; }        

        // it usually makes sense to implement + in terms of +=
        foo& operator+=(int arg)
        {
            x += arg;
            return *this;
        }
};

// implement + in terms of += on a copy
foo operator+(foo l, int r)
{
    l += r;
    return l; 
}

// for this class, make addition commutative. i.e x+y == y+x
foo operator+(int l, foo const& r)
{
    return r + l;
}

// add a const modifier because we expect and demand that this operation
// will not modify f
std::ostream& operator<< (std::ostream& text, foo const& f) {
    text  << f.getX();
    return text;
}

int main()
{
    foo bar(2);
    std::cout <<bar; //returns 2

    foo bar2 = bar + 5;
    std::cout <<bar2; //returns 7

    foo bar3 = 5 + bar2;
    std::cout <<bar3; //returns 12

    return 0;
}
#包括
#包括
福班{
私人:
int x;
公众:
foo(intx):x(x){}
//添加常量修饰符,因为此函数不修改foo
int getX()常量{返回此->x;}
//在以下方面实施+通常是有意义的+=
foo&运算符+=(int arg)
{
x+=arg;
归还*这个;
}
};
//根据副本上的+=实现+
foo运算符+(foo l,int r)
{
l+=r;
返回l;
}
//对于此类,使加法可交换。i、 ex+y==y+x
foo运算符+(int l,foo const&r)
{
返回r+l;
}
//添加常量修饰符,因为我们希望并要求此操作
//不会修改f

std::ostream&operator这将创建一个新的foo对象

foo operator+(foo& f, int x) {
    return foo(f.getX() + x);
}

//Usage
foo bar(2);
foo test = bar + 5;
std::cout << test; //outputs 7
foo操作符+(foo&f,int x){
返回foo(f.getX()+x);
}
//用法
富吧(2);;
foo试验=巴+5;

std::不能使用
f tmp=新的foo(x)向我表明您对该语言的基础知识不太精通。这将有助于您通读。类型的名称是什么,参数的名称是什么?这个类似的问题可能会引起兴趣:(顺便说一句,它被标记为重复)在这里,我为
运算符+
重载提供了多种解决方案。您的问题表明您希望表达式
bar+n
返回一个
bar
的副本,但您真的不希望它返回一个值为
bar.getX()+n
的bar副本吗?有点误解,您的代码正在为已创建的对象添加一些值。我希望我的代码在
+
符号后创建新对象。谢谢。@sqlMasterSOon听起来你想说的是希望操作符+的行为像一个复制构造函数?是的,这就是我想要的。它看起来不错,但是如何使它像
foo test+5
。您的代码正在使用另一个对象创建新对象。如何在不使用其他对象的情况下创建新对象?有可能吗?@sqlMasterSOon我不太明白。
foo-test+5之间有什么区别
foo测试(5)
?因为例如,我的类中有
heightoftree
变量。使用
foo-tree1(5)
-normal构造函数,我只想将
5
赋值给我的变量。但是使用
foo tree2+5
,我想创建一个新对象,其值乘以两倍(例如)。@sqlMasterSOon您可以编辑您的问题,并在代码中添加一个实际使用示例。
#include <iostream>
#include <memory>

class foo {
    private: 
        int x;
    public: 
        foo(int x) : x(x) { }

        // add a const modifier because this function does not modify foo
        int getX() const { return this->x; }        

        // it usually makes sense to implement + in terms of +=
        foo& operator+=(int arg)
        {
            x += arg;
            return *this;
        }
};

// implement + in terms of += on a copy
foo operator+(foo l, int r)
{
    l += r;
    return l; 
}

// for this class, make addition commutative. i.e x+y == y+x
foo operator+(int l, foo const& r)
{
    return r + l;
}

// add a const modifier because we expect and demand that this operation
// will not modify f
std::ostream& operator<< (std::ostream& text, foo const& f) {
    text  << f.getX();
    return text;
}

int main()
{
    foo bar(2);
    std::cout <<bar; //returns 2

    foo bar2 = bar + 5;
    std::cout <<bar2; //returns 7

    foo bar3 = 5 + bar2;
    std::cout <<bar3; //returns 12

    return 0;
}
foo operator+(foo& f, int x) {
    return foo(f.getX() + x);
}

//Usage
foo bar(2);
foo test = bar + 5;
std::cout << test; //outputs 7