修改类 c++ 中的成员对象

Modifying member objects inside class c++

本文关键字:成员对象 c++ 修改      更新时间:2023-10-16

我有一个类A,其成员是指向另一个类B对象的指针std::vector。我的类返回一个指向特定索引处对象的const指针。现在,如果我想修改对象,最好的方法是什么?

我可以在类 A 中编写 get/set 方法,它最终将使用类 B 的 get/set 方法,但它是代码的重复。使用 friend 关键字修改私有成员是否是一种好的做法?

或者,我可以将类A作为类Bfriend,然后它可以修改B的私有成员以避免代码重复。

或者,我可以删除返回指针上的const

class B;
class A {
    std::vector<B*> m_list;
public:
    const B* getB(int index) {
        return m_list[index];
    }
};
class B {
private:
    int a;
    float b;
    long c;
    long getC() {
        return C;
    }
    int getA() {
        return a;
    }
    float getB() {
        return b;
    }
    /*
    set_methods
    */
};

恒心和友谊是两个完全不同的、不相关的东西。

A是否是B的朋友并不重要。 这只是您的设计选择,无论您是想封装对A内部B成员的访问,还是要允许调用者直接访问B getter/setter。

就修改对象而言,任何 getter 都应该声明为 const,以便它们可以在const和非const对象上调用(因为它们不会修改它们正在读取的对象(,但是如果您希望能够修改对象的成员,则必须通过非const指针(或非const引用(访问该对象:

class B;
class A {
private:
    std::vector<B*> m_list;
public:
    // this can be called on a non-const A, and
    // the B members can be read and modified...
    B* getB(int index) {
        return m_list[index];
    }
    // this can be called only on a const A, and
    // the B members can be read but not modified...
    const B* getB(int index) const {
        return m_list[index];
    }
    /* optional:
    int getB_A(int index) const {
        return getB(index)->getA();
    }
    float getB_B(int index) const {
        return getB(index)->getB();
    }
    long getC(int index) const {
        return getB(index)->getC();
    }
    void setB_A(int index, int value) {
        getB(index)->setA(value);
    }
    void setB_B(int index, float value) {
        getB(index)->setB(value);
    }
    void setB_C(int index, long value) {
        getB(index)->setC(value);
    }
    */
};
class B {
private:
    int a;
    float b;
    long c;
public:
    int getA() const {
        return a;
    }
    float getB() const {
        return b;
    }
    long getC() const {
        return c;
    }
    void setA(int value) {
        a = value;
    }
    void setB(float value) {
        b = value;
    }
    void setC(long val) {
        c = val;
    }
};

或:

class B;
class A {
private:
    std::vector<B*> m_list;
public:
    // this can be called on a non-const A, and
    // the B members can be read and modified...
    B* getB(int index) {
        return m_list[index];
    }
    // this can be called only on a const A, and
    // the B members can be read but not modified...
    const B* getB(int index) const {
        return m_list[index];
    }
    int getB_A(int index) const {
        return getB(index)->a;
    }
    float getB_B(int index) const {
        return getB(index)->b;
    }
    long getC(int index) const {
        return getB(index)->c;
    }
    void setB_A(int index, int value) {
        getB(index)->a = value;
    }
    void setB_B(int index, float value) {
        getB(index)->b = value;
    }
    void setB_C(int index, long value) {
        getB(index)->c = value;
    }
};
class B {
private:
    int a;
    float b;
    long c;
    friend class A;
};