help in inheritance and typedef

R

Rex_chaos

Hi all,
I am going to do something just like the following tell

template <class A>
class Base
{
typedef Base& ref;

public:
void show(void)
{
cout << "Base" << endl;
}

ref get(void)
{
return *this;
}
};

template <class A>
class Derive: public Base<A>
{
typedef Derive& ref;

public:
void show(void)
{
cout << "Derive" << endl;
}
};

Here, I have a "type" ref defined in the base class. It refers to the
type of the current class. I redefine the type in it's derive class
and expect the
get(void) method will return a right instance. However, it won't.
Actually, I want a function in each class to tell the type itself. How
can I do that ?
 
G

Gianni Mariani

Rex_chaos wrote:
....
Here, I have a "type" ref defined in the base class. It refers to the
type of the current class. I redefine the type in it's derive class
and expect the
get(void) method will return a right instance. However, it won't.
Actually, I want a function in each class to tell the type itself. How
can I do that ?

Why ?
 
J

Jonathan Mcdougall

Here, I have a "type" ref defined in the base class. It refers to the
type of the current class. I redefine the type in it's derive class
and expect the
get(void) method will return a right instance. However, it won't.
Actually, I want a function in each class to tell the type itself. How
can I do that ?

C++ has covariant return values :


class A
{
public:
virtual A get();
};

class B : public A
{
public:
virtual B get();
};


Jonathan
 
J

Jonathan Mcdougall

C++ has covariant return values :
It does.

These are not legal covariant types. It only works with pointers or references.
The above is ill-formed.

class A
{
public:
virtual A& get();
};

class B : public A
{
public:
virtual B& get();
};

I think I'll take vacations for the next couple of days.

Later,


Jonathan
 
R

Ron Natalie

Jonathan Mcdougall said:
C++ has covariant return values :

It does.
class A
{
public:
virtual A get();
};

class B : public A
{
public:
virtual B get();
};
These are not legal covariant types. It only works with pointers or references.
The above is ill-formed.
 
K

klaas

Rex_chaos said:
Hi all,
I am going to do something just like the following tell

template <class A>
class Base
{
typedef Base& ref;

public:
void show(void)
{
cout << "Base" << endl;
}

ref get(void)
{
return *this;
}
};

template <class A>
class Derive: public Base<A>
{
typedef Derive& ref;

public:
void show(void)
{
cout << "Derive" << endl;
}
};

Here, I have a "type" ref defined in the base class. It refers to the
type of the current class. I redefine the type in it's derive class
and expect the
get(void) method will return a right instance. However, it won't.
Actually, I want a function in each class to tell the type itself. How
can I do that ?
You are lucky that someone has thought about you because you can
override returntypes in functions:
so you define:
template <class A>
A& get()/*c++ do not use void here!!*/
{/*whatever code you like*/
};

you just HAVE to redefine the get function to be able to do such thing
I'm afraid...
 
R

Rob Williscroft

Rex_chaos wrote in
Here, I have a "type" ref defined in the base class. It refers to the
type of the current class. I redefine the type in it's derive class
and expect the
get(void) method will return a right instance. However, it won't.
Actually, I want a function in each class to tell the type itself. How
can I do that ?

#include <iostream>
#include <ostream>

template <class A, class Ref = Base< A > >
class Base
{
typedef Ref& ref;

public:
void show(void)
{
std::cout << "Base" << std::endl;
}

ref get(void)
{
return static_cast< ref >( *this );
// cast is base to derived
}
};

template <class A>
class Derive: public Base<A, Derive< A > >
{
public:
void show(void)
{
std::cout << "Derive" << std::endl;
}
};


int main()
{
Derive< int > di;
di.get().show();
}


Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top