[vtk-developers] (Another) bug in MS Visual Studio
Lisa Sobierajski Avila
lisa.avila at kitware.com
Wed May 29 22:52:33 EDT 2002
Actually, if you said 1 you are right - because you have your test
backwards! :-)
I think the problem is that string literals are of type char * even though
they can't be modified....
Lisa
At 05:54 PM 5/29/2002, Andy Cedilnik wrote:
>Hello!
>
>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
>http://public.kitware.com/mailman/listinfo/vtk-developers
More information about the vtk-developers
mailing list