static_cast vs reinterpert_cast [英] static_cast vs reinterpert_cast

查看:83
本文介绍了static_cast vs reinterpert_cast的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个

类A:公共B {...成员函数......数据成员};


我正在做以下事情

A * p =新A();

void * p = static_cast< void *> ;(p);

factory_instance-> process(p);


这里p被传递给一个接受void ptr的函数。该函数

需要将其强制转换

A * pp = static_cast< A *>(p);


函数在工厂只接受void * p,具体的

实现需要将指针强制转换回预期的类

并使用它。


问题:虽然两者都工作正常,但我想知道更多

适合这种情况static_cast或reinterpert_cast


书籍建议

static_cast =" Forgood-behaved"和合理地/ b $ b表现良好演员,包括你现在可能没有演员的事情。

reinterpret_cast =演变为完全不同的意思。关键

是你需要回到原来的类型才能安全使用




但是在这种情况下,我无法解释句子:-)

Hi,

I have a
class A : public B {...member functions......data members};

and am doing the following
A *p=new A();
void *p=static_cast<void *>(p);
factory_instance->process(p);

Here p is passed to a function, which accepts void ptr. That function
need to cast it back
A *pp=static_cast<A *>(p);

The function is in the factory which accepts void *p only, the specific
implementations need to cast the pointer back to the expected class
and use it.

Question:Though both works fine, yet I want to know what is more
appropriate in this situation static_cast OR reinterpert_cast

The books suggests
static_cast= "For "well-behaved" and "reasonably
well-behaved" casts,including things you might now do without a cast
reinterpret_cast=To cast to a completely different meaning. The key
is that you''ll need to cast back to the original type to use it
safely.

But I am not able to interpret the sentences in this context :-)

推荐答案



Rahul写道:

Rahul wrote:

>

我正在做以下事情

A * p = new A();

void * p = static_cast< void *>(p);

factory_instance-> process(p);


这里p被传递给一个接受void ptr的函数。该函数

需要将其转回

A * pp = static_cast< A *>(p);
>
and am doing the following
A *p=new A();
void *p=static_cast<void *>(p);
factory_instance->process(p);

Here p is passed to a function, which accepts void ptr. That function
need to cast it back
A *pp=static_cast<A *>(p);



在调用process()之前不要将其转换为void *,它不需要而且

减少了你拥有的数量更好 - 只需传递A *就像这样:

A * pa = new A();

factory_instance-> process(pa);

回到A *内部进程()应该没问题。

如果process()采用A *(或者某些Base-of-A *)显然会更好)

首先。

Don''t cast to void* before calling process(), it''s not needed and the
less casts you have the better - just pass the A* as is like this:
A* pa = new A();
factory_instance->process(pa);
Casting back to A* inside process() should be fine.
It''d obviously be better if process() took an A* (or some Base-of-A*)
in the first place.


Rahul写道:
Rahul wrote:




我有一个

类A:公共B {...成员函数......数据成员};


我正在做以下事情

A * p = new A();

void * p = static_cast< void *> (p);

factory_instance-> process(p);


这里p被传递给一个接受void ptr的函数。
Hi,

I have a
class A : public B {...member functions......data members};

and am doing the following
A *p=new A();
void *p=static_cast<void *>(p);
factory_instance->process(p);

Here p is passed to a function, which accepts void ptr.



不需要static_cast。你可以将指向一个对象的指针转换成一个隐含的void的b / b
指针。

No need for a static_cast. You can convert a pointer to an object into a
pointer to void implicitly.


该函数

需要把它丢回

A * pp = static_cast< A *>(p);


该函数在工厂中只接受void * p ,具体的

实现需要将指针强制转换回预期的类

并使用它。


问题:虽然两者都有工作正常,但我想知道更多

适合这种情况static_cast或reinterpert_cast
That function
need to cast it back
A *pp=static_cast<A *>(p);

The function is in the factory which accepts void *p only, the specific
implementations need to cast the pointer back to the expected class
and use it.

Question:Though both works fine, yet I want to know what is more
appropriate in this situation static_cast OR reinterpert_cast



static_cast。一般来说,如果它能完成工作,你可以说你应该选择static_cast

而不是reinterpret_cast。

static_cast. Generally, one could say that you should choose static_cast
over reinterpret_cast if it does the job.


这些书建议

static_cast =" Forgood-behaved"和合理地/ b $ b表现良好演员,包括你现在可能没有演员的事情。

reinterpret_cast =演变为完全不同的意思。关键

是你需要回到原来的类型才能安全使用




但是在这种情况下,我无法解释句子:-)
The books suggests
static_cast= "For "well-behaved" and "reasonably
well-behaved" casts,including things you might now do without a cast
reinterpret_cast=To cast to a completely different meaning. The key
is that you''ll need to cast back to the original type to use it
safely.

But I am not able to interpret the sentences in this context :-)



你可以把它翻译成reinterpret_cast比

更邪恶的static_cast" ;-)

You could translate it to "reinterpret_cast is more evil than
static_cast" ;-)





static_cast用于编译器将
自动转换,例如char转换为int,在你的情况下

A *转换为void *。正如书中所提到的那样,reinterpret_cast被用于低级别的黑客攻击,特别是当你知道自己在做什么时,例如:


struct S

{

int a,b;

};


int main()

{

s s;

sa = 10;

sb = 20;


int * p = reinterpret_cast< int *>(& s);

cout<< " A =" << * p<< endl;

++ p;

cout<< " B =" << * p<< endl;

}


通常reinterpret_cast应该在static_cast工作的地方工作。

理想情况下在C ++中我们应该避免使用void *,因为我们放弃了

类型的安全性。同样在你的情况下你似乎有一个类

层次结构(A,B),所以使用带有基本ptrs的虚函数将是

cleaner(如果虚拟fn开销)不是问题。


谢谢和问候

Sonison James


Rahul写道:
Hi,

static_cast is meant to be used for cases which the compiler would
automatically be able to convert, such as char to int and in your case
A* to void*. reinterpret_cast is used, as the book mentions, for
low-level hacks, especially when you know what you are doing, eg:

struct S
{
int a, b;
};

int main()
{
S s;
s.a = 10;
s.b = 20;

int* p = reinterpret_cast<int*>(&s);
cout << "a=" << *p << endl;
++p;
cout << "b=" << *p << endl;
}

Typically reinterpret_cast should work where a static_cast works.
"Ideally" in C++ we should avoid using void* as much because we loose
type safety. Also in your case you do seem to be having a class
hierarchy (A,B), so using virtual functions with base ptrs would be
cleaner (if virtual fn overhead is not an issue).

Thanks and regards
Sonison James

Rahul wrote:




我有一个

类A:公共B {...成员函数.. ....数据成员};


我正在做以下事情

A * p = new A();

void * p = static_cast< void *>(p);

factory_instance-> process(p);


这里p被传递给一个函数,接受void ptr。该函数

需要将其强制转换

A * pp = static_cast< A *>(p);


函数在工厂只接受void * p,具体的

实现需要将指针强制转换回预期的类

并使用它。


问题:虽然两者都工作正常,但我想知道更多

适合这种情况static_cast或reinterpert_cast


书籍建议

static_cast =" Forgood-behaved"和合理地/ b $ b表现良好演员,包括你现在可能没有演员的事情。

reinterpret_cast =演变为完全不同的意思。关键

是你需要回到原来的类型才能安全使用




但是我无法解释上下文中的句子:-)
Hi,

I have a
class A : public B {...member functions......data members};

and am doing the following
A *p=new A();
void *p=static_cast<void *>(p);
factory_instance->process(p);

Here p is passed to a function, which accepts void ptr. That function
need to cast it back
A *pp=static_cast<A *>(p);

The function is in the factory which accepts void *p only, the specific
implementations need to cast the pointer back to the expected class
and use it.

Question:Though both works fine, yet I want to know what is more
appropriate in this situation static_cast OR reinterpert_cast

The books suggests
static_cast= "For "well-behaved" and "reasonably
well-behaved" casts,including things you might now do without a cast
reinterpret_cast=To cast to a completely different meaning. The key
is that you''ll need to cast back to the original type to use it
safely.

But I am not able to interpret the sentences in this context :-)


这篇关于static_cast vs reinterpert_cast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆