473,727 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template virtual function

7 New Member
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.A ny 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 3161
sicarie
4,677 Recognized Expert Moderator Specialist
::bumping because I was never good at template classes :( ::
Sep 11 '07 #2
alind
7 New Member
plz help me with the problem ?
:(
Sep 21 '07 #3
Benny the Guard
92 New Member
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 New Member
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 New Member
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
3623
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: virtual void do_bar() {} };
9
10281
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 possible, so I need "something like" a virtual function template. I read in some threads that there is a workaround using the visitor pattern. I read therefore in the book "Modern c++ Design" about this pattern, but couldnt find any help to my...
0
1641
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 internal interactive debugger that knows the classes within our system and allow us to walk around the objects that exist at the time of the fault. It mostly works fairly well. That catch being that we have to hand implement some of the code...
6
4004
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 as well. To support thin-template, I need to make virtual function as inline. Now, I know that compiler would generate an out-of-line copy when it
3
3938
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. template<class Type> class base { public: base& operator/=(const base&); Type *image;
4
2008
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>, list<myclass*>, just to access the elements it contains. Today I decided to refactor and to try templates. So I have abstract class A with function foo which receives list<myclass*>. And now I want it to receive an Input Iterator (following the idea behind...
12
1875
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 to call a method defined in A's base class which at runtime determines the template parameters (I know ahead what is allowed) and calls a templated member function B with A's template parameters.
2
1401
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... ______________________________________ template <class T, typename I>
13
7266
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
2345
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, but reflects the structure of my problem. In a nutshell, the abstr. base class VectorCreator has a function createVector whose return type is unfortunately fixed to vector<double>. Only now I discovered that some implementations would like to...
0
9406
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9260
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9185
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6703
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2158
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.