Storing a list of arbitrary objects in C++

前端 未结 13 2037
野趣味
野趣味 2021-02-06 08:29

In Java, you can have a List of Objects. You can add objects of multiple types, then retrieve them, check their type, and perform the appropriate action for that type.
For e

相关标签:
13条回答
  • 2021-02-06 08:51

    Sadly there is no easy way of doing this in C++. You have to create a base class yourself and derive all other classes from this class. Create a vector of base class pointers and then use dynamic_cast (which comes with its own runtime overhead) to find the actual type.

    0 讨论(0)
  • 2021-02-06 08:55

    RTTI (Run time type info) in C++ has always been tough, especially cross-compiler.

    You're best option is to use STL and define an interface in order to determine the object type:

    public class IThing
    {
       virtual bool isA(const char* typeName);
    }
    
    void myFunc()
    {
       std::vector<IThing> things;
    
       // ...
    
       things.add(new FrogThing());
       things.add(new LizardThing());
    
       // ...
    
       for (int i = 0; i < things.length(); i++)
       {
           IThing* pThing = things[i];
    
           if (pThing->isA("lizard"))
           {
             // do this
           }
           // etc
       }
    }
    

    Mike

    0 讨论(0)
  • 2021-02-06 08:59

    Beside the fact, as most have pointed out, you can't do that, or more importantly, more than likely, you really don't want to.

    Let's dismiss your example, and consider something closer to a real-life example. Specifically, some code I saw in a real open-source project. It attempted to emulate a cpu in a character array. Hence it would put into the array a one byte "op code", followed by 0, 1 or 2 bytes which could be a character, an integer, or a pointer to a string, based on the op code. To handle that, it involved a lot of bit-fiddling.

    My simple solution: 4 separate stacks<>s: One for the "opcode" enum and one each for chars, ints and string. Take the next off the opcode stack, and the would take you which of the other three to get the operand.

    There's a very good chance your actual problem can be handled in a similar way.

    0 讨论(0)
  • The short answer is... you can't.

    The long answer is... you'd have to define your own new heirarchy of objects that all inherit from a base object. In Java all objects ultimately descend from "Object", which is what allows you to do this.

    0 讨论(0)
  • 2021-02-06 09:03

    I am a fairly inexperienced, but here's what I'd go with-

    1. Create a base class for all classes you need to manipulate.
    2. Write container class/ reuse container class. (Revised after seeing other answers -My previous point was too cryptic.)
    3. Write similar code.

    I am sure a much better solution is possible. I am also sure a better explanation is possible. I've learnt that I have some bad C++ programming habits, so I've tried to convey my idea without getting into code.

    I hope this helps.

    0 讨论(0)
  • 2021-02-06 09:07

    I'd just like to point out that using dynamic type casting in order to branch based on type often hints at flaws in the architecture. Most times you can achieve the same effect using virtual functions:

    class MyData
    {
    public:
      // base classes of polymorphic types should have a virtual destructor
      virtual ~MyData() {} 
    
      // hand off to protected implementation in derived classes
      void DoSomething() { this->OnDoSomething(); } 
    
    protected:
      // abstract, force implementation in derived classes
      virtual void OnDoSomething() = 0;
    };
    
    class MyIntData : public MyData
    {
    protected:
      // do something to int data
      virtual void OnDoSomething() { ... } 
    private:
      int data;
    };
    
    class MyComplexData : public MyData
    {
    protected:
      // do something to Complex data
      virtual void OnDoSomething() { ... }
    private:
      Complex data;
    };
    
    void main()
    {
      // alloc data objects
      MyData* myData[ 2 ] =
      {
        new MyIntData()
      , new MyComplexData()
      };
    
      // process data objects
      for ( int i = 0; i < 2; ++i ) // for each data object
      {
         myData[ i ]->DoSomething(); // no type cast needed
      }
    
      // delete data objects
      delete myData[0];
      delete myData[1];
    };
    
    0 讨论(0)
提交回复
热议问题