delegation class question

  • Thread starter christopher diggins
  • Start date
C

christopher diggins

Given a class which behaves like the following inteface (full implementation
of such a class left out due to its complexity, but it can be found at
http://www.heron-language.com/cpp-iop-example.html ):

interface IFuBar {
void Fu();
void Bar();
};

I have the following code which works:

struct FuBar {
void Fu() { /* ... */ };
void Bar() { /* ... */ };
};

template<typename T> struct FuBarDelegator {
Delegator(T x);
T x;
void Fu() { x.Fu(); };
void Bar() { x.Bar(); };
};

struct mystruct : public Delegator<IFuBar>
{
mystruct() : Delegator<IFuBar>(x); // passes a reference to x
FuBar x;
};

What I am unhappy with is that I have a redundancy of an extra reference to
X in the base class and what might be a useless constructor call. Is there a
way to pass mystruct::x to the Delegator as template parameter? Is there any
another way to avoid the extra pointer?

Thanks in advance!
 
T

tom_usenet

Given a class which behaves like the following inteface (full implementation
of such a class left out due to its complexity, but it can be found at
http://www.heron-language.com/cpp-iop-example.html ):

interface IFuBar {
void Fu();
void Bar();
};

I have the following code which works:

struct FuBar {
void Fu() { /* ... */ };
void Bar() { /* ... */ };
};

template<typename T> struct FuBarDelegator {
Delegator(T x);
T x;
void Fu() { x.Fu(); };
void Bar() { x.Bar(); };
};

There are a few typos above. I assume x should be both passed by
reference and held as a reference. You constructor is also misnamed.
struct mystruct : public Delegator<IFuBar>
{
mystruct() : Delegator<IFuBar>(x); // passes a reference to x
FuBar x;
};

And again, but I think I see the intent.
What I am unhappy with is that I have a redundancy of an extra reference to
X in the base class and what might be a useless constructor call. Is there a
way to pass mystruct::x to the Delegator as template parameter? Is there any
another way to avoid the extra pointer?

The problem is that in the base list of mystruct, you don't know
anything about the members of mystruct - &mystruct::x doesn't exist
yet, so it can't possibly be a base class template parameter. If you
stick to a naming convention (always call the member "x"), you can do:

struct FuBar {
void Fu() { /* ... */ };
void Bar() { /* ... */ };
};

template<typename T, typename Derived>
struct FuBarDelegator {
void Fu() { static_cast<Derived*>(this)->x.Fu(); };
void Bar() { static_cast<Derived*>(this)->x.Bar(); };
};

struct mystruct : public FuBarDelegator<FuBar, mystruct>
{
FuBar x;
};

I don't know whether that helps...

Tom
 
C

Christopher Diggins

tom_usenet said:
There are a few typos above. I assume x should be both passed by
reference and held as a reference. You constructor is also misnamed.


And again, but I think I see the intent.


The problem is that in the base list of mystruct, you don't know
anything about the members of mystruct - &mystruct::x doesn't exist
yet, so it can't possibly be a base class template parameter. If you
stick to a naming convention (always call the member "x"), you can do:

struct FuBar {
void Fu() { /* ... */ };
void Bar() { /* ... */ };
};

template<typename T, typename Derived>
struct FuBarDelegator {
void Fu() { static_cast<Derived*>(this)->x.Fu(); };
void Bar() { static_cast<Derived*>(this)->x.Bar(); };
};

struct mystruct : public FuBarDelegator<FuBar, mystruct>
{
FuBar x;
};

I don't know whether that helps...

Tom

Hi Tom, this helps me out a whole lot, thank you! What I am going to
do then is generate the Delegator using a macro where I pass the name
of the variable as a parameter.

Christopher Diggins
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top