Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Implicit conversion and method call

0 views
Skip to first unread message

GeeRay

unread,
Apr 29, 2009, 9:59:45 AM4/29/09
to
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.

Victor Bazarov

unread,
Apr 29, 2009, 10:43:44 AM4/29/09
to
GeeRay wrote:
> 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<A> b;

> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Vladimir Jovic

unread,
Apr 29, 2009, 12:35:49 PM4/29/09
to

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();
}

blargg

unread,
Apr 29, 2009, 9:37:48 PM4/29/09
to
Just some minor style issues unrelated to your question (which someone
else already answered):

GeeRay wrote:
> how can I create a template class to decorate a type and use it as the

"class template". A template is not a class.

[...]
> class A
> {
> public:
> A(){};
^
> virtual ~A(){};
^


> void foo(){ std::cout << "foo" << std::endl;};

^
These semicolons are not appropriate here. They don't go after function
definitions.

[...]


> int main(int argn, char* argv[])

^^^^
More commonly called "argc".

Victor Bazarov

unread,
Apr 30, 2009, 11:25:44 AM4/30/09
to

Or just

A(b).foo();

will probably work just as well. But the point is that the OP didn't
want to remember that 'A' was involved. So, your operator() solution is
a bit better in that respect.

> [..]

0 new messages