Help deducing the derived class inside a base class

This is a discussion on Help deducing the derived class inside a base class within the C++ Programming forums, part of the General Programming Boards category; Hi! In my program, I've been wanting to determine the the derived type of a base class. So far, this ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    6

    Help deducing the derived class inside a base class

    Hi!

    In my program, I've been wanting to determine the the derived type of a base class. So far, this seems to work fine:

    Code:
    class BaseClass
    {
    public:
          template<class derived>
          void PrintType(derived object)
          {
               cout << "type: " << typeid(object).name() << endl;
          }
    };
    
    class DerivedClass : public BaseClass
    {
    protected:
          void Foo()
          {
               PrintType(*this);
          }
    };
    
    int main()
    {
              DerivedClass test_object;
              test_object.Foo();
              cin.get();
    }

    However, in the context of what I intend to write, PrintType() is always called within a derived class of BaseClass and the argument is always going to be *this" (with or without * works for me). Not only will the argument always be the same, but it is the only argument for my program to function as I want it. So I was wondering: is there a way to re-write PrintType() such that it has it takes no arguments and can deduce the derived type on its own?

    Thanks!

  2. #2
    a guy with long hair Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    103
    Either revise your design, or... Try to use a pattern that's already there. Maybe visitor pattern?
    Visitor pattern - Wikipedia, the free encyclopedia
    Visitor Pattern

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, it could do that if the base class was polymorphic (contained a virtual function), in which case RTTI determines the most derived type of the given value.

    Code:
    #include <iostream>
    #include <typeinfo>
    using namespace std;
    
    class BaseClass
    {
    public:
          virtual ~BaseClass() {}
          void PrintType()
          {
               cout << "type: " << typeid(*this).name() << endl;
          }
    };
    
    class DerivedClass : public BaseClass
    {
    public:
          void Foo()
          {
               PrintType();
          }
    };
    
    int main()
    {
              DerivedClass test_object;
              test_object.Foo();
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Quote Originally Posted by anon
    Yes, it could do that if the base class was polymorphic (contained a virtual function), in which case RTTI determines the most derived type of the given value.
    However, before you reach for this solution, take Xupicor's advice.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, eventually you could just use a free function:

    Code:
    #include <iostream>
    #include <typeinfo>
    using namespace std;
    
    class BaseClass
    {
    };
    
    class DerivedClass : public BaseClass
    {
    };
    
    template <class T>
    void PrintType(const T& value)
    {
        cout << "type: " << typeid(value).name() << endl;
    }
    
    int main()
    {
              DerivedClass test_object;
              PrintType(test_object);
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Maybe I should ask a more over-arching question: why do you want to print typeid(object).name() in the first place?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    It's not that important really. I was just working on a windows API wrapper class. I wanted a simple base class encapsulating class registration and window creation. Upon inheriting from the base class, the window class name automatically becomes the name of the inherited class. I know there are probably MUCH better ways of doing it, but that is what seemed most obvious to me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. base class pointer pointing at derived class
    By mynickmynick in forum C++ Programming
    Replies: 11
    Last Post: 12-01-2008, 12:26 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Replies: 3
    Last Post: 10-27-2006, 01:33 AM
  5. Replies: 1
    Last Post: 11-27-2001, 01:07 PM

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