Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. MSVC++ 8 SP1: delete const pointer [modified]

MSVC++ 8 SP1: delete const pointer [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    N O R B E R T
    wrote on last edited by
    #1

    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

    D M 2 Replies Last reply
    0
    • N N O R B E R T

      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

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      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

      CPalliniC 1 Reply Last reply
      0
      • D David Crow

        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

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        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.

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • N N O R B E R T

          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

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          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[^]

          N 1 Reply Last reply
          0
          • M Mark Salsbery

            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[^]

            N Offline
            N Offline
            N O R B E R T
            wrote on last edited by
            #5

            Thanks for your replies and especially for the links. Norbert

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups