Thread: Novice heap questions.

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    45

    Novice heap questions.

    Would there be any benefit in this pointless example to having the data of ObjectTwo be declared separately on the heap? Is not the member from ObjectOne not on the free strore? What is/are the difference(s) aside from how you access said data?


    Code:
    class ObjectOne
    {
       public:
          int data;
    };
    
    class ObjectTwo
    {
       ObjectTwo() {data = new int;}
       ~ObjectTwo() { delete data; }
    
          int * data;
    };
    
    int main()
    {
       ObjectOne * classOne = new ObjectOne();
       ObjectTwo * classTwo = new ObjectTwo();
    
       delete classOne;
       delete classTwo;
    
    return 0;
    }

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    If it is just an int, probably not.

    But once you have such pointers in your class, obey the rule of three.
    Rule of three (C++ programming) - Wikipedia, the free encyclopedia

    Btw, you have the nomenclature backwards, interchange your ClassX and ObjectX names here.
    These minor details are rather important to understand object oriented programming properly.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    45

    Rightly corrected

    Yes, you are definitely right to point those out, poor on my part - and no excuse for rushing. It is annoying that I cannot edit it immediately actually.

    Expanding on the question though, is there some convention for explicitly slotting everything to the heap? As an example, your correction of my poor naming above. Should it just be done all the time, even in simple constructs? In a 'pick one way and do it that one way all the time' ideal.
    Last edited by Incantrix; 02-24-2014 at 11:45 PM.

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Do whichever is simpler.
    (I always put builtin types and the standard containers directly)

    Only use pointers when the object is of an incomplete type. (say..forward declared classes)
    This helps reduce clutter in the header files and improve compilation times.
    A (common?) pattern for that is to put all private data behind a pointer to a large object.
    ( Opaque pointer - Wikipedia, the free encyclopedia )

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by manasij7479 View Post
    Only use pointers when the object is of an incomplete type. (say..forward declared classes)
    references work with forward declarations too, but then they must be initialized in the constructor initializer list, and only if the constructor is defined such that the definition of what the reference refers to is fully valid at that point in the code.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Started using C++ Without Fear to learn--novice questions!
    By AvocadoRivalry in forum C++ Programming
    Replies: 6
    Last Post: 10-20-2008, 03:48 PM
  2. C++ Novice
    By AaronHall in forum C++ Programming
    Replies: 2
    Last Post: 06-20-2008, 05:48 PM
  3. C++ memory heap questions.
    By lord mazdak in forum C++ Programming
    Replies: 2
    Last Post: 12-18-2005, 10:38 AM
  4. Replies: 10
    Last Post: 06-17-2005, 10:00 PM
  5. Novice needs help
    By donniebb in forum C++ Programming
    Replies: 3
    Last Post: 02-01-2003, 05:50 PM