Thread: Dilemma with templates and inheritance..!

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    31

    Question Dilemma with templates and inheritance..!

    Heya people,

    In the past couple of days I've been making a little system which I'm going to use in my latest engine to monitor some variables and their values (a bit like the Autos window in MSVC).

    Anyways, I stumbled upon some minor problems here. First off, I'd like to have a class accepting any possible variable type in the project and I found the use of templates extremely useful here.
    However, the problem is that I want to print the contents of a specific variable to the screen and that would require me to get the contents of the variable on char* form. This means I'll have to implement some way to convert the data type to a char*.
    Since I want this system to be as dynamic and abstract as possible I don't like the idea of just hardcoding the handling of all possible types into the core of the system.
    Instead it would be neat if I could e.g have a base class (or similar) which can hold some basic properties, like the name of the variable etc.

    As I said before, since I want the system to be able to handle all possible types (including custom types), I need to implement some way where one can easily add some code to do the conversion from the data type to a char*.
    So far I've been using RTTI to check the data type and then done a simple if.. else if statement to convert to a char*, like this:

    Code:
    if (GetVarType() == "int")
       sprintf(szBuffer, "%d", GetVarValue());
    ...
    Since doing string comparison is pretty slow I'd rather avoid this and come up with a better way to implement the conversion to char*.

    I've been thinking about using inheritance so the user could just add a new class whose only purpose would be to store the pointer to the variable in question AND do the conversion to a char* through some virtual function declared in a base class. I just have a hard time seeing how this could be done in practice, though.

    As you can see I've been thinking quite a bit about this and my ideas might not be the brightest, but I'm hoping some of you have some ideas which I can, hopefully, adapt

    Huge thanks in advance

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I think the way to go is to use template specialization for the various variable types that you are looking at. Create a basic template class to store the required information, and then specialize it for each of your types. For example:
    Code:
    template<class T>
    struct VarInfo
    {
       T& refToVar;
       const char* getString() { return ""; /* default implementation */ }
    };
    
    template<>
    struct VarInfo<int>
    {
       int& refToVar;
       const char* getString() { /* int specific */ }
    };
    What's more is that you could derive this structure from a base (non-templated) structure, and then use a simple factory to construct the appropriate type when you ask for it (without ever explicitly checking the type).

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Why don't you use streams to output the data. That way, any type that has operator<< defined that takes an ostream will work with your class.

    If you must have a char*, you can still use streams. Just use a string stream (in <sstream>): insert the variable into the stringstream using <<, then convert the stringstream to a string using .str() and to a const char* using .c_str().

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    31
    Quote Originally Posted by Zach L.
    Code:
    template<class T>
    struct VarInfo
    {
       T& refToVar;
       const char* getString() { return ""; /* default implementation */ }
    };
    
    template<>
    struct VarInfo<int>
    {
       int& refToVar;
       const char* getString() { /* int specific */ }
    };
    WOW! That's exactly what I had in mind I just didn't know one could directly specialize a template to a specific type so therefore I discarded the idea!

    This is great Thank you so much!

    Quote Originally Posted by jlou
    Why don't you use streams to output the data. That way, any type that has operator<< defined that takes an ostream will work with your class.
    Yeah, Just as I saw your reply I'd already done it with a pal of mine. WOrks blazingly as well.
    Thanks for the great reply!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. templates and inheritance
    By ichijoji in forum C++ Programming
    Replies: 2
    Last Post: 10-11-2004, 03:34 PM
  2. inheritance and templates
    By misplaced in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2004, 03:25 AM
  3. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM
  4. Problem with overloaded operators, templates and inheritance
    By bleakcabal in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2004, 05:07 AM
  5. Mixing templates and inheritance, and the damage caused ;)
    By SilentStrike in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 11:47 PM