Operator = with const

问题描述 投票:-2回答:2

我有Map和Person这两个类,我想用int和Person类型创建一个map,但是当我尝试添加一个map并执行“ pair.second = value”时,此操作符将调用operator =,当我​​完成operator =值是错误的(空),但应为name =“ Name”且age =15。如果我使用带有return *的operator =版本,这当然可以,但是我想使用const运算符。我该怎么办?

#include<iostream>
#include <vector>
#include <utility>
#include<string>
#include <string.h>
using namespace std;
template<class Key, class Value>
class Map {
private:
    vector<pair<Key, Value>> _pairVector;
public:
    Map() {}
    ~Map() {}
    void add(const Key& key, const Value& value); 
};
template<class Key, class Value>
void Map<Key, Value>::add(const Key & key, const Value & value){
    pair<Key, Value> pair;
    pair.first = key;
    pair.second = value;
    _pairVector.push_back(pair);
}
class Person {
private:
    string _name;
    int _age;

public:
    Person() {}
    ~Person() {}
    void setName(string name) { _name = name; }
    void setAge(int age) { _age = age; }
    string getName()const { return _name; }
    int getAge()const { return _age; }
    Person & operator=(const Person& p)const;
};
Person & Person::operator=(const Person & p) const{
    Person p2;
    p2.setName(p.getName());
    p2.setAge(p.getAge());
    return p2;
}

主程序

int main() {
    Map<int, Person> map1;
    Person p;
    p.setName("Name");
    p.setAge(15);
    map1.add(1, p);
    return 0;
}
c++
2个回答
0
投票

如果删除了operator =的运算符重载,并让C ++在相同类型的类之间使用它的默认赋值,它将按预期工作。

或者,如果希望保持运算符重载(也许以后再做一些更奇怪的事情,则应将其更改为:

 Person & Person::operator=(const Person & p) {
    setName(p.getName());
    setAge(p.getAge());
    return (* this);
}

0
投票

这是修改当前程序的方式:

#include<iostream>
#include <vector>
#include <utility>
#include<string>
#include <string>
using namespace std;
template<class Key, class Value>
class Map {
private:
    vector<pair<Key, Value>> _pairVector;
public:
    Map() {}
    ~Map() {}
    void add(const Key& key, const Value& value); 
    void show()const;
};
template<class Key, class Value>
void Map<Key, Value>::add(const Key & key, const Value & value){
    pair<Key, Value> pair;
    pair.first = key;
    pair.second = value;
    _pairVector.push_back(pair);
}

template<class Key, class Value>
void Map<Key, Value>::show()const
{
    for (auto &p: _pairVector)
    {
        std::cout<< p.first << "= " << p.second << std::endl;
    }
}
class Person {
private:
    string _name;
    int _age;

public:
    Person() {}
    ~Person() {}
    void setName(const string& name) { _name = name; }
    void setAge(int age) { _age = age; }
    string getName()const { return _name; }
    int getAge()const { return _age; }
    Person & operator=(const Person& p);
    friend ostream & operator << (ostream &out, const Person &p); 
  };

赋值运算符不应为const,首先应检查自我赋值,然后再检查另一个对象的成员副本。

Person & Person::operator=(const Person & p) {

    if(&p == this){
        return *this;
    }

    this->setName(p.getName());
    this->setAge(p.getAge());
    return *this;
}

ostream & operator << (ostream &out, const Person &p)
{
    out << p._name;
    out << "," << p._age << std::endl;
    return out;
}

int main() {
    Map<int, Person> map1;
    Person p;
    p.setName("Name");
    p.setAge(15);
    map1.add(1, p);
    map1.show();
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.