[vtk-developers] (Another) bug in MS Visual Studio

Miller, James V (Research) millerjv at crd.ge.com
Thu May 30 09:03:48 EDT 2002

I guess a side question is:

Why would you have two functions with the same name where one takes 
a const and the other does not AND the two functions perform DIFFERENT

While this is probably a bug for at least some of the compilers (not willing
to say which compilers are doing the correct thing), I would say it is
also a bad design. A user should expect functions with the same name to 
perform the same operation. When I need to provide const and non const
versions of the same function, I usually have one call the other; but at 
a minimum, I have them do the same thing.

-----Original Message-----
From: Andy Cedilnik [mailto:andy.cedilnik at kitware.com]
Sent: Wednesday, May 29, 2002 5:54 PM
To: vtk-developers
Subject: [vtk-developers] (Another) bug in MS Visual Studio


Just a funny story... It is one of those usual stories where the guy
works on computer and the thing does not work and after two hours of
cursing and complaining, he founds (another) bug in MS Visual Studio...

So, here we go. Test this on your system:
#include <iostream.h>
int TestConst(char* s){ return 1; }
int TestConst(const char* s) { return 0; }
int main(int, char** argv)
  int res = TestConst("andy");
  cout << "Result: " << res << endl;
  return res;

Now, before you compile it and test it, please try to guess what the
output should be. 

If you said 1, you are wrong. The string "andy" cannot be changed, so in
C++ term it is "const". Therefore TestConst gets as an argument a "const
character pointer", so it should call int TestConst(const char* s). But
hey, this works on inferior compilers that are so buggy that people
should not be using them, such as gcc. So, I (and hopefully the reader)
would assume that this will work on superior products which cost lots of
money such as MS Visual Studio. Well guess again. 

Now, you (reader) would think: "...well, you tried with MS Visual Studio
6.0 which is so old that your grandma is not using it any more and that
if you try this on MS Visual Studio .NET, it will work..."

Or maybe not...

So, what if our TestConst(char* s) does not just return 0 but also let
say modifies string s? This is a perfectly valid thing, you know
(example: strcpy). Well, I guess you are in trouble. 

So, when you next time see the commercial where they are explaining one
degree of separation, remember this.

				Andy Cedilnik

vtk-developers mailing list
vtk-developers at public.kitware.com

More information about the vtk-developers mailing list