#include <iostream>
#include <cstring>using namespace std;/* 联合体的使用,最主要的效果便是内存空间的节省,当然额外的附加作用便是灵活性,但是这种灵活性是以牺牲 可读性为代价的 */
union T {string s;//因为string 是非POD类型,string有non-trivial的构造函数,故而系统会删除T的默认构造函数//error: union member 'T::s' with non-trivial 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string() [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'|int n;
public:T() { new (&s) string; } //placement new机制,直接在地址上强制类型初始化~T() { s.~string(); }
};struct Student{Student(bool g, int a) : gender(g), age(a) {}bool gender;int age;
};class Singer {
public:enum Type {STUDENT, NATIVE, FOREIGNER};Singer(bool g, int a): s(g,a) { t = STUDENT; }Singer(int i) : id(i) { t = NATIVE; }Singer(const char* n, int s) {int size = (s>9) ? 9 : s;memcpy(name, n, size);name[s] = '\0';t = FOREIGNER;}~Singer() {}
private:Type t;union { //C++11引入的匿名的非受限结构体,从而配合类,可实现“变长成员”定义的效果variant memberStudent s;int id;char name[10];};
};int main()
{T t; //因为T的默认构造函数被删除了,所以如果不显示定义,则会出现error: use of deleted function 'T::T()'Singer a(true, 24);Singer b(85767);Singer c("Jack chou", 41);return 0;
}
详细解决方案
杂货边角(20):匿名非受限联合体实现类的“变长成员”variant member
热度:15 发布时间:2023-12-16 21:51:29.0
相关解决方案
- fatal error; call to a member function query() on a non-object异常求回答啊 php+sqlite
- Do you think Japan should become a permanent member of the United Nations Security Council ?解决思路
- Do you think Japan should become a permanent member of the United Nations Security Council ?该如何处理
- BC30456: 'ExecuteReader' is not a member of 'System.Data.OleDb.OleDbDataAdapter'.个错误如何解决呀
- Response.Cookies("Member").Expires=Date + 30,请问30的单位是什么? ...
- Fatal error: Call to a member function query() on a non-object in G:\wamp\apache,该如何解决
- PHP对象编程有关问题,Call to a member function hello() on a non-object
- 【】Fatal error: Call to a member function * on a non-object
- php中出现“Call to a member function Execute() on a non-object”的有关问题
- phpmyadmin出现了Fatal error: Call to a member function getName() 如何都打不开了
- php用mysqli模块连数据库Fatal error: Call to a member function query() 。解决思路
- Call to a member function fetch() on a non-object异常
- Call to a member function on a non-object,分不够再加
- Class com.googlecode.jsonplugin.JSONWriter can not access a member of class
- ant 编绎 jsp 出现异常 Package contains no member type of this na
- JSON出错:can not access a member of class * with modifiers "public"
- org.apache.struts2.json.JSONWriter can not access a member of class org.sp,该怎么解决
- org.apache.struts2.json.JSONWriter can not access a member of class org.sp,该如何解决
- Error calling IDispatch member WorkSheets,该如何解决
- Error calling IDispatch member WorkSheets解决方案
- 用qt交叉编译程序时出现QString has no member name latin1异常是什么原因,怎么解决呢
- evc 4.0 异常提示: cannot add new member
- CreatePolygonRgn' : is not a member of 'CRgn',该如何处理
- Call to a member function get() on null 这个如何解决呢
- Fatal error: Call to a member function fetch_array() on a non-object in 62 line,该如何处理
- 求大神纠错,多谢!Fatal error: Call to a member function query() on a non-object
- Call to a member function *() on a non-object in common.lib.php on line
- 【ThinkPHP】怎么实现这样的重写 http://localhost/member/register => Home/MemberDo/register
- Fatal error: Call to a member function fpage() on a non-object解决方案
- Fatal error: Call to a member function query() on a non-object in G:\wamp\apache,该如何解决