Newbie: pointer to templated abstract base class

Discussion in 'C Programming' started by NickT, Oct 2, 2009.

  1. NickT

    NickT

    Joined:
    Oct 2, 2009
    Messages:
    2
    Guys,
    sorry to ask, but I am trying to create a menu system to a bunch of parameter handlers whose intrinsic values could be int, float, enum etc.

    After some thought, I have tried the following implementation:-
    1. An abstract interface class, templated
    Code:
    template <class T>
    class ComParmIF{
    public:
      virtual T    GetValue(void)=0;
    // common to all types, does not neeed template class
      virtual void GetValString(char *,int )=0;//parameter value as string max n chars
    ...
    };
    2. several types of class to instantiate the template, e.g.

    Code:
    class FP_Parameter:public ComParmIF<float>{
    public:
    //stuff derived from ComParmIF
      virtual float 	GetValue(void);
      virtual void GetValString(char *, int);//parameter value as string max n chars
    
    ...
    private:
    float value;
    };
    3. Declare FP_Parameters
    Code:
    FP_Parameter RejectControl;
    
    And this all compiles quite nicely, so I can call their interface functions with varying data types. (I would have used overloading, it looks simpler, but overloading doesn't look at the return type.)

    But here's the problem...

    When I want to create an array of pointers to my parameter handlers so I can navigate the array as a dynamic menu...the base class (being templated) cannot be used as a data-type. But an array of base class pointers is exactly what I want here.

    i.e.

    Code:
    ComParmIF *menuitem = &RejectControl;
    
    Fails miserably :(

    As this is my first C++ project, I reckon I am going about this the wrong way.

    Any ideas?

    Thanks
     
    NickT, Oct 2, 2009
    #1
    1. Advertising

  2. NickT

    NickT

    Joined:
    Oct 2, 2009
    Messages:
    2
    Newbie: Ignore me

    Bad form to bump the thread...but to let you know that I found the flaw in my own thinking and designed a work-around that doesn't involve returning unspecified data types to a generic interface.:oops:

    The trouble with knowing bits and pieces of other languages is that you forget how strongly typed C++ is, and assume there must be a way to replicate "variant" data types from other languages.

    A little knowledge is a dangerous thing, indeed.

    Thread closed
     
    NickT, Oct 6, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias Kaeppler
    Replies:
    1
    Views:
    470
    R.F. Pels
    May 22, 2005
  2. tirath
    Replies:
    3
    Views:
    733
    Ivan Vecerina
    Oct 12, 2003
  3. sojin
    Replies:
    12
    Views:
    1,937
    marcwentink@hotmail.com
    Apr 7, 2006
  4. skishorev@yahoo.co.in
    Replies:
    4
    Views:
    864
    Rolf Magnus
    May 17, 2006
  5. chhenning
    Replies:
    5
    Views:
    382
    chhenning
    Feb 13, 2008
Loading...

Share This Page