| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"" |
| Date: |
31 Jan 2008 08:34:57 PM |
| Object: |
basic question on const_cast |
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
.
|
|
| User: "Alf P. Steinbach" |
|
| Title: Re: basic question on const_cast |
31 Jan 2008 09:27:04 PM |
|
|
* pauldepstein@att.net:
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
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.
|
|
|
|
| User: "Sean Hunt" |
|
| Title: Re: basic question on const_cast |
31 Jan 2008 09:19:45 PM |
|
|
On Jan 31, 7:34 pm, wrote:
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).
.
|
|
|
|
| User: "Salt_Peter" |
|
| Title: Re: basic question on const_cast |
31 Jan 2008 09:15:13 PM |
|
|
On Jan 31, 9:34 pm, wrote:
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) { }
};
.
|
|
|
| User: "Alf P. Steinbach" |
|
| Title: Re: basic question on const_cast |
31 Jan 2008 09:23:13 PM |
|
|
* Salt_Peter:
On Jan 31, 9:34 pm, wrote:
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) { }
};
Huh?
Cheers, bewildered,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.
|
|
|
|
|

|
Related Articles |
|
|