MSVC++ 8 SP1: delete const pointer [modified]
-
After some sloppy refactoring I found something like this in my code:
class FooBar; void foo(const FooBar* const pBar) { //do something //... delete pBar; }
So far, so bad. What surprised me, was the fact that this code compiled without any error or warning. I was told, that this compiles also in GCC. This also compiles though a non-const member function is called:void foo(const FooBar* const pBar) { pBar->~FooBar(); }
Can you tell me why these snippets work (or at least compile)? thanks, Norbert -
After some sloppy refactoring I found something like this in my code:
class FooBar; void foo(const FooBar* const pBar) { //do something //... delete pBar; }
So far, so bad. What surprised me, was the fact that this code compiled without any error or warning. I was told, that this compiles also in GCC. This also compiles though a non-const member function is called:void foo(const FooBar* const pBar) { pBar->~FooBar(); }
Can you tell me why these snippets work (or at least compile)? thanks, NorbertYou're not changing
pBar
or what it points to, are you?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
You're not changing
pBar
or what it points to, are you?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
I'm surprised too about that behavior: technically the method doesn't change neither the pointer value nor the object itself, but substantially it makes a big change to both. I think it may be a good candidate to subtle bugs.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
After some sloppy refactoring I found something like this in my code:
class FooBar; void foo(const FooBar* const pBar) { //do something //... delete pBar; }
So far, so bad. What surprised me, was the fact that this code compiled without any error or warning. I was told, that this compiles also in GCC. This also compiles though a non-const member function is called:void foo(const FooBar* const pBar) { pBar->~FooBar(); }
Can you tell me why these snippets work (or at least compile)? thanks, NorbertOf possible interest: Error message in Visual C++ when you delete a pointer to a const object[^] "NOTE: Visual C++ .NET compiler does not demonstrate this issue in conformance to the changes made in the C++ ANSI Standards." I wonder what the change is... deleting through a const pointer[^]
-
Of possible interest: Error message in Visual C++ when you delete a pointer to a const object[^] "NOTE: Visual C++ .NET compiler does not demonstrate this issue in conformance to the changes made in the C++ ANSI Standards." I wonder what the change is... deleting through a const pointer[^]
Thanks for your replies and especially for the links. Norbert