473,396 Members | 2,158 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,396 software developers and data experts.

Recover template type from non-template base class

Hi all.

Consider the following code fragment:

#include <string>
struct Base{ virtual ~Base() {} };
template <typename T> struct Derived : public Base {};

Base* Create (int param) {
switch (param) {
case 1:
return new Derived<int>;
case 2:
return new Derived<std::string>;
}
}

void foo(Derived<int>*);
void foo(Derived<std::string>*);
Ideally, I would like to write something like this:

int main() {
Base* base = Create(1);
foo(base);
}

I tried to add downcast method to Base:

template <typename T>
struct Derived;
struct Base{
virtual ~Base() {}
template <typename T>
Derived<T> * downcast(){
Derived<T>* d = static_cast<Derived<T>*>(this);
if (d == 0) throw;
return d;
}
}

But it does not help since I still need to specify Derived type during
foo call:
foo(base->downcast<int>);

which I want to avoid.

Any idea how to do it?

Thanks,
Mike Alexeev

Jun 1 '06 #1
4 2146

mi*************@qwest.com wrote:
Hi all.

Consider the following code fragment:

#include <string>
struct Base{ virtual ~Base() {} };
template <typename T> struct Derived : public Base {};

Base* Create (int param) {
switch (param) {
case 1:
return new Derived<int>;
case 2:
return new Derived<std::string>;
}
}

void foo(Derived<int>*);
void foo(Derived<std::string>*);
Ideally, I would like to write something like this:

int main() {
Base* base = Create(1);
foo(base);
}

I tried to add downcast method to Base:

template <typename T>
struct Derived;
struct Base{
virtual ~Base() {}
template <typename T>
Derived<T> * downcast(){
Derived<T>* d = static_cast<Derived<T>*>(this);
if (d == 0) throw;
return d;
}
}

But it does not help since I still need to specify Derived type during
foo call:
foo(base->downcast<int>);

which I want to avoid.

Any idea how to do it?

Thanks,
Mike Alexeev


What you need is a CRTP. do google on " Curiously recurring template
pattern".

template <class Derived>
struct base
{
Derived* downcast()
{
return static_cast<Derived*>(this);
}
};

struct derived : base<derived>
{
};

Jun 1 '06 #2

mi*************@qwest.com wrote:
Hi all.

Consider the following code fragment:

#include <string>
struct Base{ virtual ~Base() {} };
template <typename T> struct Derived : public Base {};

Base* Create (int param) {
switch (param) {
case 1:
return new Derived<int>;
case 2:
return new Derived<std::string>;
}
}

void foo(Derived<int>*);
void foo(Derived<std::string>*);
Ideally, I would like to write something like this:

int main() {
Base* base = Create(1);
foo(base);
}

I tried to add downcast method to Base:

template <typename T>
struct Derived;
struct Base{
virtual ~Base() {}
template <typename T>
Derived<T> * downcast(){
Derived<T>* d = static_cast<Derived<T>*>(this);
if (d == 0) throw;
return d;
}
}

But it does not help since I still need to specify Derived type during
foo call:
foo(base->downcast<int>);

which I want to avoid.

Any idea how to do it?

Thanks,
Mike Alexeev


What you need is a CRTP. do google on " Curiously recurring template
pattern".

template <class Derived>
struct base
{
Derived* downcast()
{
return static_cast<Derived*>(this);
}
};

struct derived : base<derived>
{
};

Jun 1 '06 #3

ampar...@gmail.com wrote:
What you need is a CRTP. do google on " Curiously recurring template
pattern".

template <class Derived>
struct base


Unfortunately, Base class can't be a template class. It could only have
template methods.

Thanks,
Mike

Jun 1 '06 #4

mi*************@qwest.com wrote:
ampar...@gmail.com wrote:
What you need is a CRTP. do google on " Curiously recurring template
pattern".

template <class Derived>
struct base


Unfortunately, Base class can't be a template class. It could only have
template methods.

Thanks,
Mike


the following works for me.

template<typename T>
struct derived;

struct base
{
template<typename T>
derived<T>* downcast()
{
return static_cast<derived<T>*>(this);
}

};

template <class T>
struct derived : base
{
void func()
{
cout << " derived::foo" << endl;
}
};

template<typename T>
void foo(derived<T>* d)
{
d->func();
}

You could also use a conversion operator if you want instead of a
downcast function.

Jun 1 '06 #5

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

Similar topics

1
by: WKC | last post by:
Recently, one of our database's mdf and ldf was corrupted. We were able to bring back the database with the capability of importing and querying the data. However, the data is not the full list. ...
4
by: chuy | last post by:
Is there a way to recover a lost password in Mysql 4.1. I have seen many articles and howto's on how to kill the Mysql process and then restart using skip grant tables option. I am not trying to...
2
by: Nate | last post by:
Hello, I am trying to recover a SQL Server 7 database from another hard disk drive that has a corrupted Windows 2000 Advanced Server installation. I am not able to repair the corrupted Windows...
3
by: Chris | last post by:
Hi, I'm really begginer in javascript. I created a form in a pop-up opened with javascript. how to recover variables of the form in the main page. Thanks for your help Chris.
4
by: SteveK | last post by:
Hi... first off... still a novice here, I subscribe to a cable-ISP... no CGI, PERL, etc... I've built a webpage and want to "gather info" from someone looking at my webpage. So far, I've tried...
5
by: Prem K Mehrotra | last post by:
I come from Oracle background. In Oracle, when one wants to do a point in time recovery, one can specify recover database until timestmap. Oracle's database maps to a db2 subsystem, i.e., in...
3
by: apple | last post by:
UDB v8 fp 6a on AIX 5.1.0.0 Below is a manual incremental recover from compressed backup datasets. With external compress backup datasets, can it be coded to do an automatic incremental recover?...
0
by: JK | last post by:
Hi all, if I use RECOVER to END OF LOGS on a partitioned database, will the necessary RESTORE#s for the non-catalog partitions run in parallel or one after the other ? Haven't found anything and...
5
by: Grant | last post by:
Hi Is there a way to recover deleted records from a table. A mass deletion has occurred and Access has been closed since it happened Louis
0
by: mike_dba | last post by:
I have been testing the db2 recover command on a DB2 V8.2 Linux database. The database contains a single partition. I am not archiving logs but retaining them on disk. the backup image is to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.