Are array of pointers to different types possible in c++?

后端 未结 7 652
灰色年华
灰色年华 2020-12-18 03:46

Are array of pointers to different types possible in c++? with example please)

7条回答
  •  余生分开走
    2020-12-18 04:03

    C++ is C with more stuff. So if you want to do it the C way, as above you just make an array of void pointers

    void *ary[10];
    ary[0] = new int();
    ary[1] = new float();
    

    DA.

    If you want to do things the object oriented way, then you want to use a collection, and have all the things you going to be adding to the collection derive from the same base object class that can be added to the collection. In java this is "Object", C++ has no base object built in, but any collection library you use will have such a thing that you can subclass.

提交回复
热议问题