C++ 语法问题

发布于 2024-07-17 04:52:49 字数 205 浏览 9 评论 0原文

下面的语法是什么意思?

typedef void* hMyClass; //typedef as a handle or reference
hMyClass f = &something;
const MyClass& foo = static_cast<MyClass&>(*f);
foo.bar();

What does the following syntax mean?

typedef void* hMyClass; //typedef as a handle or reference
hMyClass f = &something;
const MyClass& foo = static_cast<MyClass&>(*f);
foo.bar();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我恋#小黄人 2024-07-24 04:52:49

static_cast 意味着系统实际上并不尝试确保当您从一种引用类型转换为另一种引用类型时,您要转换的内容实际上是目标类型的实例(与动态转换不同)。

因此,您告诉编译器,您足够聪明,知道 hMyClass 中包含的地址以及来自某些内容的地址实际上包含 MyClass 的实例,并且您对如果您将发生的不可预测的事情承担全部责任错了。

你的“东西”是什么类型? 您也可能在那里遇到错误。 你可能想要一个&something。

A static_cast means that the system does not actually try to make sure that when you are converting from one reference type to another, the thing you are converting is actually an instance of the target type (unlike dynamic casts).

Hence, you are telling the compiler that you are smart enough to know that the address which is contained in hMyClass and which came from something actually contains an instance of MyClass, and that you are taking full responsibility for the unpredictable things that will happen if you are wrong.

What is the type of your "something"? You could also have an error there. You may want a &something.

假面具 2024-07-24 04:52:49

这实际上是无效的。 您正在将值分配给类型,而不是第 2 行上的变量。

This actually isn't valid. You're assigning a value to a type, not a variable on line 2.

不再见 2024-07-24 04:52:49

本质上,有人将 MyClass 指针存储在 void 指针中,很可能将其传递给回调。 该代码可能已被回调,正在将其强制转换回来,以将其用作 MyClass。

此外,正如 eagerwishes 所指出的那样,它还存在语法错误。

Essentially, somebody has stored an MyClass pointer in a void pointer, likely passing it to callback. This code, having presumably been called back, is casting it back, to use it as a MyClass.

Also, it has syntax errors, as eagerwishes notes.

夜唯美灬不弃 2024-07-24 04:52:49

如果没有更多上下文,很难确切地说出这“意味着”什么。 我假设您正在处理其他人的代码,并且您显示的行似乎分散在许多函数中。

我对代码意图的最佳猜测是,这是一种处理第三方代码/库的机制。 在 C++ 中,将(非模板化)第三方库与您自己的类型一起使用是很常见的。 如果这些库需要临时保存您创建和拥有的数据,您需要一种方法来让库访问您的数据,即使该库不知道您的类型。 一个例子是回调/事件功能。 如果一个库在事件发生时异步通知您,您需要给它一个函数指针和用户定义的数据,以便当调用该函数时,您知道如何处理它。 库通常采用 void* 指针来存储用户提供的数据。 在 C++ 中,通常只传递用户提供的数据的对象实例,然后委托给该对象来处理回调。 代码如下所示:

// 3rd-party library API
typedef void (*PingNotifyPtr)(void*);
void NotifyOnPing(PingNotifyPtr, void* userData);
// User Code
void MyNotify(void* myData)
{
   MyClass* ptr = (MyClass*)myData;
   // do something with ptr
}
void Main()
{
   MyClass *pClass = new MyClass();
   NotifyOnPing(&MyNotify, pClass);
   // the callback is now armed with all the data it needs.
}

许多库通过声明“void*”参数来实现此目的。 这是指向某个内存位置的原始指针。 该位置可以是任何内容,例如整数、类实例、字符串等。第 3 方库保留该指针并在某个时刻将其返回给您的代码。

示例代码的第三行采用 void* 指针并将其转换为对类实例的引用。 C++ 引用类似于指针,只不过它们不能为 NULL 并且它们使用值语法(例如,使用“.”运算符而不是“->”运算符。)示例代码的第 4 行然后使用 re 上的方法。 - 构成类实例。

无论如何,如果您正在处理的代码库中所有 4 行代码都是连续的,则该代码意味着某人对 C++ 一无所知。 这是在一行中执行相同代码的更紧凑的方法:

((const MyClass*)something)->Foo();

或者,如果您更喜欢值语法:

(*((const MyClass*)something)).Foo();

祝您好运!

It's hard to say exactly what this "means" without more context. I presume you're working on someone else's code and the lines you're showing appear scattered across a number of functions.

My best guess as to the code intent is that this is a mechanism to deal with 3rd-party code/libraries. It is common in C++ to use (non-templated) 3rd-party libraries with your own types. If those libraries need to temporarily hold data that you created and own, you need a way to give the library access to your data even though the library doesn't know about your types. One example is a callback/event feature. If a library will asynchronously notify you when an event occurs, you need to give it a function pointer and also user-defined data so that when the function is called, you know what to do with it. Libraries commonly take a void* pointer for this user-supplied data. It is common in C++ to just pass in an object instance for the user-supplied data and then delegate to that object to handle the callback. Here's what the code would look like:

// 3rd-party library API
typedef void (*PingNotifyPtr)(void*);
void NotifyOnPing(PingNotifyPtr, void* userData);
// User Code
void MyNotify(void* myData)
{
   MyClass* ptr = (MyClass*)myData;
   // do something with ptr
}
void Main()
{
   MyClass *pClass = new MyClass();
   NotifyOnPing(&MyNotify, pClass);
   // the callback is now armed with all the data it needs.
}

Many libraries accomplish this by declaring "void*" arguments. This is a raw pointer to some memory location. Anything could be at that location, an integer, a class instance, a string, etc. The 3rd-party library holds onto that pointer and returns it back to your code at some point.

The 3rd line of your example code is taking the void* pointer and casting it to a reference to a class instance. C++ references are like pointers except they cannot be NULL and they use value syntax (using the '.' operator instead of the '->' operator, for example.) The 4th line of your example code then uses a method on the re-constituted class instance.

Anyway, if all 4 lines of code are consecutive in the code base you're dealing with, that code means that someone's ignorant of C++. Here's a more compact way to do the same code in one line:

((const MyClass*)something)->Foo();

or, if you prefer value syntax:

(*((const MyClass*)something)).Foo();

Good Luck!

避讳 2024-07-24 04:52:49

void* 通常用于通用指针。

在 C# 中,它类似于:

object o = new XmlDocument();

object o = new List();

但是,在 C++ 中,几乎没有强制执行类型安全。 IIRC,static_cast 与 (cast) 相同,因为不进行运行时类型检查。

void* is commonly used for a generic pointer.

In C# it would be similar to something like:

object o = new XmlDocument();

object o = new List();

However, in C++, there is very little type safety being enforced. IIRC, static_cast is the same as (cast), in that no run-time type checks are made.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文