473,473 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

casting template using const_cast

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

}

const XYZ<10abc; //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.
Nov 7 '08 #1
2 3167
On 11ÔÂ7ÈÕ, ÏÂÎç10ʱ01·Ö, puru <purushotta...@gmail..comwrote:
I have a template class like below.
template <TInt Sclass XYZ
{

}

const XYZ<10abc; //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);

--
Best Regards
Barry
Nov 7 '08 #2
On 7 Nov., 15:01, puru <purushotta...@gmail.comwrote:
I have a template class like below.
template <TInt Sclass XYZ
{

}

const XYZ<10abc; *//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
Nov 8 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Cgacc20 | last post by:
I have a c struct from old code that cannot be modified and I am trying to write a wrapper C++ class around it. This class is often passed as a pointer to some c functions of a library and I...
2
by: Asapi | last post by:
"Only static_cast can be used with value types; const_cast and dynamic_cast can only be used with pointers and references." Is this a correct statement? Can reinterpret_cast be used with...
15
by: Trevor Lango | last post by:
I want to be able to cast away the constness of a private member variable in a member function of a class. I have the private data member declared as follows: const double x; I have an...
7
by: Vaca Louca | last post by:
Hello, My setup: Debian sarge on dual Pentium 4. g++ 3.3.5-3. (the other system is Windows XP with MS Visual Studio .NET 2003) I have an auto_array<T> template (based on a template taken from...
44
by: Agoston Bejo | last post by:
What happens exactly when I do the following: struct A { int i; string j; A() {} }; void f(A& a) { cout << a.i << endl;
5
by: brekehan | last post by:
I've always been a little sketchy on the differences between static, dynamic, and reinterpret casting. I am looking to clean up the following block by using C++ casting instead of the C style...
14
by: Jonas.Holmsten | last post by:
Hello I'm porting some C++ stuff to C and having problem to get it through gcc. Here is a condensed version of the problem: void foo(const int * const * const ptr) {} main()
2
by: Laurent Deniau | last post by:
I am looking for the "cleanest" way to cast away the constness of a pointee in C89, something like const_cast<T*>() in C++. Actually, I am using: #define CONST_CAST(typename,value) \ (((union {...
5
by: jason.cipriani | last post by:
There have been some recent threads about casting pointers to and from void* that have me rethinking some of my usual practices. I have a couple of questions. 1. What is the purpose of C++'s...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.