我在.h文件中有两个非常相似的类,在构造函数中需要彼此。它是关于一个Color类的,一个将使用无符号字符0到255作为RGB,另一个将使用浮点数0.0到1.0作为RGB,并且我需要能够在构造函数和赋值运算符以及其他成员函数中从一个和另一个进行转换。C++构造函数中需要彼此的两个类?
Color3.h:
class Color3 {
public:
unsigned char R, G, B;
Color3()
: R(0), G(0), B(0) {
}
Color3(unsigned char r, unsigned char g, unsigned char b)
: R(r), G(g), B(b) {
}
Color3(const Color3f& other)
: R(other.R*255), G(other.G*255), B(other.B*255) {
}
};
class Color3f {
public:
float R, G, B;
Color3f()
: R(0), G(0), B(0) {
}
Color3f(float r, float g, float b)
: R(r), G(g), B(b) {
}
Color3f(const Color3& other)
: R(other.R/255), G(other.G/255), B(other.B/255) {
}
};
我可以把它们放在不同的文件,而无需进入一个圆形(我认为,这是多么称为)包括哪些内容?我想我知道这个问题的答案,但我想知道可能有哪些其他解决方案。我更喜欢他们在同一个文件中,但如果没有其他方式,我会分开他们。
请编写没有明显错误的代码,如果你自己试着编译你写的内容,会更好。在你的类中,Color3f的构造函数被命名为Color3 –
对不起,我修复了代码(复制粘贴从未给我带来过什么好处) –