473,803 Members | 3,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
35 34601
On 2008-08-08 04:33:40 -0400, James Kanze <ja*********@gm ail.comsaid:
On Aug 7, 12:19 pm, Pete Becker <p...@versatile coding.comwrote :
>On 2008-08-07 04:14:03 -0400, James Kanze <james.ka...@gm ail.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.cas t]/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.avai lablewrote:
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 objektorientier ter Datenverarbeitu ng
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 objektorientier ter Datenverarbeitu ng
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
5434
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 typedef struct Rec { unsigned char msg; unsigned long len;
20
2192
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 like the end user to specify the name of the log file, I have a scanf in there to allow the user to do so, but it isn't a const at that point. I have seen reference to calls like static_cast, dynamic_cast and reinterpret_cast in C that will allow...
7
1976
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
2187
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 returns the index of the code in */ /* the LIBRARY structure if it is found. */ int findcode( LIBRARY *b, int n, char *tcode ) { int i;
9
30527
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. How can I convert the sets of 4 bytes to floats? Thanks, Greg Book
2
10939
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 think for a sec. At first I read it as converting a 'const char *' to a 'std::string *' void f(const std::string &s) { std::cout << s.size() << "\n";
11
12787
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
12658
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
4789
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 precision floating point types. The number of decimal digits guaranteed to be correct on my implementation is 6. (i.e numeric_limits < float >::digits10 = 6 ) If I'm reading the IEEE standard, I'd could paraphrase the issue
0
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9565
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10550
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2972
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.