小编Ana*_*ass的帖子

对运算符定义c ++的误解

我试图理解为什么在调用时没有为此类定义的运算符(<)被执行:

//File A.h (simplified class)
#ifndef __A__H
#define __A__H

#include <string>
#include <cstdlib>
#include <iostream>
using namespace std;

class A {

private:
    string _str;
    int _number;

public:
    A( string str="", int age=0): _str(str), _number(number){} //inline

    int operator < (const A &a1 ) const 
    {
        cout<<"Call of new operator <"<<endl;

        if ( _str == a1._str )
            return _number < a1._number; 
        return _str < a1._str; //here use of (<) associated to string 
     }

};
#endif

int main()
{
    A *obj1= new A("z",10); …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading

0
推荐指数
1
解决办法
69
查看次数

标签 统计

c++ ×1

operator-overloading ×1