casting template using const_cast

P

puru

I have a template class like below.
template <TInt S> class XYZ
{

}

const XYZ<10> abc; //Instance of the class

How do remove the constantness of abc using const_cast. I mean, I want
the syntax for that. Something like const_cast<XYZ<10>> abc gives
compiler error.
 
B

Barry

I have a template class like below.
template <TInt S> class XYZ
{

}

const XYZ<10> abc; //Instance of the class

How do remove the constantness of abc using const_cast. I mean, I want
the syntax for that. Something like const_cast<XYZ<10>> abc gives
compiler error.

const_cast can only do with pointer, reference and pointer to
member(function).

try this

XYZ<10>* p = const_cast<XYZ<10>*>(&abc);
 
P

peter koch

I have a template class like below.
template <TInt S> class XYZ
{

}

const XYZ<10> abc;  //Instance of the class

How do remove the constantness of abc using const_cast. I mean, I want
the syntax for that. Something like const_cast<XYZ<10>> abc gives
compiler error.

Others have explained how, but I believe having to cast is a design
error. Instead of casting yourself out of your errors, you should
correct the design, which in the long run is going to save you time
and trouble.

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,168
Messages
2,570,914
Members
47,455
Latest member
Delilah Code

Latest Threads

Top