How to get subtype of a derived class in base class

rajenpandit

Consider the following example

template <typename T>
class A
{
        typename T::x a;          //not used
        virtual typename T::x* function()=0;
};

class B:public A<B>
{
public:
        typedef XXX x;
        x obj;
        x* function()
        {
             return &obj;
        }

};

Here I want to use the the type B::x inside class A.

Is there any way to do this?

Excelcius

This doesn't work. In order to instantiate A, the compiler needs to have at least a forward declaration of x. But you cannot forward declare members of a type as this is an implementation detail of the type and would imply that you already know some parts about the class.

You could, however, try to pass the return type directly to A and use this as the return type:

template <typename T>
class A
{
public:
    typedef T ret_type;
    virtual ret_type* function() = 0;
};

class B : public A<int>
{
public:
    A::ret_type obj;
    virtual A::ret_type* function() override
    {
         return &obj;
    }
};

With this approach you could do this:

class X : public A<int> { ... };
class Y : public A<int> { ... };

A<int>* p1 = new X();
A<int>* p2 = new Y();
p1->function();
p2->function();

...making the baseclass much more useful.

A parent should not need to know anything about its children, otherwise inheritance becomes meaningless. In your case A can be considered an interface and it makes sense that any class implementing the interface A<int> will behave the same. Contrary, if you pass the child-class to A, every interface implementation is different and unique, even though it might have exactly the same return types. If you really need this, consider making A a utility class and embedding it inside the subclass as a member ("favor composition over inheritance").

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to Get Base Class Instance from a Derived Class

From Dev

How to get only base class properties from derived class?

From Dev

How to deduce subtype of a class from a base pointer

From Dev

How to serialize derived class by pointer to base class? Derived::serialize not called

From Dev

Member variable of subtype of base class in base class

From Dev

How to get access to the attributes of an derived class from the base class in C++?

From Dev

Convert a base class to a derived class

From Dev

composition of a derived class in a base class

From Dev

Base class to Derived class in Iteration

From Dev

composition of a derived class in a base class

From Dev

How to serialize the base class with derived classes

From Dev

How to use a derived property on a base class?

From Dev

base pointer to derived class

From Dev

Cast derived class to base

From Dev

Constructor in base and derived class

From Java

Is there a way to get a derived class' type in a base class' function?

From Dev

Is it possible to get derived class' property if it is in the form of base class?

From Dev

How to return derived type while operating over base and derived class?

From Dev

How to call an operator from a base class within the derived class?

From Dev

How to pass a derived class property to the constructor of a base class

From Dev

How to call a function from a derived class in a base class?

From Dev

How to call derived class method from base class pointer?

From Dev

How to make Derived class templated on Base class in CRTP

From Dev

How to make a derived concrete class implicitly cast a generic base class?

From Dev

how to serialize a base class variable with a different name in a derived class

From Dev

How can a derived class invoke private method of base class?

From Dev

How to return a derived class using only code in the base class?

From Dev

How to call constructor of a template base class in a template derived class?

From Dev

How to use a covariant return to retrieve a derived class from a base class?

Related Related

  1. 1

    How to Get Base Class Instance from a Derived Class

  2. 2

    How to get only base class properties from derived class?

  3. 3

    How to deduce subtype of a class from a base pointer

  4. 4

    How to serialize derived class by pointer to base class? Derived::serialize not called

  5. 5

    Member variable of subtype of base class in base class

  6. 6

    How to get access to the attributes of an derived class from the base class in C++?

  7. 7

    Convert a base class to a derived class

  8. 8

    composition of a derived class in a base class

  9. 9

    Base class to Derived class in Iteration

  10. 10

    composition of a derived class in a base class

  11. 11

    How to serialize the base class with derived classes

  12. 12

    How to use a derived property on a base class?

  13. 13

    base pointer to derived class

  14. 14

    Cast derived class to base

  15. 15

    Constructor in base and derived class

  16. 16

    Is there a way to get a derived class' type in a base class' function?

  17. 17

    Is it possible to get derived class' property if it is in the form of base class?

  18. 18

    How to return derived type while operating over base and derived class?

  19. 19

    How to call an operator from a base class within the derived class?

  20. 20

    How to pass a derived class property to the constructor of a base class

  21. 21

    How to call a function from a derived class in a base class?

  22. 22

    How to call derived class method from base class pointer?

  23. 23

    How to make Derived class templated on Base class in CRTP

  24. 24

    How to make a derived concrete class implicitly cast a generic base class?

  25. 25

    how to serialize a base class variable with a different name in a derived class

  26. 26

    How can a derived class invoke private method of base class?

  27. 27

    How to return a derived class using only code in the base class?

  28. 28

    How to call constructor of a template base class in a template derived class?

  29. 29

    How to use a covariant return to retrieve a derived class from a base class?

HotTag

Archive