Is it possible???

This is a discussion on Is it possible??? within the C++ Programming forums, part of the General Programming Boards category; Is it possible to have an array of pointers pointing to instances of different classes? For instance, I have several ...

  1. #1
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669

    Is it possible???

    Is it possible to have an array of pointers pointing to instances of different classes?

    For instance, I have several different type instances:

    int *pI;
    double *pD;
    myClass *pmCl;

    All of them are pointers, I want to place all of these pointers into an array - How can this be done - can it be done?

    A pointer only holds the memory address of the variable so what type do I have to initialize the array to be in order to hold those addresses. Normally, I would have to declare an array of specific types such as an int array to hold the int pointers and another array to hold the pointers to the instances of my class.

    Is it possible to acheive this or is this what Linked Lists are for?
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    A) I don't think this can be done with arrays.

    B) This can be done with linked lists as long as all of the data types are declared within each structure.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you could cast them to void* the generic pointer
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    That sounds a bit stupid to me. why would you want to do that???

    Stoned is right, you could cast to void* but you'd have to cast them back to the correct type before you use them.. in which case you'd need to keep track of what is what elsewhere in your code... dont you think that kinda defeats the purpose???

    Smells like bad coding to me

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  5. #5
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    This was not a question asked for my purposes really. I just wished to know how or if it could be done. My motivation was another question asked on this forum that recieved no answer about someone who was creating an array of pointers containing pointers to classes, pointer-to-pointer. He was getting casting errors when trying to dereference the ptr-ptr (**) array values.

    An array of general pointers is something that I can't really see myself using right now but it does answer my question about whether a pointer can hold the address of any type. A void* variable can hold the address of any type, thus it's sorta like polymorphism.

    I don't know why or where I would use this, but then again I don't know why someone would use an array of pointers to pointers to classes and I don't know where I would have occasion to use pointers to pointers period unless derefrencing?
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  6. #6
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    How about putting all the different classes that you want to keep inside a union, along with an int which keeps track of what type of class it is? You then have an array of pointers to that union.

    ie:

    struct classes
    {
    ___int type;


    ___union myClass
    ___{
    ______class one
    ______{
    _________...
    ______};

    ______class two
    ______{
    _________...
    ______};

    ______class three
    ______{
    _________...
    ______};

    ___}
    }
    .
    .
    .

    You would use the type to determine which class to access and subsequently which code to execute pertaining to a particular class.

    Might work...

    ps Sorry about the underscores. It's the only way I can think of to get the indentations (it strips leading spaces).
    Last edited by samGwilliam; 02-07-2002 at 11:17 AM.

  7. #7
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >thus it's sorta like polymorphism.

    Yes, if the classes are in an object hierachy you wouldn't need to mess around with void pointers, just create an array of base pointers and use virtual methods.

  8. #8
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    [ps Sorry about the underscores. It's the only way I can think of to get the indentations (it strips leading spaces)
    Use code tags [c o d e] & [/c o d e] w/o spaces.

Popular pages Recent additions subscribe to a feed

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