2017-06-14 17 views
0

私はクラスAを持ち、他のクラスのオブジェクトへのポインタのstd::vectorBです。私のクラスは特定のインデックスにあるオブジェクトへのポインタをconst返します。さて、オブジェクトを修正したいのであれば、それを行う最善の方法は何ですか?クラスC++内のメンバオブジェクトを変更する

Aクラスのget/setメソッドを書くことができます。これは最終的にクラスBのget/setメソッドを使用しますが、コードの複製です。 friendキーワードを使用してプライベートメンバーを変更することをお勧めしますか?

それとも、私はそれは、コードの重複を避けるためにBのプライベートメンバーを変更することができ、クラスAがクラスBfriendも作ることができます。

または、返されたポインタの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 
    */ 
}; 
+4

あなたが求めていることは完全にはっきりしません。あなたの質問を示すためにコード[mcve]を生成できますか? – George

+3

"friendキーワードを使用してプライベートメンバーを変更することをお勧めしますか?" - No. – KonstantinL

+1

"返されたポインタのconstを取り除くことができます" - これは "friend"より悪いです。 – KonstantinL

答えて

1

const性と友情は2つの完全に異なると関係のないものです。

ABの友人かどうかは関係ありません。これは、BメンバーへのアクセスをAにカプセル化したい場合、または発信者がBゲッター/セッターに直接アクセスできるようにする場合のどちらかを選択するだけです。

限りオブジェクトを変更することは、任意のゲッターは、それらが似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; 
}; 
関連する問題