const char* vs. char*

M.Schrijver at el.utwente.nl M.Schrijver at el.utwente.nl
Tue Jul 13 06:10:31 EDT 1999


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.
-----------------------------------------------------------------------------





More information about the vtkusers mailing list