【发布时间】:2021-03-01 01:23:48
【问题描述】:
我觉得这个问题对其他人来说也很有趣。所以我选择了这个来最后问我关于 Stack Overflow 的第一个问题。对这种情况的简要描述如下:
class A {
public:
Type_m_first m_first;
Type_m_second m_second;
}
class B { // shoud be const on a.m_first
private:
A& a;
public:
B(A& a_) : a(a_) {};
}
编辑: 将A 的成员设置为private 并使用getter 和setter 并不能解决我的问题。但是,我希望有一个像这样简单的解决方案,因为我可能会因为缺乏编程经验而错过一些简单的东西。
现在,我需要 B 的任何尚未实现的方法,以便只有 const 可以访问 A.m_first 而非const 可以访问 A.m_second,同样,我需要任何在访问B.a 时,使用具有相同访问限制的B 类型的其他代码。当然,这是不可能的(至少从我谦虚的角度来看)。
不过,我的问题是:
如何对B 类强制执行这样的const-限制对非const 成员变量的访问?
编辑 使用私人m_first 和m_second 以及m_first 和m_second 的getter 和setter,情况将是相同的。那么问题来了:如何限制B访问m_first的constgetter,拒绝B访问m_first的setter,同时允许B访问使用m_second的setter?
以上是问题。但是,如果没有以下上下文,这个问题可能是不完整的,因为它说明了问题的重要性。我面临的实际情况如下:
class A {
public:
Type_m_first m_first;
Type_m_second m_second;
public:
// A lot of code.
private:
// A lot of code.
}
class B { // shoud be const on a.m_first
private:
A& a;
public:
B(A& a_) : a(a_) {};
private:
// A lot of code, that I am supposed to move from somewhere else to here
// or write myself.
public:
// A lot of code, that I am supposed to move from somewhere else to here
// or write myself.
}
class C { // shoud be const on a.m_second
private:
A& a;
public:
C(A& a_) : a(a_) {};
private:
// A lot of code, that I am supposed to move from somewhere else to here
// or write myself.
public:
// A lot of code, that I am supposed to move from somewhere else to here
// or write myself.
}
/*
* A lot of other Code that is supposed to work with A, B and C. The following three
* functions serve as an example. As you see, everything is manipulating essentially
* the same data from a.
*/
void f(A& a, /* other args */ ) { /* ... */ };
void g(B& b, /* other args */ ) { /* ... */ }; // shoud be const on b.a.m_first by design
int h(C& c, /* other args */ ) { /* ... */ }; // shoud be const on c.a.m_second by design
int main() {
A a;
B b = B(a);
C c = C(a);
f(a, /* other args */ );
g(b, /* other args */ );
return h(c, /* other args */ );
}
同样,我需要 B 的任何尚未实现的方法,只有 const 可以访问 A.m_first,而非 const 可以访问 A.m_second。但是,对于C 类,我需要正好相反:我需要C 的任何尚未实现的方法,只有const 可以访问A.m_second,而没有@ 987654355@ 访问A.m_first。同样,任何使用 B 和 C 类型的代码都应该有相应的访问限制。
当然,问题又来了:我为什么需要这个?答案是,算法的逻辑结构将由这样的设计强制执行。到目前为止,公开所有内容只是一个问题,因为意外忽略逻辑结构会导致难以发现代码中的错误,并且由于代码的复杂性,很难跟踪这些设计未强制执行的限制.
我想出的最佳解决方案 - 但尚未实现 - 是将代码复制到两个包装类:
class A_first {
public:
const Type_m_first& m_first;
Type_m_second& m_second;
A_first(A&) ; m_first(const A.m_first), m_second(A.m_second) {};
public:
// Same code as before.
protected:
// Same code as before.
private:
// Same code as before.
}
class A_second {
public:
Type_m_first& m_first;
const Type_m_second& m_second;
A_first(A&) ; m_first(A.m_first), m_second(const A.m_second) {};
public:
// Same code as before.
protected:
// Same code as before.
private:
// Same code as before.
}
这并不可取,因为随着时间的推移代码会发生很大变化,并且跟踪三个类的变化很容易出错。我的问题是,遇到这种情况该怎么办?
【问题讨论】:
-
如果不了解我们看不到的代码,很难给出建议。但是@HAL9000 的答案已经指出了方向:如果一个数据成员不被在一个类之外(即默认值)操作,它应该是私有的并且应该有 getter / setter 方法。此外,操作 A 的数据成员的代码最好也位于 A 内部; B 和 C 也是如此。所以也许你的函数 f、g、h 可能最终成为 A、B 和 C 的方法。
-
@kaba 非常感谢,我编辑了问题并使其更加清晰。
标签: c++ software-design const-correctness