variable return type in c++ class

前端 未结 7 1724
花落未央
花落未央 2021-01-25 08:33

i have a class with the following structure:

class myClass
{
    private:
        int type;
        classOne objectOne;
        classTwo objectTwo;
    public:
          


        
相关标签:
7条回答
  • 2021-01-25 08:42

    I am not sure why you would not use templates for your case.

    You can have something like below:

    template <class ClassType>
    class myClass
    {
        private:
            int type;
            ClassType object;            
        public:
            myClass(ClassType object_in)
            {
                this->object = object_in;
                /*
                    C++ doesn't support reflection so I don't think there 
                    is a robust way of doing the following at runtime.
                */
                type = /* Get Type at runtime */;
            }
            /*
                Have another method which return object in a straigtforward way.
            */
    };
    

    However, then this become trivial. Any more insight into what your use case is, such that you have to know the type?

    Update: If the ClassType is going to be an Object, you can have a const static int TypeID member for the class, which is set at compile time. You can then use it determine the Type at runtime.

    0 讨论(0)
  • 2021-01-25 08:48

    If they're completely different structures, with no common base then an alternative way you can return them from the same function is to use void*.

    However that's bad form in C++, usually indicating a design failure - either use two different functions, or use a common base class.

    It's apples and oranges. If you put an apple into an recipe that calls for an orange it won't be the same recipe anymore.

    0 讨论(0)
  • 2021-01-25 08:54

    Unless the two types are related (in which case you can create a function that will return a pointer/reference to the common ancestor) you cannot do that directly in C++.

    C++ is a statically typed language, meaning that the type of every expression must be known at compile time, but you are trying to define a function whose return type depends on runtime values.

    Depending on the particular problem to solve, there might be different approaches that you could take, including using type erasure (return a boost::any, boost::variant or your own type-erasure).

    0 讨论(0)
  • 2021-01-25 08:56

    ClassOne and ClassTwo need to have the same return type then either via inheritance or composition. i.e ClassOne and ClassTwo need to be subclasses of the same super class OR they need to impl the same interface.

    0 讨论(0)
  • 2021-01-25 08:56

    The first problem is how you will determine the class of which type has been returned. I think it is possible to return a pointer to structure of this type

    struct res {
      myClass* c1;
      ClassOne* c2;
    } ;
    

    The field of the not chosen class is NULL, the other points to the object.

    0 讨论(0)
  • 2021-01-25 08:57

    The use of type-id is a sign that you need virtual functions for myClass. Even if the other two classes are totally independent, the fact that they are returned by the same function could easily make them inherit a base class. And also you can just return a pair containing class1, class2 and one of them can be null.

    0 讨论(0)
提交回复
热议问题