C ++构造函数中需要彼此的两个类?

时间:2020-11-30 19:15:01

I have two very similar classes in a .h file that need each other in the constructor. It's about a Color class and one will use unsigned char 0 to 255 as RGB and the other will use floats 0.0 to 1.0 as RGB and I need to be able to convert from one and other in the constructor and assignment operators and other member functions.

我在.h文件中有两个非常相似的类,它们在构造函数中需要彼此。它是一个Color类,一个将使用无符号字符0到255作为RGB,另一个将使用浮点数0.0到1.0作为RGB,我需要能够在构造函数和赋值运算符以及其他成员函数中从一个和另一个转换。

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) {

    }
};

Can I put them in separate files without getting into an circular (I believe that's how it is called) include? I think I know the answer to this question but I want to know what other solutions might be out there. I would prefer them to be in the same file but if there's no other way then I'll separate them.

我可以将它们放在单独的文件中而不进入循环(我相信它是如何被称为)包含?我想我知道这个问题的答案,但我想知道其他解决方案可能是什么。我希望他们在同一个文件中,但如果没有别的办法,我会将它们分开。

3 个解决方案

#1


4  

Yes.

class Color3f; // <--- Forward declaration

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)
    {

    }

    // Can't define this yet with only an incomplete type.
    inline Color3(const Color3f& other);
};


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 is now a complete type, so define the conversion ctor.
Color3::Color3(const Color3f& other)
        : R(other.R*255), G(other.G*255), B(other.B*255)
    {

    }

#2


0  

These situations are what 'pragma once' is for. Just add it to the top of your .h file and it will only be included once:

这些情况是'pragma once'的用途。只需将其添加到.h文件的顶部,它将只包含一次:

#pragma once

However, make sure that it is only an 'include' loop, and not an actual functionality loop. It looks like your constructors might actually cause I loop when instantiated.

但是,请确保它只是一个“包含”循环,而不是实际的功能循环。看起来你的构造函数实际上可能会在实例化时导致我循环。

#3


0  

You can add a declaration of the other class to the top of your header file to avoid circular dependence problem. For example:

您可以将另一个类的声明添加到头文件的顶部,以避免循环依赖问题。例如:

class Color3;

at the top of Color3f.h, and same for the other class. Combined with #pragma once or #ifndef include guard, the program can compile fine with split files.

在Color3f.h的顶部,对于其他类也是如此。结合#pragma once或#ifndef include guard,程序可以使用拆分文件进行编译。

But classes this similar should really be declared as the same class specialized with different template parameters. For example Color3<unsigned char> and Color3<float>.

但是类似的类应该真正声明为具有不同模板参数的同一类。例如Color3 和Color3

#1


4  

Yes.

class Color3f; // <--- Forward declaration

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)
    {

    }

    // Can't define this yet with only an incomplete type.
    inline Color3(const Color3f& other);
};


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 is now a complete type, so define the conversion ctor.
Color3::Color3(const Color3f& other)
        : R(other.R*255), G(other.G*255), B(other.B*255)
    {

    }

#2


0  

These situations are what 'pragma once' is for. Just add it to the top of your .h file and it will only be included once:

这些情况是'pragma once'的用途。只需将其添加到.h文件的顶部,它将只包含一次:

#pragma once

However, make sure that it is only an 'include' loop, and not an actual functionality loop. It looks like your constructors might actually cause I loop when instantiated.

但是,请确保它只是一个“包含”循环,而不是实际的功能循环。看起来你的构造函数实际上可能会在实例化时导致我循环。

#3


0  

You can add a declaration of the other class to the top of your header file to avoid circular dependence problem. For example:

您可以将另一个类的声明添加到头文件的顶部,以避免循环依赖问题。例如:

class Color3;

at the top of Color3f.h, and same for the other class. Combined with #pragma once or #ifndef include guard, the program can compile fine with split files.

在Color3f.h的顶部,对于其他类也是如此。结合#pragma once或#ifndef include guard,程序可以使用拆分文件进行编译。

But classes this similar should really be declared as the same class specialized with different template parameters. For example Color3<unsigned char> and Color3<float>.

但是类似的类应该真正声明为具有不同模板参数的同一类。例如Color3 和Color3