简体   繁体   English

嵌套struct构造函数和Union的问题

[英]Problems with nested struct constructor and Union

I have a struct nested winin a class and a union of that struct. 我有一个结构嵌套winin一个类和该结构的联合。 If I have a struct constructor that takes arguments, then the union won't compile. 如果我有一个带有参数的结构构造函数,那么union将不会编译。

I also want to create an instance of the struct using an argument. 我还想使用参数创建struct的实例。 That line also fails. 那条线也失败了。

class test
{
public:
test(void);
~test(void);

struct dtType {
    // inline constructors with initialisation lists
    dtType() : mins(0), hrs(0),day(0),mnth(0),year(0),DPOffset(0),DTType(0) {}
    dtType(byte z) : mins(z), hrs(z),day(z),mnth(z),year(z),DPOffset(0),DTType(0) {}
    dtType(byte n,byte h, byte d, byte m, byte y, byte o, byte t) : mins(n), hrs(h),day(d),mnth(m),year(y),DPOffset(o),DTType(t) {}

    // overloaded operator functions
    bool operator< (dtType date){return true;};
    bool operator<= (dtType date){return true;};
    bool operator> (dtType date){return true;};
    bool operator>= (dtType date){return true;};
    bool operator== (dtType date){return true;};

    // data members
    unsigned mins: 3;
    unsigned hrs: 5; // 8 bits
    unsigned day: 5; 
    unsigned mnth: 4;
    unsigned year: 7; // 16 bits
    unsigned DPOffset: 6; 
    unsigned DTType  : 2;
};

// if I comment out the union it compiles, otherwise I get:
// error C2620: member 'test::dtUnion::dt' of union 'test::dtUnion' has user-defined  constructor or non-trivial default constructor

union dtUnion { 
    dtType dt;
    unsigned long dtLong; // 32 bits
} dtU;

// if I call dtType judgement_day(); it compiles. Otherwise I get:
dtType judgement_day(1); // error C2059: syntax error : 'constant'

};

Following the answers below I have now tried the following, but I get error C2438: judgement_day,dateMask,timeMask : cannot initialize static class data via constructor 下面的答案我现在尝试了以下,但我得到错误C2438:judgement_day,dateMask,timeMask:无法通过构造函数初始化静态类数据

class test
{
public:
    test(): judgement_day(1),dateMask(1,1,1,1,1,0,0),timeMask(1,1,0,0,0,0,0){}
~test();
public:
struct dtType {
    // inline constructors with initialisation lists
    dtType() : mins(0), hrs(0),day(0),mnth(0),year(0),DPOffset(0),DTType(0) {}
    dtType(byte z) : mins(z), hrs(z),day(z),mnth(z),year(z),DPOffset(0),DTType(0) {}
    dtType(byte n,byte h, byte d, byte m, byte y, byte o, byte t) : mins(n), hrs(h),day(d),mnth(m),year(y),DPOffset(o),DTType(t) {}
    // overloaded operator functions
    bool operator< (dtType date){return true;};
    bool operator<= (dtType date){return true;};
    bool operator> (dtType date){return true;};
    bool operator>= (dtType date){return true;};
    bool operator== (dtType date){return true;};
    // data members
    unsigned mins: 3;
    unsigned hrs: 5; // 8 bits
    unsigned day: 5; 
    unsigned mnth: 4;
    unsigned year: 7; // 16 bits
    unsigned DPOffset: 6; 
    unsigned DTType  : 2;
};
const static dtType judgement_day;
const static dtType dateMask;
const static dtType timeMask;
};

Having union members with explicit constructors is disallowed by the standard, it has nothing to do with the union being a member or a nested type. 标准不允许具有显式构造函数的union成员,它与作为成员的union或嵌套类型无关。

The following would also fail to compile: 以下也无法编译:

struct X
{
    X() {};
};
union Y
{
    X k;
};

Others have answered the question about the union; 其他人回答了关于工会的问题; it can't contain members with non-trivial default constructors. 它不能包含具有非平凡默认构造函数的成员。 To answer the second question, about member initialisation: 要回答第二个问题,关于成员初始化:

You can't initialise non-static members in the class definition; 您无法在类定义中初始化非静态成员; they have to be initialised in the constructor's initialiser list: 它们必须在构造函数的初始化列表中初始化:

class test {
    // stuff
    dtType judgement_day;
};

test::test() : judgement_day(1) {}

The reason that dtType judgement_day(); dtType judgement_day(); compiles is that it's declaring a function, not a variable. compiles是它声明一个函数,而不是一个变量。

UPDATE: since you actually want these members to be static, they are instead initialised in a source file, similarly to global variables: 更新:因为您实际上希望这些成员是静态的,所以它们在源文件中初始化,类似于全局变量:

// in the header
class test {
    // stuff
    const static dtType judgement_day;
};

// in exactly one source file
const test::dtType dtType::judgement_day(1);

A union can not contain class instances. union不能包含类实例。 Consider a union containing two objects, when creating that union which constructor should be called? 考虑一个包含两个对象的联合,在创建应该调用构造函数的联合时? The compiler can't call both constructors as that may cause one object to contain faulty values. 编译器不能调用两个构造函数,因为这可能导致一个对象包含错误的值。

For as long as the union contains a dtType member, then dtType cannot have constructors. 只要union包含一个dtType成员,那么dtType就不能有构造函数。

However you can change your contructors to be void Set() functions instead, to be called implicitly after a construction. 但是,您可以将构造函数更改为void Set()函数,而不是在构造之后隐式调用。

For example: 例如:

int main()
{
    test::dtType judgement_day;
    judgement_day.Set(1);

    test::dtUnion x;
    x.dt.Set(1);

    assert(judgement_day == x.dt);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM