【发布时间】:2020-09-10 09:33:25
【问题描述】:
我想创建一个类/结构,其中一个属性在功能上依赖于其他属性。如何实现?
struct Numbers {
int a;
int b;
int c; // c == a+b
}
Numbers n1 {1, 2, 3}; // Ok.
Numbers n2 {1, 2, 4}; // Error!
在我的用例中,a, b, c 是不变的,如果这很重要(所以可以使用 const int)。
所有属性都会在类/结构方法中出现多次,所以目标是缓存值a+b。以加法为例,依赖函数可能更复杂。
【问题讨论】:
标签: c++ class struct attributes functional-dependencies