const char* vs. char*

Mark Beall mbeall at simmetrix.com
Tue Jul 13 09:38:14 EDT 1999


Jim,
While I agree that this can be a pain to do, experience has shown
me that it's really a worthwile effort since it often shows up
design/implementation problems that otherwise are hidden (if
you have an argument that really should be const and are using
it as though it isn't there really is an issue that should
be dealt with). Good compilers can also take advantage of this 
to be able to make certain optimizations.

mark

>Unfortunately it is in general very difficult to go back through a 
>software system and change
>parameter lists to take "const" pointers or reference.  This is not simply 
>a matter of changing the
>parameter lists.  You have to dig around the code for all uses of the 
>functions.  Usually what
>happens is that you end up passing a const into one function which calls 
>another function which calls
>another function, etc, etc, and then near the bottom of the call chain you 
>have to call a function
>which does not allow a const object to be passed in.  Then you end up 
>riddling the code with extra
>copies of functions or copy the data to a non-const, etc., etc.
>
>To retrofit a system to take const parameters is a large bucket of worms. 
>It is much easier to do
>this from the beginning of a software system.
>
>Now, your particular case at hand with the SetStringMacro may be easy 
>enough. We'll take a look at it
>and see what the ramifications may be.
>
>
>-----Original Message-----
>From: M.Schrijver at el.utwente.nl [mailto:M.Schrijver at el.utwente.nl]
>Sent: Tuesday, July 13, 1999 6:11 AM
>To: vtkusers at gsao.med.ge.com
>Subject: const char* vs. char* 
>
>
>Hello all,
>
>I find it very inconvenient that some functions use char* instead of
>const char* (e.g. the vtkSetStringMacro in vtkSetGet.h, and
>vtkImageReader::SetFileName() etc.). This means that the following code
>does not compile:
>
>......
>......
>const char* filename="../filename.bmp";
>reader->SetFileName(filename);
>
>I know, I know, I can drop the const here. But that leaves the door wide
>open to anyone trying to modify the string (e.g. replacing the '../'
>part with the full path, which will definitely blow your system), and
>there are other situations in which you simply can't. What, for example,
>if you do your string manipulations with the std::string class, and want
>to pass the result to vtk. The function std::string::c_str() returns a
>const char*, not char*. That would mean you'd have to copy the string to
>a temporary buffer in order to pass it to vtk!
>
>In addition, I don't see why anyone would /not/ make the argument to
>such functions const. I've tried it with vtk, and it compiles and runs
>fine. So, I don't see a problem .... Anyone who does?
>
>Many thanks,
>
>Marc Schrijver
>
>
>ir. Marc Schrijver
>Faculty of Electrical Engineering (S&S NT)
>University of Twente
>The Netherlands
>E-mail: m.schrijver at el.utwente.nl
>
>
>
>-----------------------------------------------------------------------------
>This is the private VTK discussion list.  Please keep messages on-topic.
>Check the FAQ at: <http://www.automatrix.com/cgi-bin/vtkfaq>
>To UNSUBSCRIBE, send message body containing "unsubscribe vtkusers" to
><majordomo at gsao.med.ge.com>.  For help, send message body containing
>"info vtkusers" to the same address.     Live long and prosper.
>-----------------------------------------------------------------------------
>
>
>-----------------------------------------------------------------------------
>This is the private VTK discussion list.  Please keep messages on-topic.
>Check the FAQ at: <http://www.automatrix.com/cgi-bin/vtkfaq>
>To UNSUBSCRIBE, send message body containing "unsubscribe vtkusers" to
><majordomo at gsao.med.ge.com>.  For help, send message body containing
>"info vtkusers" to the same address.     Live long and prosper.
>-----------------------------------------------------------------------------
>


-----------------------------------------------------------------------------
This is the private VTK discussion list.  Please keep messages on-topic.
Check the FAQ at: <http://www.automatrix.com/cgi-bin/vtkfaq>
To UNSUBSCRIBE, send message body containing "unsubscribe vtkusers" to
<majordomo at gsao.med.ge.com>.  For help, send message body containing
"info vtkusers" to the same address.     Live long and prosper.
-----------------------------------------------------------------------------





More information about the vtkusers mailing list