C++超载

发布于 2024-11-11 17:49:31 字数 229 浏览 2 评论 0原文

可能的重复:
运算符重载

我想重载<和>按字母顺序排列字符串,我不知道该怎么做。

如何再次调用字符串类来重新定义<和>?

Possible Duplicate:
Operator overloading

I would like to overload < and > to alphabetize strings and I'm not sure how to do it.

How do I call the string class again to redefine < and >?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔日梦未散 2024-11-18 17:49:31

您不需要重载它们,因为 std::string 已经提供了它们。


一般来说,要重载函数,您不会调用类。你不能调用一个类。您也不能向类添加方法。例如,如果您想为现有类 A 提供 operator <,则必须在 A< 的命名空间中将 is 创建为自由函数/代码>:

bool operator<(const A& left, const B& right) {
    // implement logic here
}

You don't need to overload these, as std::string already provides them.


In general, to overload functions, you don't call a class. You cannot call a class. You can also not add methods to classes. If you want to provide, for instance, operator < for an existing class A, you have to create is as a free function in the namespace of A:

bool operator<(const A& left, const B& right) {
    // implement logic here
}
十二 2024-11-18 17:49:31

二元运算符大于(>)和小于(<)运算符主要用于if语句中,因此下面的源代码示例也使用if语句。我们看一下运算符重载源码:

#include<iostream>
using namespace std;

class Box {
    public:
        Box(double boxLength, double boxWidth, double boxHeight)
            :length(boxLength), width(boxWidth), height(boxHeight) {}

        // Compute the volume of a box.
        double volume() const {
            return length*width*height;
        }

        // First inline function
        inline bool operator<(const Box& someBox) const {
            return volume() < someBox.volume();
        }

        // Second inline function
        inline bool operator<(const double someValue) const {
            return volume() < someValue;
        }

        // Third inline function
        inline bool operator>(const Box& someBox) const {
            return volume() > someBox.volume();
        }

        // Fourth inline function
        inline bool operator>(const double someValue) const {
            return volume() > someValue;
        }

    private:
        double length;
        double width;
        double height;
};

int main() {
    Box myBox(15.0, 10.0, 5.0);
    Box myBox2(15.0, 5.0, 5.0);

    cout << "The myBox volume is: " << myBox.volume() << "\n";
    cout << "The myBox2 volume is: " << myBox2.volume() << "\n";

    // Trying the less than binary operator
    if(myBox < myBox2) {
        cout << "The myBox volume is less than myBox2 volume!\n";
    }else{
        cout << "The myBox volume is not less than myBox2 volume!\n";
    }

    // Trying the less than binary operator
    if(myBox < 1000) {
        cout << "The myBox volume is less than 1000!\n";
    }else{
        cout << "The myBox volume is not less than 1000!\n";
    }

    // Trying the greater than binary operator
    if(myBox > myBox2) {
        cout << "The myBox volume is greater than myBox2 volume!\n";
    }else{
        cout << "The myBox volume is not greater than myBox2 volume!\n";
    }

    // Trying the greater than binary operator
    if(myBox > 500) {
        cout << "The myBox volume is greater than 500!\n";
    }else{
        cout << "The myBox volume is not greater than 500!\n";
    }

    return 0;
}

可以从String继承Box来为字符串重载这些运算符

The binary operators greater than (>) and less than (<) operators are mostly used in if statements, so the source code example below is also using if statements. Let's look at the operator overloading source code:

#include<iostream>
using namespace std;

class Box {
    public:
        Box(double boxLength, double boxWidth, double boxHeight)
            :length(boxLength), width(boxWidth), height(boxHeight) {}

        // Compute the volume of a box.
        double volume() const {
            return length*width*height;
        }

        // First inline function
        inline bool operator<(const Box& someBox) const {
            return volume() < someBox.volume();
        }

        // Second inline function
        inline bool operator<(const double someValue) const {
            return volume() < someValue;
        }

        // Third inline function
        inline bool operator>(const Box& someBox) const {
            return volume() > someBox.volume();
        }

        // Fourth inline function
        inline bool operator>(const double someValue) const {
            return volume() > someValue;
        }

    private:
        double length;
        double width;
        double height;
};

int main() {
    Box myBox(15.0, 10.0, 5.0);
    Box myBox2(15.0, 5.0, 5.0);

    cout << "The myBox volume is: " << myBox.volume() << "\n";
    cout << "The myBox2 volume is: " << myBox2.volume() << "\n";

    // Trying the less than binary operator
    if(myBox < myBox2) {
        cout << "The myBox volume is less than myBox2 volume!\n";
    }else{
        cout << "The myBox volume is not less than myBox2 volume!\n";
    }

    // Trying the less than binary operator
    if(myBox < 1000) {
        cout << "The myBox volume is less than 1000!\n";
    }else{
        cout << "The myBox volume is not less than 1000!\n";
    }

    // Trying the greater than binary operator
    if(myBox > myBox2) {
        cout << "The myBox volume is greater than myBox2 volume!\n";
    }else{
        cout << "The myBox volume is not greater than myBox2 volume!\n";
    }

    // Trying the greater than binary operator
    if(myBox > 500) {
        cout << "The myBox volume is greater than 500!\n";
    }else{
        cout << "The myBox volume is not greater than 500!\n";
    }

    return 0;
}

you can inherit Box from String to overload these operators for string

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文