从 class 外部访问联合 "type"?

Accessing a union "type" from outside the class?

我写了这段代码,所以我可以自动将 float 和 int 写在一起,作为 64 位:

namespace X{

template<typename P>
class MyClass<P>{
    public:

        MyCompStruct getStruct(){
            return MyCompStruct;          // Is this correct?
        }

    private:

    float a;                                                

    struct MyCompStruct{
        union{
            struct{
                float                       b;                                                   
                int32_t                     c;                                              
            };

            uint64_t                        composite;
         };

         operator=(const MyCompStruct& src) {
              this->composite= src.composite; 
         }
    };
};

}

但我正在努力声明,以便我可以使用类型为 MyClass

:

的对象从不同的 class 获取结构
X::MyClass<P>::MyCompStruct mcs = obj.getStruct();   // Completely wrong
mcs.b = ...
mcs.c = ...

有人可以帮忙吗?

你的代码有几个问题

  1. MyCompStruct 是使用模板专门化语法定义的。去掉多余的 <P>.
  2. MyCompStruct 声明为 private,因此只有 MyClassMyClass 的朋友可以访问它。如果您希望其他人访问 MyCompStruct,您需要将其设置为 public(如果您想限制对派生 类 的访问,则需要保护)。
  3. MyCompStructgetStruct 中首次使用后声明。在使用之前声明它。
  4. 您的赋值运算符没有 return 类型并且 return 没有数据。这是错误的。
  5. getStruct 正在尝试 return 类型而不是类型的 实例 。您需要在其中添加一些括号。
  6. 停止使用 using namespace std;。这只是糟糕的形式。它不在你发布的代码中,但我能闻到。
  7. getStruct 似乎不是一个可以对对象进行任何修改的函数。宣布它 const 对你的灵魂有益。

这是您的代码,其中包含针对所指出问题的所有修复程序above.I 删除了命名空间,因为它与此处无关。

template<typename P>
class MyClass   //  Removed extra <P>
{
public: //  Make it public

    //  Decalre before it's usage.
    struct MyCompStruct
    {
        union{
            struct{
                float                       b;
                int32_t                     c;
            };  //  Unnamed unions are a non-standard. Be careful.

            uint64_t                        composite;
        };

        //  Added a return type
        MyCompStruct& operator = (const MyCompStruct& src)
        {
            composite = src.composite;

            //  Returning this allows operations to be chained a = x = y;
            return *this;
        }
    };


    MyCompStruct getStruct() const
    {
        return MyCompStruct();  //  Return an instance instead of a type
    }

private:

    float a;
};

基本用法:

int main()
{
    MyClass<int> obj;
    MyClass<int>::MyCompStruct mcs = obj.getStruct();
}

现在所有问题都已解决,您有几个选择 getStruct

在它上面的版本中 return 是 MyCompStruct 的新创建实例。如果你 want/need 只能访问类型本身,而 getStruct 所做的只是创建一个空实例,你可以完全摆脱 getStruct 并允许其他人直接创建 [=14 的实例=].

int main()
{
    MyClass<int>::MyCompStruct mcs;
}

如果这不是您想要的行为,您更愿意 return 对 MyClass 拥有(作为成员变量)的已创建实例的引用或指针,您可以像下面这样更改它

template<typename P>
class MyClass
{
public:

    /**** Removed unchanged code ****/

    // Return a non-const reference allowing others to modify the
    // contents of compData. If you don't want others to MODIFY the data
    // change the return type to const MyCompStruct&
    MyCompStruct& getStruct()
    {
        return compData;  //  Return our owned instance
    }

    // Return a const reference to compData. This prevents others
    // from modifying the data and allows them to access it with a
    // const qualified instance of MyClass.
    const MyCompStruct& getStruct() const
    {
        return compData;  //  Return our owned instance
    }

private:

    MyCompStruct compData;
};