C++ const help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2005
Posts: 2
Reputation: blackjack is an unknown quantity at this point 
Solved Threads: 0
blackjack blackjack is offline Offline
Newbie Poster

C++ const help

 
0
  #1
Feb 12th, 2007
I'm not that used to C++ and hoping that someone can help me out with the const keyword. If I make a parameter to a function 'const' ,and then try to reference it, like:

  1. int some_func(const char* param){
  2. char* ptr = param;
  3. ...
  4. }
to do something with it, the compiler gives the "invalid conversion from const char* to char*" when I try to pass in a parameter. The question is, is there some way to reference the 'param' while keeping it constant? or how can I fix the error message? Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: C++ const help

 
0
  #2
Feb 12th, 2007
Why not just use std::strings?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ const help

 
0
  #3
Feb 12th, 2007
you can do this: char* ptr = const_cast<char*>(param;), but that defeats the purpose of using const in the first place. Casting out the const should be avoided whenever possible.
Last edited by Ancient Dragon; Feb 12th, 2007 at 2:39 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: C++ const help

 
0
  #4
Feb 12th, 2007
Either using :
  1. const char* ptr = param ;
  2. // or
  3. char* ptr = const_cast<char*> (param) ;

But it would be interesting to know what you are trying to achieve here.....
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 2
Reputation: blackjack is an unknown quantity at this point 
Solved Threads: 0
blackjack blackjack is offline Offline
Newbie Poster

Re: C++ const help

 
0
  #5
Feb 12th, 2007
Basically, I need to step through any array using pointers instead of the array indices. I changed my char* ptr to const char* ptr and it does work. Thanks.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: C++ const help

 
0
  #6
Feb 12th, 2007
Originally Posted by blackjack View Post
Basically, I need to step through any array using pointers instead of the array indices. I changed my char* ptr to const char* ptr and it does work. Thanks.
But why would having a second const char* copy do you any good? Neither pointer can you modify, so copying it would be sort of pointless (sorry, bad pun).
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC