从const对< const unsigned char *,size_t> *转换为constpair< unsigned char *,size_t> * [英] Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>*

查看:68
本文介绍了从const对< const unsigned char *,size_t> *转换为constpair< unsigned char *,size_t> *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




是否可以进行C ++ - 来自

const pair< const unsigned char *,size_t> *

to

const pair< unsigned char *,size_t> *




Alex Vinokur

解决方案

2008-10-12 13:16,Alex Vinokur写道:





是否可以进行C ++ - 从

const对< const unsigned char *,size_t> *

转换为

const pair< unsigned char *,size_t> *




const_cast可能有效,但我认为这不算什么就可以了。

constness,你应该使用reinterpret_cast。


-

Erik Wikstr ?? m


10月12日,7:16 * pm, Alex Vinokur< ale ... @ users.sourceforge.netwrote:





是否可以做C ++ - 来自

const pair< const unsigned char *,size_t> *

to

const pair< unsigned char *,size_t> *




const pair< const unsigned char *,size_t> * p1 = 0;

const pair< ; unsigned char *,size_t> * p2 =

reinterpret_cast< const pair< unsigned char *,size_t> *>(p1);


但reinterpre_cast应该尽可能避免。

*实际上*,我想你可以这样做:


char * s = const_cast< char *> (p1-> first);


或者你能告诉我你所处的情景吗?


-

最好的问候

Barry


10月12日,2:22 * pm,Barry< dhb2 ... @ gmail.comwrote:


10月12日,7:16 * pm,Alex Vinokur< ale ... @ users.sourceforge.netwrote:




是否可以进行C ++ - 来自

const pair< const unsigned char *,size_t> *

to

const pair< unsigned char *,size_t> *




* const pair< const unsigned char *,size_t> * p1 = 0;

* const pair< unsigned char *,size_t> * p2 =

* * * * reinterpret_cast< const pair< unsigned char *,size_t> *>(p1);


但应避免使用reinterpre_cast如果可能的话。

*实际上*,我想你可以这样做:


* char * s = const_cast< char *>(p1 - >第一个);


或者你能告诉我你的情景吗?



我有函数foo1( const pair< unsigned char *,size_t> * p);

我还需要函数foo2(const pair< const unsigned char *,size_t> *

p)与foo1()相同的东西。


目前

void foo2(const pair< const unsigned char *,size_t> * p)

{

//我想在这里使用C ++ - 样式转换

const pair< unsigned char *,size_t> * p1 =(const pair< unsigned

char *,size_t> *)p;

foo1(p1);

}


Alex Vinokur


Hi,

Is it possible to do C++-casting from
const pair<const unsigned char*, size_t>*
to
const pair<unsigned char*, size_t>*
?

Alex Vinokur

解决方案

On 2008-10-12 13:16, Alex Vinokur wrote:

Hi,

Is it possible to do C++-casting from
const pair<const unsigned char*, size_t>*
to
const pair<unsigned char*, size_t>*
?

const_cast might work, but I don''t think this counts as casting away
constness, you should probably use reinterpret_cast.

--
Erik Wikstr??m


On Oct 12, 7:16*pm, Alex Vinokur <ale...@users.sourceforge.netwrote:

Hi,

Is it possible to do C++-casting from
const pair<const unsigned char*, size_t>*
to
const pair<unsigned char*, size_t>*
?

const pair<const unsigned char*, size_t>* p1 = 0;
const pair<unsigned char*, size_t>* p2 =
reinterpret_cast<const pair<unsigned char*, size_t>*>(p1);

but reinterpre_cast should be avoided if possible.
*Practically*, I wonder you can just do it this way:

char* s = const_cast<char*>(p1->first);

Or can you tell me the scenario you are in?

--
Best Regards
Barry


On Oct 12, 2:22*pm, Barry <dhb2...@gmail.comwrote:

On Oct 12, 7:16*pm, Alex Vinokur <ale...@users.sourceforge.netwrote:

Hi,

Is it possible to do C++-casting from
const pair<const unsigned char*, size_t>*
to
const pair<unsigned char*, size_t>*
?


* const pair<const unsigned char*, size_t>* p1 = 0;
* const pair<unsigned char*, size_t>* p2 =
* * * * reinterpret_cast<const pair<unsigned char*, size_t>*>(p1);

but reinterpre_cast should be avoided if possible.
*Practically*, I wonder you can just do it this way:

* char* s = const_cast<char*>(p1->first);

Or can you tell me the scenario you are in?

I have function foo1 (const pair<unsigned char*, size_t>* p);
I need also function foo2 (const pair<const unsigned char*, size_t>*
p) that does the same thing as foo1().

Currently
void foo2 (const pair<const unsigned char*, size_t>* p)
{
// I would like to use here C++-style casting
const pair<unsigned char*, size_t>* p1 = ( const pair<unsigned
char*, size_t>* ) p;
foo1(p1);
}

Alex Vinokur


这篇关于从const对&lt; const unsigned char *,size_t&gt; *转换为constpair&lt; unsigned char *,size_t&gt; *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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