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

delegation class question

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!

--
Christopher Diggins
http://www.cdiggins.com
http://www.heron-language.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #1
2 1167
On 26 Apr 2004 18:17:10 -0400, "christopher diggins"
<cd******@videotron.ca> wrote:
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++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #2
tom_usenet <to********@hotmail.com> wrote in message news:<cp********************************@4ax.com>. ..
On 26 Apr 2004 18:17:10 -0400, "christopher diggins"
<cd******@videotron.ca> wrote:
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


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
Jul 22 '05 #3

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

Similar topics

7
by: Rene Pijlman | last post by:
Section 6.5 "What is delegation?" of the FAQ says: "Python programmers can easily implement delegation. For example, the following class implements a class that behaves like a file but converts...
1
by: Robert Dick | last post by:
Derived classes sometimes need to delegate portions of the work in overridden methods to methods in their base classes. This was traditionally done with explicit calls in python, e.g., class...
6
by: DPfan | last post by:
Is the following so-called "delegation"? If not how to make some changes so that the F class delegates its operation to an E instance. On the other hand the following code runs without any...
3
by: Tony Johansson | last post by:
Hello! What does it mean with delegation and can you give me one example. //Tony
2
by: russell.lane | last post by:
I'm building out a pretty standard n-tier web application. The stack includes application/presentation, biz logic, and data access layers on top of an SQL server back end. We want to use...
4
by: JimLad | last post by:
In advance, sorry if this is the wrong group... SQL Server 2000 SP3 on Server 2003. SQL Account and Computer both Trusted for Delegation. Given SPN. IIS 5.0 on W2000. Kerberos enabled....
6
by: Marc Castrechini | last post by:
This is a classic double hop delegation issue, however its the first time we are setting this up so we are doing something incorrectly. If we run through the IDE or using a localhost path on the...
5
by: =?Utf-8?B?TWF5ZXI=?= | last post by:
Hi, I'm using two form classes and I would like all methods of the second class (the child class) to be managed by the first class (the main class). Is delegation the best solution for me? If so,...
6
by: cbare | last post by:
Hello JS Gurus, One thing I haven't figured out about javascript is the treatment of __proto__. Inheritence, whether prototypes or class-based, is just a shorthand form of delegation (leaving...
13
by: barcaroller | last post by:
What is the common way/design-pattern (if any) in C++ for delegating function calls that are not handled by a certain class. Public inheritance would be one way but not all classes are meant to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.