Thread: creating objects

  1. #1
    sockets mad
    Join Date
    Mar 2002
    Posts
    126

    creating objects

    Hi,

    I've been reading a book on C++ for a while. I've been reading about how to create objects on the heap. If I'm right the difference when declaring would be something like this:

    CObject *Object = new CObject;

    which creates a pointer to the object on the heap where-as:

    CObject Object;

    just creates it like usual.

    It explains the concept but doesn't explain why you should want to use one or the other or anything, it just explains the difference.

    I apologise for any technical innaccuracies, I'm new to a lot of this and I don't have the book to hand.

    Many thanks,

    Daniel

  2. #2
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Sometimes, you are not sure how many objects you need at compile time, so you create them dynamically on the heap at run time. This is especially true when you need an unknown array of objects.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    ... or if you need a large array of items, it's better to use the heap, because the stack has a lower space limit.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    161
    ... or if you need an object to exist beyond the scope of a function.

  5. #5
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Object pointers should be used for the same tasks as normal pointers.

  6. #6
    sockets mad
    Join Date
    Mar 2002
    Posts
    126
    Thanks for the advice people, filled in the gaps.

    Sorry for the double post Hammer, pressed refresh on the wrong page =)

    -Dan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Creating a map of objects with variable width and height
    By MrSparky in forum C++ Programming
    Replies: 6
    Last Post: 07-30-2007, 03:06 PM
  3. Creating vectors of objects!
    By bobthebullet990 in forum C++ Programming
    Replies: 17
    Last Post: 08-08-2006, 04:51 PM
  4. A question about dynamically creating an array of objects
    By edd1986 in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2006, 12:30 PM
  5. creating objects in c++
    By sachitha in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2004, 12:19 PM