943,627 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2303
  • C++ RSS
Feb 12th, 2007
0

C++ const help

Expand Post »
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:

c++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
blackjack is offline Offline
2 posts
since Jul 2005
Feb 12th, 2007
0

Re: C++ const help

Why not just use std::strings?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Feb 12th, 2007
0

Re: C++ const help

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Feb 12th, 2007
0

Re: C++ const help

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.....
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Feb 12th, 2007
0

Re: C++ const help

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
blackjack is offline Offline
2 posts
since Jul 2005
Feb 12th, 2007
0

Re: C++ const help

Click to Expand / Collapse  Quote originally posted by blackjack ...
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).
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Searching array problems
Next Thread in C++ Forum Timeline: Problem using QT4 with codeblocks





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC