423,850 Members | 1,559 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,850 IT Pros & Developers. It's quick & easy.

converting a char* to a const char*

P: n/a
Hi:
What is best and safest way of converting a char* to a const char *? Can I
use const_cast?
Cheers
Sean.
Aug 6 '08 #1
Share this Question
Share on Google+
35 Replies


P: n/a
"Sean Farrow" <se*********@seanfarrow.co.ukwrites:
Hi:
What is best and safest way of converting a char* to a const char *? Can I
use const_cast?
#include <iostream>
int main(){
char* c=new char[3];

const char* cc=c; // It's that simple!

c[0]='a';c[1]='b';c[2]=0;
std::cout<<cc<<std::endl;
return(0);
}
--
__Pascal Bourguignon__
Aug 6 '08 #2

P: n/a
Sean Farrow wrote:
Hi:
What is best and safest way of converting a char* to a const char *? Can I
use const_cast?
No const_cast is rather technical and best not used (unless you *really*
understand what its purpose is). In a nutshell, if you have an object
that you know is not const but it appears as const via e.g a function
argument then you can use const_cast, otherwise you can't. IOW it
removes constness in the case where you know that a variable is in
memory and isnt constant, however for constants that realy are constant
it can have nasty efects. As its quite rare to know this much about what
a variable is except in private member functions and so on its rare to
need it.

regards
Andy Little
Aug 6 '08 #3

P: n/a
Hi:
it's a function call needing a const char*, I assume from your description,
then const_cast can be used.
Sean.
"kwikius" <an**@servocomm.freeserve.co.ukwrote in message
news:48**********@mk-nntp-2.news.uk.tiscali.com...
Sean Farrow wrote:
>Hi:
What is best and safest way of converting a char* to a const char *? Can
I use const_cast?

No const_cast is rather technical and best not used (unless you *really*
understand what its purpose is). In a nutshell, if you have an object that
you know is not const but it appears as const via e.g a function argument
then you can use const_cast, otherwise you can't. IOW it removes constness
in the case where you know that a variable is in memory and isnt constant,
however for constants that realy are constant it can have nasty efects. As
its quite rare to know this much about what a variable is except in
private member functions and so on its rare to need it.

regards
Andy Little


Aug 6 '08 #4

P: n/a
Sean Farrow wrote:
What is best and safest way of converting a char* to a const char *? Can I
use const_cast?
You have got it backwards: const_cast is used to *remove* constness,
not to add it.

You don't need to do anything special to add constness to a pointer.
Anything that expects a const pointer can be given a non-const one.
Aug 6 '08 #5

P: n/a
Sean Farrow wrote:
it's a function call needing a const char*, I assume from your description,
then const_cast can be used.
void f( const char * );

....

void g( char * p )
{
f( p );
}
I fail to see where the problem is that you're trying to solve with
const_cast. Could you give a code example?
Aug 6 '08 #6

P: n/a
Hi:
I've solved the issue, it was const_cast I needed, that's going to teach me
to rad error info properly.
Thanks to everybody.
Sean.
"Juha Nieminen" <no****@thanks.invalidwrote in message
news:2q*************@read4.inet.fi...
Sean Farrow wrote:
>What is best and safest way of converting a char* to a const char *? Can
I
use const_cast?

You have got it backwards: const_cast is used to *remove* constness,
not to add it.

You don't need to do anything special to add constness to a pointer.
Anything that expects a const pointer can be given a non-const one.

Aug 6 '08 #7

P: n/a
On 2008-08-06 10:19:42 -0400, Juha Nieminen <no****@thanks.invalidsaid:
Sean Farrow wrote:
>What is best and safest way of converting a char* to a const char *? Can I
use const_cast?

You have got it backwards: const_cast is used to *remove* constness,
not to add it.

You don't need to do anything special to add constness to a pointer.
Anything that expects a const pointer can be given a non-const one.
Not to a raw pointer, but const_cast can be used to add const in cases
where there is no implicit conversion, such as a type with multiple
levels of indirection. Granted, this is obscure (which is why i only
mention it and don't give an example). const_cast can be used to add or
remove const; in the simple cases of adding const it's not needed, so
its primary use is, as you say, to remove const.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 6 '08 #8

P: n/a
On Aug 6, 4:19 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Sean Farrow wrote:
What is best and safest way of converting a char* to a const
char *? Can I use const_cast?
You have got it backwards: const_cast is used to *remove*
constness, not to add it.
Usually. It can be used either way, and there are times when
you want to explicitly add the const, to control overload
resolution, or template instantiation, for example. Most of the
time. of course, you don't bother, since the compiler will
happily add all the consts you want, if needed.
You don't need to do anything special to add constness to a
pointer. Anything that expects a const pointer can be given a
non-const one.
For first-level const. You can't pass a char** to a function
expecting a char const**.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 6 '08 #9

P: n/a
On Aug 6, 3:10*pm, "Sean Farrow" <sean.far...@seanfarrow.co.ukwrote:
Hi:
it's a function call needing a const char*, I assume from your description,
then const_cast can be used.
Nope! Dont bother with it. make a copy of the string or use
std::string.

regards
Andy Little

Aug 6 '08 #10

P: n/a
On 2008-08-06 13:47:32 -0400, kwikius <an**@servocomm.freeserve.co.uksaid:
On Aug 6, 3:10Â*pm, "Sean Farrow" <sean.far...@seanfarrow.co.ukwrote:
>Hi:
it's a function call needing a const char*, I assume from your descriptio
n,
>then const_cast can be used.

Nope! Dont bother with it. make a copy of the string or use
std::string.
Wait, I'm confused. If the situation is this:

void f(const char *);

void g(char *p)
{
// how to call f with the character array that p points to?
}

Then the answer is far simpler: just do it. f(p) relies on the implicit
conversion of char* to const char*. No need for const_case, copying, or
std::string.

Of course, if the situation is something different, then the answer is
almost certainly different. <g>

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 6 '08 #11

P: n/a
On Aug 6, 7:03*pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-08-06 13:47:32 -0400, kwikius <a...@servocomm.freeserve.co.uksaid:
On Aug 6, 3:10*pm, "Sean Farrow" <sean.far...@seanfarrow.co.ukwrote:
Hi:
it's a function call needing a const char*, I assume from your descriptio
n,
then const_cast can be used.
Nope! Dont bother with it. make a copy of the string or use
std::string.

Wait, I'm confused. If the situation is this:

void f(const char *);

void g(char *p)
{
* * * * // how to call f with the character array that p points to?

}

Then the answer is far simpler: just do it. f(p) relies on the implicit
conversion of char* to const char*. No need for const_case, copying, or
std::string.
hmm.. Apologies to the O.P. I wasnt aware that const_cast can be used
to cast to const. And rereading the post it seems that what he wanted.

The idea that its ok to cast away const in general use with const_cast
is bad. From time to time one gets an error message related to trying
to use a value that is passed as a constant ref sa non const. In this
situation one temptation is to use const_cast. However the real answer
is to try to find out what the problem is with the design or your
understanding of the function.

In writing a function Since the user of the function doesnt expect
the value to be changed in the function, there is no reason to use
const_cast. If you are going to write to a reference then make that
clear.

Problem is that const_cast sometimes looks like a nice easy solution
to problems, which it isnt.

regards
Andy Little
Aug 6 '08 #12

P: n/a
On 2008-08-06 14:19:43 -0400, kwikius <an**@servocomm.freeserve.co.uksaid:
>
Problem is that const_cast sometimes looks like a nice easy solution
to problems, which it isnt.
Agreed. It's an advanced feature, and beginners shouldn't use it.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 6 '08 #13

P: n/a
>const_cast can be used to add orremove const;

How can const_cast add constantness? I was under impression that it
only remove constantness if object is "not really" constant.
Aug 6 '08 #14

P: n/a
>
For first-level const. *You can't pass a char** to a function
expecting a char const**.
First is the first level const?

Aug 6 '08 #15

P: n/a
puzzlecracker wrote:
>const_cast can be used to add orremove const;

How can const_cast add constantness? I was under impression that it
only remove constantness if object is "not really" constant.
int n;
int* p = &n;
const int* cp = const_cast<const int*>(p);

The const_cast is superfluous here, but legal. I'm sure someone can
contrive a template example where it would be required!

--
Ian Collins.
Aug 6 '08 #16

P: n/a
In article <38**********************************@k30g2000hse. googlegroups.com>,
puzzlecracker <ir*********@gmail.comwrote:
const_cast can be used to add orremove const;
How can const_cast add constantness? I was under impression that it
only remove constantness if object is "not really" constant.
Probbaly where I've seen it pop up most is in changing the
type of the "this" pointer to force a const overloaded function
to be called instead of the non-const one.

Usual caveats apply: consider using it this way wisely,
consider any other alternatives, blah blah.

Also, I forget if using it this way is undefined behavior.
--
Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 6 '08 #17

P: n/a
In article <ed**********************************@2g2000hsn.go oglegroups.com>,
puzzlecracker <ir*********@gmail.comwrote:
>For first-level const. =A0You can't pass a char** to a function
expecting a char const**.

First is the first level const?
Dunno if this helps: http://www.comeaucomputing.com/techtalk/#deconstutoh
--
Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 6 '08 #18

P: n/a
Sean Farrow wrote:
I've solved the issue, it was const_cast I needed, that's going to teach me
to rad error info properly.
Now that you have resolved the problem, could you also explain to us
the situation where you needed to use const_cast to *add* constness
(instead of removing it) so that also we can learn from it?

(Somehow I have the feeling that the const_cast is *not* the real
solution to your problem. It just happens to work, and masks the real
problem.)
Aug 6 '08 #19

P: n/a
James Kanze wrote:
>You have got it backwards: const_cast is used to *remove*
constness, not to add it.

Usually. It can be used either way, and there are times when
you want to explicitly add the const, to control overload
resolution, or template instantiation, for example.
Isn't that what static_cast is for? const_cast sounds like the wrong
tool for the job (because common C++ wisdom dictates that static_cast is
good and const_cast should usually be avoided if possible).

You may as well say "if you want to add constness eg. for overload
resolution, use reinterpret_cast". That's just wrong.
Aug 6 '08 #20

P: n/a
On 2008-08-06 19:28:36 -0400, Juha Nieminen <no****@thanks.invalidsaid:
James Kanze wrote:
>>You have got it backwards: const_cast is used to *remove*
constness, not to add it.

Usually. It can be used either way, and there are times when
you want to explicitly add the const, to control overload
resolution, or template instantiation, for example.

Isn't that what static_cast is for? const_cast sounds like the wrong
tool for the job (because common C++ wisdom dictates that static_cast is
good and const_cast should usually be avoided if possible).
This overlooks another overbroad principle: const_cast should be used
to adjust constness; using other casts for that purpose should be
avoided.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 7 '08 #21

P: n/a
On Aug 7, 1:28 am, Juha Nieminen <nos...@thanks.invalidwrote:
James Kanze wrote:
You have got it backwards: const_cast is used to *remove*
constness, not to add it.
Usually. It can be used either way, and there are times when
you want to explicitly add the const, to control overload
resolution, or template instantiation, for example.
Isn't that what static_cast is for? const_cast sounds like the
wrong tool for the job (because common C++ wisdom dictates
that static_cast is good and const_cast should usually be
avoided if possible).
That's a good point. As Pete said, const_cast is for adjusting
const, and that's what you're doing. But IMHO, it's arguable
both ways:

-- If you use static_cast, you can't accidentally add const
(but is this really a problem?). Also, it won't show up if
someone greps for const_cast (which they're doubtlessly
doing to find "dangerous" code, and adding const isn't
dangerous).

-- If you use const_cast, you can't accidentally perform a
Derived* to Base* (or worse, a Base* to Derived*) conversion
which you didn't want.

IMHO, that second point predominates, and is, ultimately, the
reason why the new style casts were introduced: const_cast
allows adjusting const-ness, with no risk of accidentally
performing any other conversion, static_cast allows moving up
and down in the hierarchy, without any risk of accidentally
removing const-ness or type punning, and reinterpret_cast forces
type punning (even if the types involved are pointers within an
inheritance hierarchy), without any risk of removing const-ness.
You may as well say "if you want to add constness eg. for
overload resolution, use reinterpret_cast". That's just wrong.
Agreed. Using static_cast or reinterpret_cast to adjust
const-ness, even when they allow it (i.e. adding const) is just
wrong.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 7 '08 #22

P: n/a
Pete Becker wrote:
On 2008-08-06 14:19:43 -0400, kwikius <an**@servocomm.freeserve.co.uk>
said:
>>
Problem is that const_cast sometimes looks like a nice easy solution
to problems, which it isnt.

Agreed. It's an advanced feature, and beginners shouldn't use it.
Do you have a link to good article explaining const_cast, cause I never
read anything similar.

I am asking, because in my application is like this:

int main( const int argc, const char* argv[] )
{
....

int arg1 = argc;
char** arg2 = const_cast< char** ( &argv[0] );
glutInit( &arg1, arg2 );
....
}

glutInit function is described here:
http://www.opengl.org/documentation/...c3/node10.html

Am I doing something not allowed? If so, how to fix?
Aug 7 '08 #23

P: n/a
On 2008-08-07 04:14:03 -0400, James Kanze <ja*********@gmail.comsaid:
>
-- If you use static_cast, you can't accidentally add const
But you can. <gThe prohibition is that "The static_cast operator
shall not cast away constness...". [expr.static.cast]/1.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 7 '08 #24

P: n/a
On 2008-08-07 04:43:44 -0400, anon <an**@no.nosaid:
Pete Becker wrote:
>On 2008-08-06 14:19:43 -0400, kwikius <an**@servocomm.freeserve.co.uksaid:
>>>
Problem is that const_cast sometimes looks like a nice easy solution
to problems, which it isnt.

Agreed. It's an advanced feature, and beginners shouldn't use it.

Do you have a link to good article explaining const_cast, cause I never
read anything similar.

I am asking, because in my application is like this:

int main( const int argc, const char* argv[] )
Why? "char *argv[]" is the usual form.
{
...

int arg1 = argc;
char** arg2 = const_cast< char** ( &argv[0] );
glutInit( &arg1, arg2 );
...
}

glutInit function is described here:
http://www.opengl.org/documentation/...c3/node10.html

Am I doing something not allowed? If so, how to fix?
No, it's allowed. But it's contradictory: the declaration of main says
that the chars in the argument array are not modifiable, then later on
the cast says that they are. Make up your mind.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 7 '08 #25

P: n/a
James Kanze wrote:
>You may as well say "if you want to add constness eg. for
overload resolution, use reinterpret_cast". That's just wrong.

Agreed. Using static_cast or reinterpret_cast to adjust
const-ness, even when they allow it (i.e. adding const) is just
wrong.
Intentionally misinterpreting someone's text, even if it's clearly a
pun, is annoying.
Aug 7 '08 #26

P: n/a
In article <g7**********@news01.versatel.de>, anon <an**@no.nowrote:
>Pete Becker wrote:
>On 2008-08-06 14:19:43 -0400, kwikius <an**@servocomm.freeserve.co.uk>
said:
>>Problem is that const_cast sometimes looks like a nice easy solution
to problems, which it isnt.
Agreed. It's an advanced feature, and beginners shouldn't use it.
Do you have a link to good article explaining const_cast, cause I never
read anything similar.

I am asking, because in my application is like this:

int main( const int argc, const char* argv[] )
{
...

int arg1 = argc;
char** arg2 = const_cast< char** ( &argv[0] );
glutInit( &arg1, arg2 );
...
}
This is well somewhat confusing, though useful :) as main normally
doesn't take arguments declared that way. In specific, argc and argv
and *argv and **argv etc are all modifiable. So if the const_cast
was added because of that mistake, then the const_cast is a mistake.
Which, once again points to the dangers of casting, adding cast to
get around errors, etc. Now, assuming this was some other function
and not main and assuming the arguments are of those type, then yes,
a const_cast<like that might make sense.

BTW, there is also a side issue about declaring argc const.
The usual argument is that a top level const parameter does not lend
to the interface of function, therefore, it should not be there.
I'm not saying one is right and one is wrong (though others here
might :} ), just putting the issue on the table as you will no doubt
eventually run across it.
--
Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 7 '08 #27

P: n/a
On Aug 7, 10:43 am, anon <a...@no.nowrote:
Pete Becker wrote:
On 2008-08-06 14:19:43 -0400, kwikius <a...@servocomm.freeserve.co.uk>
said:
Problem is that const_cast sometimes looks like a nice easy
solution to problems, which it isnt.
Agreed. It's an advanced feature, and beginners shouldn't use it.
Do you have a link to good article explaining const_cast,
cause I never read anything similar.
I am asking, because in my application is like this:
int main( const int argc, const char* argv[] )
{
...
int arg1 = argc;
char** arg2 = const_cast< char** ( &argv[0] );
glutInit( &arg1, arg2 );
...
}
glutInit function is described here:http://www.opengl.org/documentation/...c3/node10.html
Am I doing something not allowed? If so, how to fix?
Yes. That's not a standard definition of main. The two
standard definitions are:
int main()
and
int main( int argc, char** argv )
Anything else is an implementation extension, if it works at
all.

I wonder: why don't compilers complain about versions of main
they don't support? I just compiled a
int main( double d ){}
, and none of the compilers I use even output a warning.
Although I'm pretty sure that if I tried to use the d, I'd just
get garbage. The behavior in such cases is implementation
defined; there's no reason for an implementation to not generate
an error if it doesn't support the definition. (IMHO, this
would be far more useful than forbidding "void main()", despite
the fact that it more or less works.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 8 '08 #28

P: n/a
On Aug 7, 5:42 pm, com...@panix.com (Greg Comeau) wrote:
This is well somewhat confusing, though useful :) as main
normally doesn't take arguments declared that way. In
specific, argc and argv and *argv and **argv etc are all
modifiable.
Is this an intentional change from C, or something that just
slipped through. (In C, the type of argv is char**, but the
application isn't allowed to modify the pointers in the array
pointed to by argv---although it is allowed to modify argv
itself, and the strings the pointers point to: "argv=p" is
legal, as is "argv[1][0]='\0' (provided that argc >= 2), but not
"argv[1]=p".)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 8 '08 #29

P: n/a
On Aug 7, 12:19 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-08-07 04:14:03 -0400, James Kanze <james.ka...@gmail.comsaid:
-- If you use static_cast, you can't accidentally add const
But you can. <gThe prohibition is that "The static_cast operator
shall not cast away constness...". [expr.static.cast]/1.
That's a typo. I meant to say that you can't accidentally
remove const.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 8 '08 #30

P: n/a
On 2008-08-08 04:33:40 -0400, James Kanze <ja*********@gmail.comsaid:
On Aug 7, 12:19 pm, Pete Becker <p...@versatilecoding.comwrote:
>On 2008-08-07 04:14:03 -0400, James Kanze <james.ka...@gmail.comsaid:
>>-- If you use static_cast, you can't accidentally add const
>But you can. <gThe prohibition is that "The static_cast operator
shall not cast away constness...". [expr.static.cast]/1.

That's a typo. I meant to say that you can't accidentally
remove const.
Glad to hear it. <g>

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 8 '08 #31

P: n/a
James Kanze wrote:
static_cast allows moving up
and down in the hierarchy, without any risk of accidentally
removing const-ness or type punning
But isn't dynamic_cast the preferred way to do that?
--
Fran
Aug 10 '08 #32

P: n/a
On Aug 10, 4:04 am, Francis Litterio <em...@not.availablewrote:
James Kanze wrote:
static_cast allows moving up
and down in the hierarchy, without any risk of accidentally
removing const-ness or type punning
But isn't dynamic_cast the preferred way to do that?
Generally. If you're clearly moving to the base class, it
doesn't matter. Otherwise, however, yes, dynamic_cast is
prefered.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 10 '08 #33

P: n/a
James Kanze wrote:
Generally. If you're clearly moving to the base class, it
doesn't matter. Otherwise, however, yes, dynamic_cast is
prefered.
Btw, is a dynamic_cast internally optimized into a regular static_cast
by the compiler if it determines that the casting is from a derived type
to a base type and thus runtime casting is not required?
Aug 10 '08 #34

P: n/a
On Aug 10, 12:56 pm, Juha Nieminen <nos...@thanks.invalidwrote:
James Kanze wrote:
Generally. If you're clearly moving to the base class, it
doesn't matter. Otherwise, however, yes, dynamic_cast is
prefered.
Btw, is a dynamic_cast internally optimized into a regular
static_cast by the compiler if it determines that the casting
is from a derived type to a base type and thus runtime casting
is not required?
The standard says absolutely nothing about implementation, but
it would be a very poor implementation that did any runtime
lookup when the compiler had all of the information already.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 10 '08 #35

P: n/a
Juha Nieminen wrote:
Sean Farrow wrote:
>What is best and safest way of converting a char* to a const char *? Can I
use const_cast?

You have got it backwards: const_cast is used to *remove* constness,
not to add it.
It can do either. The more common use is of course removing constness.
However, if you have a const overload of a function and you want to
share common code you might very well cast TO const in the non-const
function.

A better way might be to call a common function, but either works. I'd
consider calling a common function even if it contains the entire const
version of the function because I really, really, really object any time
I see a const_cast in code. It is such a dangerous cast that I simply
will NOT use it if I can avoid it in any way. Obviously I won't do more
dangerous things to avoid it but it's hard to think of one.
Aug 11 '08 #36

This discussion thread is closed

Replies have been disabled for this discussion.