我已经编写了近二十年的C和C++代码,但这些语言的一个方面我从未真正理解过.我显然使用常规演员表,即
MyClass *m = (MyClass *)ptr;
Run Code Online (Sandbox Code Playgroud)
到处都是,但似乎有两种其他类型的演员,我不知道其中的区别.以下代码行之间有什么区别?
MyClass *m = (MyClass *)ptr;
MyClass *m = static_cast<MyClass *>(ptr);
MyClass *m = dynamic_cast<MyClass *>(ptr);
Run Code Online (Sandbox Code Playgroud) 如果我有一个C++枚举:
enum Foo
{
Bar,
Baz,
Bork,
};
Run Code Online (Sandbox Code Playgroud)
如何告诉编译器使用a uint16_t来实际存储枚举值?
编辑:GCC在C++ 11的实现中是否支持此功能?
声明如下:
enum DrawBoldMode : unsigned
{
DBM_NONE = 0,
DBM_ITEM = 1<<0, // bold just the nearest line
DBM_SECTION = 1<<1, // bold all lines in the same section
DBM_LINETYPE = 1<<2, // bold all lines of the same line type
DBM_POINTAGE = 1<<3, // bold all lines of the same line type
};
Run Code Online (Sandbox Code Playgroud)
如何导出DrawBoldMode的基础类型(即无符号)?
如何从流输入到枚举类型?
我可以这样做
unsigned int sex = 0;
stream >> sex;
student.m_bio.sex = static_cast<Sex>(sex);
Run Code Online (Sandbox Code Playgroud)
除此以外?
我有以下内容enum:
enum enumLoanPaymentPolicy
{
Unspecified = 0,
InterestInArrears = 1 << 1,
InterestInAdvance = 1 << 2,
RoundUpRepayments = 1 << 3,
RoundInterest = 1 << 4,
RoundUpFlows = 1 << 5,
RoundingMask = RoundUpRepayments | RoundInterest | RoundUpFlows,
};
Run Code Online (Sandbox Code Playgroud)
然后在其他地方,给定foo这个枚举的value(),我想提取与之相关的位集Round.
我用foo & RoundingMask它,但我应该使用什么类型?
理想情况下我会使用somethingorother(enumLoanPaymentPolicy) bar = foo & RoundingMask其中somethingorother有点像decltype.这甚至可能吗?