[英]What type of C++ cast should be used to cast a pointer to a struct to its first member?
X
is defined as the following: X
定义如下:
struct X
{
Y y;
// more fields...
int a;
};
I have a variable of type X
.我有一个
X
类型的变量。 However, I would like to cast it to the type of its first member, in order to pass that into a function.但是,我想将其转换为其第一个成员的类型,以便将其传递给 function。 I know that the C Standard permits it (and I suppose the C++ one does so as well).
我知道 C 标准允许这样做(我想 C++ 也允许这样做)。
In C I would do it like so:在 C 我会这样做:
X x;
Y* y = (Y*) x;
doStuff(y);
What type of cast is the right one in C++ for this? C++ 中的哪种类型的演员适合这个?
static_cast
or reinterpret_cast
? static_cast
还是reinterpret_cast
?
None.没有任何。
You can't mess around with objects using pointers like that.您不能使用这样的指针来处理对象。 C++ is not C, and these are not "just bytes" (contrary to popular belief).
C++ 不是 C,这些不是“只是字节”(与流行的看法相反)。
And you don't need to!你不需要!
Pass &x.y
instead;改为传递
&x.y
; it's already the Y*
you want.它已经是你想要的
Y*
。
I'd always recommend using static_cast
instead of reinterpret_cast
in any situation where the static_cast
isn't rejected by the compiler.在编译器不拒绝
static_cast
的任何情况下,我总是建议使用static_cast
而不是reinterpret_cast
。 If possible try to avoid doing any casting at all - in this case you probably want: Y* y = &x.y
.如果可能的话,尽量避免做任何转换——在这种情况下,你可能想要:
Y* y = &x.y
。
To answer the comment:要回答评论:
In this case, I have a PROCESS_MEMORY_COUNTERS_EX variable.
在这种情况下,我有一个 PROCESS_MEMORY_COUNTERS_EX 变量。 However the WinAPI function GetProcessMemoryInfo takes a PROCESS_MEMORY_COUNTERS*.
但是 WinAPI function GetProcessMemoryInfo 采用 PROCESS_MEMORY_COUNTERS*。 The former type starts with the exact same fields as the latter, and adds a few at the end.
前一种类型以与后者完全相同的字段开头,并在末尾添加一些。 The intended usage is to pass into the function a pointer to the latter type, even if we hold a pointer to the former (larger) type.
预期用途是将指向后一种类型的指针传递给 function,即使我们持有指向前一种(更大)类型的指针。
The documentation for GetProcessMemoryInfo() states that the second parameter is: GetProcessMemoryInfo() 的文档指出第二个参数是:
A pointer to the PROCESS_MEMORY_COUNTERS or PROCESS_MEMORY_COUNTERS_EX structure that receives information about the memory usage of the process.
指向 PROCESS_MEMORY_COUNTERS 或 PROCESS_MEMORY_COUNTERS_EX 结构的指针,用于接收有关进程的 memory 使用情况的信息。
The Win32 API is a C API, and not a C++ one, so you can just use a C style cast here, or preferably a reinterpret_cast
to make your intention clearer. The Win32 API is a C API, and not a C++ one, so you can just use a C style cast here, or preferably a
reinterpret_cast
to make your intention clearer. I'd expect static_cast
to be rejected by the compiler in this case.在这种情况下,我希望编译器会拒绝
static_cast
。 Note that the third cb
parameter is there to tell the function which type of structure you actually provided - it should be set to either sizeof(PROCESS_MEMORY_COUNTERS)
or sizeof(PROCESS_MEMORY_COUNTERS_EX)
.请注意,第三个
cb
参数用于告诉 function 您实际提供的结构类型 - 它应该设置为sizeof(PROCESS_MEMORY_COUNTERS)
或sizeof(PROCESS_MEMORY_COUNTERS_EX)
。
@FredLarson In this case, I have a PROCESS_MEMORY_COUNTERS_EX variable.
@FredLarson 在这种情况下,我有一个 PROCESS_MEMORY_COUNTERS_EX 变量。 However the WinAPI function GetProcessMemoryInfo takes a PROCESS_MEMORY_COUNTERS*.
但是 WinAPI function GetProcessMemoryInfo 采用 PROCESS_MEMORY_COUNTERS*。 The former type starts with the exact same fields as the latter , and adds a few at the end.
前一种类型以与后者完全相同的字段开头,并在末尾添加一些。 The intended usage is to pass into the function a pointer to the latter type, even if we hold a pointer to the former (larger) type.
预期用途是将指向后一种类型的指针传递给 function,即使我们持有指向前一种(更大)类型的指针。
The cleanest way to accomplish that would be probably (ab)using inheritance.实现这一目标的最简单方法可能是(ab)使用 inheritance。 This way, you can have your
_EX
type share members with the base type while actually being an instance of it.这样,您可以让您的
_EX
类型与基类型共享成员,同时实际上是它的一个实例。
struct X {
int a;
int b;
};
struct X_EX : public X {
int other_member;
};
void doStuff(X*);
void foo(X_EX* ptr) {
doStuff(ptr);
}
However, do note that "Inherit-to-extend" is seen as a code smell, and something to avoid if possible nowadays.但是,请注意“继承到扩展”被视为代码异味,如果可能的话,现在应该避免。 I'd make sure to put a comment explaining why it's necessary here.
我一定会发表评论,解释为什么这里有必要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.