Thread: Objects and member variables on the heap

  1. #1
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209

    Objects and member variables on the heap

    Let's say I have a simple class, example. None of the member variables are pointers, just plain ints. If I instantiate an object of that class on the heap,
    Code:
    example *pExample = new example;
    will this allocate space for all of the member variables on the heap also?

  2. #2
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    Yes. A class can be considered a new datatype.
    The size is determined by how much space is needed to hold the contents.

    Unlesss I am misunderstanding your question.

  3. #3
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    I think you did. I was wondering how the variables are stored on certain circumstances.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    I believe that an object (an instance of a class) is handled as a single entity. If the object is instantiated in the heap all the member variables are allocated there. If it is instantiated in the stack everything is there. In fact if I'm not mistaken the only thing that gets allocated when you instantiate an object are non static members variables of the object. Member functions and static member variables are allocated only once when the program loads.

    The pointer on to the class on the other hand is allocated in the stack since it is a local variable.

  5. #5
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Alright, I understand now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Complicated member objects that depend on `this`
    By drrngrvy in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2006, 09:48 PM
  3. Question about cout an stack object?
    By joenching in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2005, 10:10 PM
  4. Accessing/working with member objects of parent classes
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 03-31-2005, 11:01 PM
  5. Modifying CDocument Member Objects :: MFC
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 04-12-2002, 05:52 PM