Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Newbie: pointer to templated abstract base class

Reply
Thread Tools

Newbie: pointer to templated abstract base class

 
 
NickT NickT is offline
Junior Member
Join Date: Oct 2009
Posts: 2
 
      10-02-2009
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
 
Reply With Quote
 
 
 
 
NickT NickT is offline
Junior Member
Join Date: Oct 2009
Posts: 2
 
      10-06-2009
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.

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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Defining templated member function outside templated class chhenning C++ 5 02-13-2008 07:36 PM
what is the difference between abstract class and pure abstract class? skishorev@yahoo.co.in C++ 4 05-17-2006 08:07 AM
Can abstract base class have V-table?, Will the pointer to virtual destructor be entered into the virtual table? sojin C++ 12 04-07-2006 09:02 AM
Deriving abstract class from non-abstract class Matthias Kaeppler Java 1 05-22-2005 01:28 PM
[RTTI] cast base class pointer to <templated> derived class pointer tirath C++ 3 10-12-2003 01:44 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57