I'm trying to understand how to access data in this author's class. I don't seem to be able to access via:
auto c = MyClass();
c.SomeData.a;
class MyClass {
public:
struct SomeData {
typedef MyClass SomeType;
static constexpr uint32_t a;
...
}
struct SomeOtherData {
typedef MyClass SomeType;
static constexpr uint32_t b;
...
}
Are all those anonymous structs different ways to view the same data like a union?
The SomeData
and SomeOtherData
structs are inner types of MyClass
, they are not non-static data fields of MyClass
, which is why you can't access them via your c
variable the way you are. MyClass
would need to declare actual member fields using those inner types, eg:
class MyClass {
public:
struct SomeData {
typedef MyClass SomeType;
static constexpr uint32_t a;
...
};
SomeData sd;
struct SomeOtherData {
typedef MyClass SomeType;
static constexpr uint32_t b;
...
};
SomeOtherData sod;
...
};
MyClass c;
c.sd.a;
c.sod.a;
But, since the fields you are actually trying to access are static
, just refer to them by their owning type only, you don't need a MyClass
variable at all, eg:
MyClass::SomeData::a;
MyClass::SomeOtherData::b;
And no, these inner structs are nothing like union
s.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.