473,796 Members | 2,590 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delegation question...

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 inherit
from (e.g. STL).
Example:

class A
{
public:
foo();

private:
set<stringmyset ;
}

A myObj;
myObj.insert(); // compiler error of course
Is there some mechanism (direct or indirect) where a function that is
not handled by myObj gets delegated to another object (e.g. myset)?
Jun 27 '08 #1
13 1470
barcaroller wrote:
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 inherit
from (e.g. STL).
Example:

class A
{
public:
foo();

private:
set<stringmyset ;
}

A myObj;
myObj.insert(); // compiler error of course
Is there some mechanism (direct or indirect) where a function that is
not handled by myObj gets delegated to another object (e.g. myset)?
I believe you can add the following to your class:
set<string&oper ator->() { return myset; }

and then you can do myObj->insert()
Although, this seems a bit troublesome.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity .net/wordpress/>
Jun 27 '08 #2
barcaroller wrote:
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 inherit
from (e.g. STL).
Example:

class A
{
public:
foo();

private:
set<stringmyset ;
}

A myObj;
myObj.insert(); // compiler error of course
Is there some mechanism (direct or indirect) where a function that is
not handled by myObj gets delegated to another object (e.g. myset)?
No, C++ does not support this form of delegation.

--
Ian Collins.
Jun 27 '08 #3
On 24 mai, 00:43, Ian Collins <ian-n...@hotmail.co mwrote:
barcaroller wrote:
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 inherit
from (e.g. STL).
Example:
class A
{
public:
foo();
private:
set<stringmyset ;
}
A myObj;
myObj.insert(); // compiler error of course
Is there some mechanism (direct or indirect) where a function that is
not handled by myObj gets delegated to another object (e.g. myset)?
No, C++ does not support this form of delegation.
Not directly. The closest you can come, I think, is to use
private inheritance and using declarations.

Note that this type of delegation is effectively exposing part
of your internals, to some degree. Although significantly
wordier, I rather favor being explicit in forwarding, so that
the complete interface of the object isn't available. Most of
the time, at least; I also have at least one case where the
non-mutable interface of the object is exactly that of
std::vector< std::string and I can conceive of others. Which
means that I do have to duplicate a lot (including things like
typedef's). But it's not 100% duplication either; I have
iterator typedefed to std::vector<std ::string>::cons t_iterator,
for example.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #4
On 2008-05-23 23:58, barcaroller wrote:
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 inherit
from (e.g. STL).
Example:

class A
{
public:
foo();

private:
set<stringmyset ;
}

A myObj;
myObj.insert(); // compiler error of course
Is there some mechanism (direct or indirect) where a function that is
not handled by myObj gets delegated to another object (e.g. myset)?
Private inheritance is one way to do it:

#include <iostream>

class Foo
{
public:
void print() { std::cout << "Foo\n"; }
};

class Bar : private Foo
{
public:
using Foo::print;
};

int main()
{
Bar b;
b.print();
}

But most often I would recommend to manually do the delegation:

#include <iostream>

class Foo
{
public:
void print() { std::cout << "Foo\n"; }
};

class Bar
{
Foo f;
public:
void print() { f.print(); }
};

int main()
{
Bar b;
b.print();
}

--
Erik Wikström
Jun 27 '08 #5
James Kanze wrote:
On 24 mai, 00:43, Ian Collins <ian-n...@hotmail.co mwrote:
>barcaroller wrote:
>>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 inherit
from (e.g. STL).
>>Example:
>> class A
{
public:
foo();
private:
set<stringmyset ;
}
>> A myObj;
myObj.insert(); // compiler error of course
>>Is there some mechanism (direct or indirect) where a function that is
not handled by myObj gets delegated to another object (e.g. myset)?
>No, C++ does not support this form of delegation.

Not directly. The closest you can come, I think, is to use
private inheritance and using declarations.
I was going to suggest that technique, but decided against it as the OP
wanted to delegate members of std::set. I wouldn't recommend deriving
from a standard container.

--
Ian Collins.
Jun 27 '08 #6
On May 24, 9:47 am, Ian Collins <ian-n...@hotmail.co mwrote:

[...]
I was going to suggest that technique, but decided against it as the OP
wanted to delegate members of std::set. I wouldn't recommend deriving
from a standard container.
Not even privately? I have no problems with private inheritance
from a standard container; private inheritance is part of the
implementation.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #7
James Kanze wrote:
On May 24, 9:47 am, Ian Collins <ian-n...@hotmail.co mwrote:

[...]
>I was going to suggest that technique, but decided against it as the OP
wanted to delegate members of std::set. I wouldn't recommend deriving
from a standard container.

Not even privately? I have no problems with private inheritance
from a standard container; private inheritance is part of the
implementation.
Yes, you're right, I overlooked private inheritance.

--
Ian Collins.
Jun 27 '08 #8
On May 24, 5:19*pm, Ian Collins <ian-n...@hotmail.co mwrote:
James Kanze wrote:
On May 24, 9:47 am, Ian Collins <ian-n...@hotmail.co mwrote:
* * [...]
I was going to suggest that technique, but decided against it as the OP
wanted to delegate members of std::set. *I wouldn't recommend deriving
from a standard container.
Not even privately? *I have no problems with private inheritance
from a standard container; private inheritance is part of the
implementation.

Yes, you're right, I overlooked private inheritance.
Private inheritance is not suggested on standard container either.
Because the standard containers are not designed for inheritance at
all.
Just think about the polymorphism and virtual destruction , then
you'll
get the conclusion that inheritance from standard containors will be
dangerous.
Jun 27 '08 #9
tedzhou wrote:
On May 24, 5:19 pm, Ian Collins <ian-n...@hotmail.co mwrote:
>James Kanze wrote:
>>On May 24, 9:47 am, Ian Collins <ian-n...@hotmail.co mwrote:
[...]
I was going to suggest that technique, but decided against it as the OP
wanted to delegate members of std::set. I wouldn't recommend deriving
from a standard container.
Not even privately? I have no problems with private inheritance
from a standard container; private inheritance is part of the
implementatio n.
Yes, you're right, I overlooked private inheritance.

Private inheritance is not suggested on standard container either.
Because the standard containers are not designed for inheritance at
all.
Just think about the polymorphism and virtual destruction , then
you'll
get the conclusion that inheritance from standard containors will be
dangerous.
With public inheritance maybe, but you can't point a base* to a derived
object if derived uses private inheritance. Private inheritance hides
the fact that a derived is a base. Try

class base {};
class derived : base {};

base* p = new derived;

--
Ian Collins.
Jun 27 '08 #10

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

Similar topics

7
3279
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 all written data to uppercase: class UpperOut: def __init__(self, outfile): self.__outfile = outfile def write(self, s):
6
4430
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 problem. Is there any potential problems with it? class E { public: void Draw_E(int a, int b) { cout << "Draw in E " << a*b<< endl; }
3
2424
by: Tony Johansson | last post by:
Hello! What does it mean with delegation and can you give me one example. //Tony
0
997
by: Preston Park | last post by:
We are trying to get windows authentication to work with Reporting Services and Analysis Services in a way that may be unsupported. Setup: There are two domains: A and B. There are two servers in the A domain: A\R and A\S Server R is running Reporting Services to serve OLAP reports from data on
2
2435
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 impersonation and delegation to forward the user's Windows login through all layers in the stack. To support this, I'm setting up a set of domain accounts which we use to create SPNs for the various services in the various layers. At this point, I'm...
4
2020
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. Computer Trusted for Delegation. Integrated Windows Authentication selected. Medium pooled. Not the default website - using IP address to connect from client. IWAN_<computernamelocal account is running as part of operating system and trusted for...
6
2842
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 web server the command succeeds. However, if we use the servername or ip through IIS it fails. For this reason we know we have permissions setup correctly on the file server. Can anyone identify what we could possibly be doing wrong here: ...
3
3496
by: Patrick | last post by:
Hello I have the following scenario - SQL 2005 server (serversql) - Windows 2003 with IIS (serveriis) - Windows 2003 ADS (serverads) I want to connect to an intranet application using NTML with impersonation and delegation. so for this I made the following change in web.config <identity impersonate="true"/>
5
1459
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, how can I define delegation? -- Thanks, Mayer
0
9533
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10190
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7555
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4122
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.