修改类 c++ 中的成员对象
Modifying member objects inside class c++
我有一个类A
,其成员是指向另一个类B
对象的指针std::vector
。我的类返回一个指向特定索引处对象的const
指针。现在,如果我想修改对象,最好的方法是什么?
我可以在类 A
中编写 get/set 方法,它最终将使用类 B
的 get/set 方法,但它是代码的重复。使用 friend
关键字修改私有成员是否是一种好的做法?
或者,我可以将类A
作为类B
的friend
,然后它可以修改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;
};
相关文章:
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 在运行时有条件地删除类成员或跳过调用该成员对象的构造函数
- C++是否有定义的方法来传递指向类的成员对象的成员函数的指针
- (2 问题)"类"类型重新定义(即使 #pragma 一次),以及静态函数内的静态成员对象初始化?
- 声明成员对象而不调用其默认构造函数
- 指向成员对象的指针 - 中断线程
- 是否有更好的方法来封装成员对象可以访问的共享存储池?
- 应该在成员对象上调用析构函数
- 调用成员对象的构造函数
- 将 const 类型引用对象注册为类成员对象C++
- 修改类 c++ 中的成员对象
- 从成员对象调用方法
- 从对象调用成员对象,错误:引用非常量值的初始值必须是左值
- 构造函数,成员对象
- 不能使外部类成为内部类内的成员对象
- 使用从另一个类继承的类的对象初始化成员对象
- 如何避免需要在初始化列表中初始化成员对象
- 初始化具有参数的类成员对象的正确方法
- 将指针添加到成员对象中的指针动态阵列
- C 将成员对象函数分配给类成员功能