473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

converting a char* to a const char*

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
35 34070
"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
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
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
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
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
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
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
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
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
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
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
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
>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
>
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: jagmeena | last post by:
Hello, I am sure this problem has been addressed before, however, I could'nt get a suitable solution to my problem. Hence I am posting here. Thanks a lot for all your help. The code I have is ...
20
by: Nate | last post by:
I am working on an Oakley parser and want to call an fopen function to read in the log file. I can use this to read the file, but only if I pass a "const char" variable to fopen. Since I would...
7
by: jamihuq | last post by:
Hello, I would like to convert the following inline function to a macro. Can someone help? Thx Jami inline char * fromDESC(const char * &aDesC)
3
by: fakeprogress | last post by:
How would I go about converting this C code to C++? /* LIBRARY is an array of structures */ /* This function compares 'tcode' with */ /* existing codes in the array. */ /* It...
9
by: Gregory.A.Book | last post by:
I am interested in converting sets of 4 bytes to floats in C++. I have a library that reads image data and returns the data as an array of unsigned chars. The image data is stored as 4-byte floats....
2
by: pookiebearbottom | last post by:
Just looking for opinion on which of the 3 methods below people use in their code when they convert a 'const char *' to a 'const std::string &' came across #3 in someone's code and I had to...
11
by: hamishd | last post by:
Is this possible? Sorry if this question isn't relevant here. actually, I'm really trying to convert a unsigned char * to an int
5
by: Hans Mull | last post by:
Hi! How can I convert a string to a const unsigned char*? (string::c_str() converts the string to a signed char) Thanks in advance, Hans
7
by: ma740988 | last post by:
Consider the equation (flight dynamics stuff): Yaw (Degrees) = Azimuth Angle(Radians) * 180 (Degrees) / 3.1415926535897932384626433832795 (Radians) There's a valid reason to use single...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.