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. Static member variable pointer.

Static member variable pointer.

Scheduled Pinned Locked Moved C / C++ / MFC
csharpquestion
3 Posts 3 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.
  • J Offline
    J Offline
    Jawache
    wrote on last edited by
    #1

    Just a quick question, when I have a static member pointer var.. class Poo { static Poo * _poo } -- implemented Poo * Poo::_poo = new Poo(); Poo::Poo()...... Do I look after the destruction of the pointer or does that happen automatically Cheers Asim Hussain e: asim@jawache.net w: www.jawache.net

    N A 2 Replies Last reply
    0
    • J Jawache

      Just a quick question, when I have a static member pointer var.. class Poo { static Poo * _poo } -- implemented Poo * Poo::_poo = new Poo(); Poo::Poo()...... Do I look after the destruction of the pointer or does that happen automatically Cheers Asim Hussain e: asim@jawache.net w: www.jawache.net

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #2

      Jawache wrote: Do I look after the destruction of the pointer or does that happen automatically Typically anytime you use the new operator to allocate memory on the heap you need to handle the deletion of it when you are through. Nick Parker
      May your glass be ever full. May the roof over your head be always strong. And may you be in heaven half an hour before the devil knows you’re dead. - Irish Blessing


      1 Reply Last reply
      0
      • J Jawache

        Just a quick question, when I have a static member pointer var.. class Poo { static Poo * _poo } -- implemented Poo * Poo::_poo = new Poo(); Poo::Poo()...... Do I look after the destruction of the pointer or does that happen automatically Cheers Asim Hussain e: asim@jawache.net w: www.jawache.net

        A Offline
        A Offline
        Alvaro Mendez
        wrote on last edited by
        #3

        Here are some points to consider: 1. If the value of _poo will never change, in other words, it will never be reassigned to a different object, then:

        • It doesn't really need to be deleted -- the program will clean it up when it exits. However, if you run the program in the DevStudio debugger, you'll get a message about a memory block that wasn't freed, which for this case is harmless, but it's better to not have it so that it's not confused with real leaks.

        • You don't really need to make _poo a pointer to Poo. You can make it a Poo object directly:

          class Poo
          {
          static Poo _poo;
          };

          Poo Poo::_poo;

        2. If the value of _poo (as you have it) will change, then you will obviously need to free the memory that it points to before reassigning it. Otherwise you'll have memory leaks. Regards, Alvaro


        Well done is better than well said. -- Benjamin Franklin (I actually prefer medium-well.)

        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