Like Tree1Likes
  • 1 Post By Elysia

compiletime test for inheritance between classes

This is a discussion on compiletime test for inheritance between classes within the C++ Programming forums, part of the General Programming Boards category; a while back, a guy i worked with came up with a really cool bit of code; it would tell ...

  1. #1
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839

    compiletime test for inheritance between classes

    a while back, a guy i worked with came up with a really cool bit of code; it would tell you if one class inherited from another, and this test was done at compile time.

    it was used like this:

    Inheritance<Base,Derived>::exists

    i could really use it now, but i don't have his code and i don't know how he did it. any ideas?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,588
    it probably used dynamic_cast. it could be done like this at runtime:

    Code:
    template<typename Base, typename Derived>
    bool InheritanceExists()
    {
      Derived* derived;
      return dynamic_cast<Base*>(derived);
    }

  3. #3
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    actually it didn't. it was a pure compile-time operation. i just found it. it's on p34 of alexandrescu's modern c++.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,010
    Boost has some interesting meta template stuff.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    definitely not worth pulling in boost over. the implementation is trivial, actually.

    it just so happens this is in the book preview:

    Modern C++ design: generic programming and design patterns applied - Andrei Alexandrescu - Google Books

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,010
    Boost has a lot of other goodies, too. Boost is always worth having.
    Elkvis likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance and Template Classes
    By brunion1 in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2011, 03:23 AM
  2. Help with classes and inheritance!!
    By alacahzam in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2011, 12:39 PM
  3. help in inheritance classes
    By Agnesa in forum C++ Programming
    Replies: 7
    Last Post: 03-11-2003, 10:43 PM
  4. Inheritance Classes w/ same function name
    By Diamonds in forum C++ Programming
    Replies: 3
    Last Post: 03-11-2003, 01:11 AM
  5. Classes & Inheritance
    By TankCDR in forum C++ Programming
    Replies: 5
    Last Post: 03-09-2002, 06:25 PM

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