Implicit conversion and method call

G

GeeRay

Hi all,
how can I create a template class to decorate a type and use it as the
type itself?

For example:

I want to do this:

#include <iostream>
class A
{
public:
A(){};
virtual ~A(){};
void foo(){ std::cout << "foo" << std::endl;};
};


template<class T>
class B
{
public:
B(){};
virtual ~B(){};

operator T(){return instance;};
private:
T instance;
};



int main(int argn, char* argv[])
{
B<A> b();
b.foo();
}


Is it possible?

Thanks in advance.
 
V

Victor Bazarov

GeeRay said:
Hi all,
how can I create a template class to decorate a type and use it as the
type itself?

For example:

I want to do this:

#include <iostream>
class A
{
public:
A(){};
virtual ~A(){};
void foo(){ std::cout << "foo" << std::endl;};
};


template<class T>
class B
{
public:
B(){};
virtual ~B(){};

operator T(){return instance;};
private:
T instance;
};



int main(int argn, char* argv[])
{
B<A> b();

Drop the parentheses, otherwise you're declaring a function. My answer
assumes that the definition of 'b' is like this:

B said:
b.foo();
}


Is it possible?

No. For the member function calls (like the . you use to access the
'foo' member) the conversions are not considered. You can overload the
member access operator for pointers (pretending that your 'B' class is a
pointer), like so:

template<class T> class B { ...

T* operator->() { return &instance; }
};

, then you could write something like

B<A> b;
b->foo();

which is not necessarily the best syntax, of course...

V
 
V

Vladimir Jovic

Victor said:
GeeRay said:
Hi all,
how can I create a template class to decorate a type and use it as
the type itself?

For example:

I want to do this:

#include <iostream>
class A
{
public:
A(){};
virtual ~A(){};
void foo(){ std::cout << "foo" << std::endl;};
};


template<class T>
class B
{
public:
B(){};
virtual ~B(){};
operator T(){return instance;};
private:
T instance;
};



int main(int argn, char* argv[])
{
B<A> b();

Drop the parentheses, otherwise you're declaring a function. My answer
assumes that the definition of 'b' is like this:

B said:
b.foo();
}


Is it possible?

No. For the member function calls (like the . you use to access the
'foo' member) the conversions are not considered. You can overload the
member access operator for pointers (pretending that your 'B' class is a
pointer), like so:

template<class T> class B { ...

T* operator->() { return &instance; }
};

, then you could write something like

B<A> b;
b->foo();

which is not necessarily the best syntax, of course...

Another happy solution:
static_cast< A >(b).foo();

or something little better:

#include <iostream>
class A
{
public:
A(){}
virtual ~A(){}
void foo(){ std::cout << "foo" << std::endl;}
};
template<class T>
class B
{
public:
B(){}
~B(){}

T& operator()(){ return instance; }
private:
T instance;
};
int main(int argn, char* argv[])
{
B<A> b;
b().foo();
}
 

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

Forum statistics

Threads
473,772
Messages
2,569,588
Members
45,099
Latest member
AmbrosePri
Top