C++ 将类指针转换为空指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18929225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:20:28  来源:igfitidea点击:

Casting Class Pointer to Void Pointer

c++casting

提问by domlao

How can I able to cast a class pointer to a generic pointer like void*? Like is this code valid?,

我怎样才能将类指针转换为像 void* 这样的通用指针?像这个代码有效吗?,

class CFoo
{
   int a;
public:
   CFoo():a(1){}
   ~CFoo(){}
   getNum(){return a;}
};

void tfunc(void* data)
{
    CFoo* foo = static_cast<CFoo*>(data);
    std::cout << "Number: " << foo->getNum();
    delete foo;
}

int main()
{
   CFoo* foo = new CFoo;
   void* dt = static_cast<void*>(foo);
   tfunc(dt); // or tfunc(static_cast<void*>(food));

   return 0;
}

回答by domlao

This is perfectly valid. Here is what standard has to say about it:

这是完全有效的。以下是标准对此的看法:

§4.10 Pointer conversions

2 An rvalue of type "pointer to cvT," where Tis an object type, can be converted to an rvalue of type "pointer to cvvoid." The result of converting a "pointer to cvT" to a "pointer to cvvoid" points to the start of the storage location where the object of type Tresides, as if the object is a most derived object (1.8) of type T(that is, not a base class subobject).

§4.10 指针转换

2 类型为“指向cv 的指针”的右值T,其中T是对象类型,可以转换为类型为“指向cv 的指针”的右值void。一个“指针转换的结果CVT”发送给“指针CVvoid”指向其中类型的对象的存储位置的开始T所在,因为如果对象是类的一个最派生对象(1.8) T(即,不是基类子对象)。

which means you can convert your pointer to class to a void pointer. And ...

这意味着您可以将指向类的指针转换为空指针。和 ...

§5.2.9 Static cast

10 An rvalue of type "pointer to cvvoid" can be explicitly converted to a pointer to object type. A value of type pointer to object converted to "pointer to cvvoid" and back to the original pointer type will have its original value.

§5.2.9 静态转换

10 “指向cv 的指针void”类型的右值可以显式转换为指向对象类型的指针。转换为“指向cv 的指针void”并返回原始指针类型的对象指针类型值将具有其原始值。

which means you can use static_castto convert a void pointer back to an original class pointer.

这意味着您可以使用static_cast将 void 指针转换回原始类指针。

Hope it helps. Good Luck!

希望能帮助到你。祝你好运!

回答by quamrana

In C++ you don't need the static cast to get tovoid*:

在 C++ 中,您不需要静态转换即可void*

int main()
{
    CFoo* foo = new CFoo;
    void* dt = foo;
    tfunc(dt); // or tfunc(foo);

    return 0;
}

NB: your implementation of tfunc()is quite correct in that it doesneed the cast.

注意:您的实现tfunc()是非常正确的,因为它确实需要演员阵容。

回答by billz

Is this valid?

这是有效的吗?

Yes, it is valid as per standard § 5.2.9.7

是的,它根据标准 § 5.2.9.7 有效

A prvalue of type “pointer to cv1 void” can be converted to a prvalue of type “pointer to cv2 T,” where T is an object type and cv2 is the same cv-qualification as, or greater cv-qualification than, cv1. The null pointer value is converted to the null pointer value of the destination type. A value of type pointer to object converted to “pointer to cv void” and back, possibly with different cv-qualification, shall have its original value.[ Example:

“指向 cv1 void 的指针”类型的纯右值可以转换为“指向 cv2 T 的指针”类型的纯右值,其中 T 是对象类型,而 cv2 与 cv1 具有相同的 cv 限定,或者比 cv1 更高的 cv 限定。空指针值转换为目标类型的空指针值。转换为“指向 cv void 的指针”并返回的指向对象类型的值,可能具有不同的 cv 限定,应具有其原始值。[ 例子:

T* p1 = new T;
const T* p2 = static_cast<const T*>(static_cast<void*>(p1));
bool b = p1 == p2; // b will have the value true.

回答by UfnCod3r

    CFoo* foo = new CFoo;
    void* dt = (void*)foo;