Thread: Array of pointers for dynamically allocating class instances

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    17

    Array of pointers for dynamically allocating class instances

    I'm using an array of pointers to dynamically allocate class instances of a class and it's derived classes. I'm doing great so far, except when I have to copy an object. I'm getting bugs in my program and I think I know why. I just don't know how to solve the problem.

    I declare the array of pointers like this:

    class CObject {
    ...
    ...
    }

    CObject *templates[MAXOBJS];
    CObject *pointers[MAXOBJS];

    Here's where the problem lies. I want to copy a *templates object to a *pointers object. If I use:

    pointers[50] = templates[50];

    This just copies the pointer, and not the data at the pointer's address, correct? So if I were to:

    pointers[50].String = "Hello";

    Now I have just modified templates[50].String as well? How do I actually copy the data vs. the address?

    (Yes I'm aware both source and destination pointers must point to an object created with the new keyword)
    Last edited by sh0x; 09-10-2001 at 10:30 PM.
    -sh0x

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  2. static array of function pointers within class
    By Yarbles in forum C++ Programming
    Replies: 6
    Last Post: 11-02-2005, 02:10 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM