basic question on const_cast

P

pauldepstein

Textbooks usually say that const_cast can "add or remove" a const
modifier. However, all the example I've seen are examples of
"remove". I've never seen a const_cast used to make something const
that was originally non_const. Is this ever done?

Thanks,

Paul Epstein
 
S

Salt_Peter

Textbooks usually say that const_cast can "add or remove" a const
modifier. However, all the example I've seen are examples of
"remove". I've never seen a const_cast used to make something const
that was originally non_const. Is this ever done?

Thanks,

Paul Epstein

yes, everywhere, except it doesn't take the form you are thinking of.
as an example:

void foo(const int i)
{
// i = 88; // error
// read/access the const var
}

int main()
{
int n(99);
foo( n );
n = 88; // ok
}

same goes for say... a copy constructor

class A
{
public:
A(const A& copy) { }
};
 
S

Sean Hunt

Textbooks usually say that const_cast can "add or remove" a const
modifier. However, all the example I've seen are examples of
"remove". I've never seen a const_cast used to make something const
that was originally non_const. Is this ever done?

Thanks,

Paul Epstein

Usually, the ability to remove const_cast is done to work around bad
design - the inverse is also true. Consider the following class:

class A
{
int i;
public:
int operator () (int i2) { return i = i2; }
int operator () (int i2) const { return i; }
};

In this case, you might have to const_cast to force the overload to
the const version (because bad design means it acts differently).
 
A

Alf P. Steinbach

* Salt_Peter:
yes, everywhere, except it doesn't take the form you are thinking of.
as an example:

void foo(const int i)
{
// i = 88; // error
// read/access the const var
}

int main()
{
int n(99);
foo( n );
n = 88; // ok
}

same goes for say... a copy constructor

class A
{
public:
A(const A& copy) { }
};

Huh?


Cheers, bewildered,

- Alf
 
A

Alf P. Steinbach

* (e-mail address removed):
Textbooks usually say that const_cast can "add or remove" a const
modifier. However, all the example I've seen are examples of
"remove". I've never seen a const_cast used to make something const
that was originally non_const. Is this ever done?

Yes, e.g. in the case where you want to call a const member function
overload, but it is probably better expressed using a reference.

I.e., instead of

const_cast<T const*>( this )->foo(); // Calling const overload.

it might be preferable to do

T const* const constSelf = this;
constSelf->foo();

With that convention, searching for "const_cast" in the code will yield
only casts that cast away constness.


Cheers, & hth.,

- Alf
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top