473,327 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

template virtual function

7
I m having a problem with templates. A per C++ std says i cant use override template virtual functions. in other words virtual functions cant be templates due to some vtable size issues. Now i want to simulate it somehow. I read in some other posts that it is somehow possible throg visitor pattern but i m unable to get it done. My intended code is posted below. can somebody give me a workaround to get this functionality.Any help is appreciated. I tried searching in other posts but was not able to get the correct ans to my problem. Thanx in advance.
Expand|Select|Wrap|Line Numbers
  1. //--------------------------
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class Mat
  6. {
  7.     public:
  8.         template <class T> virtual  void read(T &x) = 0;
  9.         template <class T>virtual  void read(T &x, const int  &y) = 0;
  10. };
  11. class Mat1:public Mat
  12. {
  13.     public:
  14.         /*virtual*/ template <class T> void read(T &x)
  15.         {
  16.             cout << "mat1 ::x"<< zz <<endl;
  17.         }
  18.         /*virtual*/ template <class T> void read(T &x, const int  &y)
  19.         {
  20.             cout << "mat1 ::xx"<< zz <<endl;
  21.         }
  22.         private:
  23.     int zz;
  24. };
  25. class Mat2:public Mat
  26. {
  27.     public:
  28.         /*virtual*/ template <class T> void read(T &x)
  29.         {
  30.             cout << "mat2 ::x"<<z << endl;
  31.         }
  32.         /*virtual*/ template <class T> void read(T &x, const int  &y)
  33.         {
  34.             cout << "mat2 ::xx "<< z <<endl;
  35.         }
  36.     private:
  37.         int z;
  38. };
  39.  
  40. int main()
  41. {
  42.     Mat* x = new Mat1();
  43.     x->read(3);
  44.     x->read (4,2);
  45.     return 0;
  46. }
  47. //--------------------------
//passing of the variables in functions are importatnt coz the internal //functionality depends on them. Also private members are also needed //in my design.
Sep 10 '07 #1
5 3106
sicarie
4,677 Expert Mod 4TB
::bumping because I was never good at template classes :( ::
Sep 11 '07 #2
alind
7
plz help me with the problem ?
:(
Sep 21 '07 #3
One thing you could try is make your class the template. Then no problems overriding the virtual functions. Should work as long as you have T the same for every function.
Sep 21 '07 #4
alind
7
i cannot make the class as template because in my case T is the thing that is going to vary. (i want to use boost matrices as T.) THe selection of pasticular type of boost matrix will depend upon the tun time conditions.
Sep 25 '07 #5
alind
7
so it seems that my problem does not have any solution
k thanks for aa the help buddies.
Oct 3 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo { public: void bar() { do_bar(); } protected:...
9
by: Sebastian Faust | last post by:
Hi, I have a design problem about which I am thinking now for a while and still couldnt find any help in deja. What I need is something like a virtual function template. I know that this is not...
0
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. Essentially at the point of the error, we invoke an...
6
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual...
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
4
by: pocmatos | last post by:
Hi all, I have an abstract class acting as interface to a given class of objects. And mostly everywhere around my program I'm passing things like: vector<int>, list<unsigned long>,...
12
by: stefan.bruckner | last post by:
Hi, I am looking for a way to achieve the following. I've tried a couple of things, but they all ended up being too complicated: I have a templated class A. I want another class B to be able...
2
by: .rhavin grobert | last post by:
hello;-) i have that following little template that defines some type of vector. it works with structs and i want to use it also for simple pointers. the problem is, in following function......
13
by: Mike -- Email Ignored | last post by:
Is a pure virtual function in allowed in a template base class? In any case, I have one working. Am I skating on thin ice? Thanks, Mike.
3
by: Markus Dehmann | last post by:
I have an abstract base class which contains a function that I would like to template, but virtual template functions are illegal. I put a mini code example below, which doesn't do anything great,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.