Dynamically allocated array of an abstract type

This is a discussion on Dynamically allocated array of an abstract type within the C++ Programming forums, part of the General Programming Boards category; How can you ynamically allocate an array of an abstract type? eg. Code: int i; Abstract* a; cout << "How ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    2

    Dynamically allocated array of an abstract type

    How can you ynamically allocate an array of an abstract type?
    eg.
    Code:
        int i; Abstract* a;
        cout << "How many ? ";
        cin >> i;
        
        a = new (nothrow) Abstract[i];
    Class Abstract is abstract(has a = 0 function) and has 2 non-asbtract child classes(which I want to use to fill the array).
    However if I hardcode the array it works
    eg.
    Code:
        Abstract* a[] = {
            new ConcreteOne(),
            new ConcreteTwo(),
            new ConcreteOne()
        };

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    The first is an array of Abstract objects, which is invalid.
    The second is an array of pointers to Abstract, which is valid.

    Code:
    Abstract** a = new Abstract*[i];
    Remember to delete both every individual object, and then array. A Boost.PtrContainer would make this stuff a lot easier.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    2
    Whooh that was fast.
    Thanks.
    I've just started with C++ so I don't know much about the different libs, but I'll take a look at that Boost library.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you just started with C++ (or even if you haven't) you shouldn't be using new to create a dynamic array. Use vector instead. Or in this case, the boost ptr_vector because you are holding pointers in your array.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2,149
    Quote Originally Posted by Zerok View Post
    Whooh that was fast.
    Thanks.
    I've just started with C++ so I don't know much about the different libs, but I'll take a look at that Boost library.
    just to give you fair warning, the boost library is HUGE. try not to be intimidated by it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 02:17 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  4. delete dynamically allocated char array
    By xddxogm3 in forum C++ Programming
    Replies: 7
    Last Post: 11-23-2003, 03:57 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21