简体   繁体   English

应该使用哪种类型的 C++ 类型转换来将指向结构的指针转换为它的第一个成员?

[英]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.

相关问题 如何在C ++中将成员变量指针转换为泛型类型 - How to cast member variable pointer to generic type in C++ C++中dynamic_pointer_cast的作用是什么? 什么时候使用? dynamic_pointer_cast 的替代方案是什么? - What is the use of dynamic_pointer_cast in C++? When is it used? What are alternatives for dynamic_pointer_cast? 将指向struct的指针强制转换为指向该结构的唯一成员的指针 - Cast a pointer to struct to a pointer to the only member of that struct 在C ++中将成员函数指针“强制转换”为函数指针的最简单方法是什么? - What is the simplest way to “cast” a member function pointer to a function pointer in C++? C ++将派生类的成员指针转换为基类指针 - c++ cast member pointer of derived Class to base class pointer C++ 将对象转换为其原始类型 - C++ cast object to its original type 如果我在c ++中转换(指向类A的指针)(指向其子类B的指针)会发生什么 - what happens if I cast (a pointer to class A) to (a pointer to its subclass B) in c++ 将结构转换为与其第一组成员匹配的类型的正确方法是什么? - What is the correct way to cast a struct to a type that matches its first set of members? C / C ++中的类型转换到底是什么? - What exactly is a type cast in C/C++? C ++:转换/转换void指针以引用结构 - C++: Convert/Cast void pointer to struct reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM