相关疑难解决方法(0)

常规演员与static_cast与dynamic_cast

我已经编写了近二十年的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++ pointers casting

1661
推荐指数
8
解决办法
67万
查看次数

如何更改枚举(C++)使用的整数类型?

如果我有一个C++枚举:

enum Foo
{
  Bar,
  Baz,
  Bork,
};
Run Code Online (Sandbox Code Playgroud)

如何告诉编译器使用a uint16_t来实际存储枚举值?

编辑:GCC在C++ 11的实现中是否支持此功能?

c c++ enums

24
推荐指数
4
解决办法
2万
查看次数

如何获得枚举的基本类型?

声明如下:

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的基础类型(即无符号)?

c++ templates c++11

10
推荐指数
2
解决办法
1439
查看次数

从流输入到枚举类型

如何从流输入到枚举类型?

我可以这样做

unsigned int sex = 0;
stream >> sex;
student.m_bio.sex = static_cast<Sex>(sex);
Run Code Online (Sandbox Code Playgroud)

除此以外?

c++ enums istream

8
推荐指数
1
解决办法
5300
查看次数

你能得到枚举的基础类型吗?

我有以下内容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.这甚至可能吗?

c++

0
推荐指数
1
解决办法
78
查看次数

标签 统计

c++ ×5

enums ×2

c ×1

c++11 ×1

casting ×1

istream ×1

pointers ×1

templates ×1