提问人:Itachi Uchiwa 提问时间:8/24/2021 最后编辑:timrauItachi Uchiwa 更新时间:8/30/2022 访问量:60
C++ 入门第 5 版联合和类类型成员
C++ Primer 5th edition unions and members of class type
问:
您好,我有这个来自 C++ 入门第 5 版第 19.6 章联合:
class Token { public: // copy control needed because our class has a union with a string member // defining the move constructor and move-assignment operator is left as an exercise Token(): tok(INT), ival{0} { } Token(const Token &t): tok(t.tok) { copyUnion(t); } Token &operator=(const Token&); // if the union holds a string, we must destroy it; see § 19.1.2 (p. 824) ~Token() { if (tok == STR) sval.~string(); } // assignment operators to set the differing members of the union Token &operator=(const std::string&); Token &operator=(char); Token &operator=(int); Token &operator=(double); private: enum {INT, CHAR, DBL, STR} tok; // discriminant union { // anonymous union char cval; int ival; double dval; std::string sval; }; // each Token object has an unnamed member of this unnamed union type // check the discriminant and copy the union member as appropriate void copyUnion(const Token&); };
当我们从复制构造函数调用时,联合成员将被默认初始化,这意味着将初始化的第一个成员。因为我们不是第一个成员,所以我们知道该成员不持有 .在赋值运算符中,可能已经持有 .我们将直接在赋值运算符中处理这种情况。这种方式可以假设,如果它的参数包含一个 ,则必须构造自己的:
copyUnion
union
string
union
string
union
string
copyUnion
string
copyUnion
string
void Token::copyUnion(const Token &t) { switch (t.tok) { case Token::INT: ival = t.ival; break; case Token::CHAR: cval = t.cval; break; case Token::DBL: dval = t.dval; break; // to copy a string, construct it using placement new; see case Token::STR: new(&sval) string(t.sval); break; } }
- 这本书没有展示复制构造函数的实现,但重要的是我;他说:“当我们从复制构造函数调用时,union 成员将被默认初始化,这意味着 will 的第一个成员已被初始化......”但我认为作为 a 一部分的对象的任何成员都没有默认初始化,因此必须在 copy-ctor-init-list 中显式初始化它。
copyUnion
union
union
class
这是我的例子:
struct A{
A(){std::cout << "A()\n";}
A(A const&){std::cout << "A(A const&)\n";}
~A(){std::cout << "~A()\n";}
};
struct Foo{
Foo();
Foo(Foo const&);
~Foo();
enum {CLS_A, INT, CHAR} disc_; // discriminant
union {
A a_; // first member is of class type that's defined its own def-ctor
int age_;
char degree_;
};
};
Foo::Foo() :
disc_(CLS_A),
a_(){ // explicitly initializing the member a_ otherwise it is not initialized
std::cout << "Foo()\n";
}
Foo::Foo(Foo const&) : /*Foo()*/ { // a_ is not default-init so only if I un-comment the call the def-tor
std::cout << "Foo(Foo const&)\n";
}
Foo::~Foo(){
std::cout << "~Foo()\n";
if(disc_ == CLS_A)
a_.~A(); // not automatically called
}
int main(){
Foo f;
Foo f2 = f;
}
输出:
A()
Foo()
Foo(Foo const&)
~Foo()
~Foo()
~A()
- 正如你可以的那样,dtor 只调用一次,因为对象的成员尚未在 copy-ctor 中初始化。
A
a_
f2
现在,如果我取消注释从 copy-ctor 对 default-ctor 的调用:A()
Foo::Foo(Foo const&) : Foo(){/*..*/}
输出:
A()
Foo()
A()
Foo()
Foo(Foo const&)
~Foo()
~A()
~Foo()
~A()
下面是他的复制构造函数:
Token(const Token &t): tok(t.tok) { // didn't initialize any member of the `union`
copyUnion(t);
}
现在这没问题。那么,在本书的复制构造函数中,第一个成员是默认初始化的是什么意思呢?谢谢!union
答:
1赞
eerorika
8/24/2021
#1
这本书是错误的。由于没有任何变体成员的默认成员初始值,并且由于任何变体成员都没有成员初始值,因此任何变体成员都没有初始化。第一个变体成员未处于活动状态(其他任何变体成员均未处于活动状态)。
评论
0赞
Itachi Uchiwa
8/24/2021
好的,非常感谢。我一直在努力理解它。
0赞
Itachi Uchiwa
8/24/2021
为了简洁起见,我刚刚将 copy-ctor 中的初始化委托给 default-ctor。
评论