473,394 Members | 1,893 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,394 software developers and data experts.

inheritance and templates

Hi guys,
I'm becoming crazy with templates, maybe for it's a trivial problem...
but I can't go through :-(
any hint will be appreciated.

I show you without templates a piece of code: 2 classes, one inherited
by the other, like these:

#include <iostream>
using namespace std;

class A {
public:
A () {a=100; b=50;};
virtual void f()=0;
void g() {b=20;cout<<endl<<b<<endl;};
int a; int b;
};

class B: public A {
public:
B(){ c=30; }
void f(){cout <<endl<< "result:" << a << " "<< " "<< b << " " << c;
};
int c; int d;
};

//and then the main with a pointer to the class base.

int main(int argc, char **argv){
A * aPtr = new B();
aPtr->f();
aPtr->g();
delete aPtr;
return 0;
}

it works great!!
Now suppose that I need "a" and "c" of a generic type T and "b" and "d"
of a generic type U.
And now, begans the problem!

I wrote this (it compiles):

template <typename T, typename U>
class A {
public:
A () {a=100; b=50;};
virtual void f()=0;
void g() {b=20;cout<<endl<<b<<endl;};
T a; U b;
};

template <typename T, typename U>
class B: public A < B<T,U>, B<T,U {
public:
B(){ c=30;}
void f(){
cout <<endl<< "result:" << A<T,U>::a << " "<< " "<< A<T,U>::b <<
" " << c;
};

T c; U d;
};
but I really don't know how to declare the functions in the main, in a
similar way as I did for a non-template version. I've tried several
things, even monsters like:

int main(int argc, char **argv){
A<int,int* aPtr;
aPtr = new B< <int,int>, <int,int ();
aPtr->f(); aPtr->g();
delete aPtr;
return 0;
}
nothing, I don't know,
do you have any hint?

thanks a lot,
Tirzanello

Jul 30 '06 #1
2 1406
ti********@gmail.com schrieb:
Hi guys,
I'm becoming crazy with templates, maybe for it's a trivial problem...
but I can't go through :-(
any hint will be appreciated.

I show you without templates a piece of code: 2 classes, one inherited
by the other, like these:

#include <iostream>
using namespace std;

class A {
public:
A () {a=100; b=50;};
virtual void f()=0;
void g() {b=20;cout<<endl<<b<<endl;};
int a; int b;
};

class B: public A {
public:
B(){ c=30; }
void f(){cout <<endl<< "result:" << a << " "<< " "<< b << " " << c;
};
int c; int d;
};

//and then the main with a pointer to the class base.

int main(int argc, char **argv){
A * aPtr = new B();
aPtr->f();
aPtr->g();
delete aPtr;
return 0;
}

it works great!!
Now suppose that I need "a" and "c" of a generic type T and "b" and "d"
of a generic type U.
And now, begans the problem!

I wrote this (it compiles):

template <typename T, typename U>
class A {
public:
A () {a=100; b=50;};
virtual void f()=0;
void g() {b=20;cout<<endl<<b<<endl;};
T a; U b;
};

template <typename T, typename U>
class B: public A < B<T,U>, B<T,U {
You should inherit from the same type as you use four lines later: A<T,U>

You made it more complex than it is.
public:
B(){ c=30;}
void f(){
cout <<endl<< "result:" << A<T,U>::a << " "<< " "<< A<T,U>::b <<
" " << c;
};

T c; U d;
};
but I really don't know how to declare the functions in the main, in a
similar way as I did for a non-template version. I've tried several
things, even monsters like:

int main(int argc, char **argv){
A<int,int* aPtr;
aPtr = new B< <int,int>, <int,int ();
aPtr = B<int,int>();
aPtr->f(); aPtr->g();
delete aPtr;
return 0;
}
--
Thomas
Jul 30 '06 #2
You should inherit from the same type as you use four lines later: A<T,U>

it works!!!! it works!!!!

thanks a lot Thomas!!!

cheers
tirzanello

Jul 30 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
1
by: Markus Seeger | last post by:
Hi, I'm writing a commandline argument parser that can handle switches by using some techniques similar to the Qt slot mechanism. example: // inheritance method (what I'm trying at the...
5
by: Dart | last post by:
What are the major advantages of inheritance of a class of no virtual functions, promoting code re-use? Thanks!
3
by: darkstorm | last post by:
I have a doubt regarding inheritance involving templates Consider this: ///////////////////////////////////// template<typename T> class A { private: T m_a;
10
by: makc.the.great | last post by:
now that I am looking at templates, there's the question. why same effect couldn't/shouldn't be achieved with inheritance? provided the difference of the two, when and where do I use templates...
6
by: Ravi Rao | last post by:
Hi, This is about "templates vs inheritance" (please, before you flame me, I do understand that they cover largely non-intersecting domains). Apart from D&E*, I've found either online...
4
by: Joe | last post by:
We are working on a project that requires 3rd parties (ie, consultants) to use our ASP.net webforms in a very reusable manner. The big catch is that we are not allowed to give them source. There...
4
by: srinivasarao_moturu | last post by:
class ABC { public : virtual void operation1(); virtual void operation2(); virtual int GetValue(); virtual char GetValue(); virtual void SetValue(int); virtual void SetValue(char); }
5
by: Lars Hillebrand | last post by:
Hello, i discovered a weird behaviour if i use templates together with virtual inheritance and method over. I managed to reproduce my problem with a small example: // *********** <code...
2
by: Isaac Gelado | last post by:
Hi, I am having problems with inheritance in templates classes. Say I have the following classes: class A {}; class B: public A {}; template<typename Tclass C {}; Now in my code I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...

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.