473,394 Members | 1,739 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,394 software developers and data experts.

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 1434
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&operator->() { 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.comwrote:
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>::const_iterator,
for example.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
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.comwrote:
>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.comwrote:

[...]
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 objektorientierter Datenverarbeitung
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.comwrote:

[...]
>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.comwrote:
James Kanze wrote:
On May 24, 9:47 am, Ian Collins <ian-n...@hotmail.comwrote:
* * [...]
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.comwrote:
>James Kanze wrote:
>>On May 24, 9:47 am, Ian Collins <ian-n...@hotmail.comwrote:
[...]
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.
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
tedzhou <zh*******@163.comwrote:
On May 24, 5:19*pm, Ian Collins <ian-n...@hotmail.comwrote:
James Kanze wrote:
On May 24, 9:47 am, Ian Collins <ian-n...@hotmail.comwrote:
* * [...]
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.
Polymorphism and virtual destruction don't apply to private
inheritance.
Jun 27 '08 #11
tedzhou wrote:
On May 24, 5:19 pm, Ian Collins <ian-n...@hotmail.comwrote:
>James Kanze wrote:
>>On May 24, 9:47 am, Ian Collins <ian-n...@hotmail.comwrote:
[...]
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.
Not necessarily. Several of the standard adapter classes
(std::priority_queue for example) have protected members, which implies
that they were designed to be inherited from.
Jun 27 '08 #12
tedzhou wrote:
On May 24, 5:19*pm, Ian Collins <ian-n...@hotmail.comwrote:
>James Kanze wrote:
On May 24, 9:47 am, Ian Collins <ian-n...@hotmail.comwrote:
[...]
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.
That argument is bogus for two reasons:

(a) As others have pointed out, private differs from public inheritance with
regard to destruction.

(b) Even with public inheritance, the argument does not hold as it would
apply equally well to std::unary_function or std::iterator, two classes
that are meant to be publicly inherited from and that lack virtual
destructors. Inheritance in C++ is not necessarily tied to polymorphism.

[Note: that you failed to point out a valid reason as to why public
inheritance from standard containers might not be such a good move, is not
to say that there are no such reasons. However, those tend to focus on
other issues rather than the lack of a virtual destructor.]
Best

Kai-Uwe Bux
Jun 27 '08 #13
On May 26, 11:00 am, Ian Collins <ian-n...@hotmail.comwrote:
tedzhou wrote:
On May 24, 5:19 pm, Ian Collins <ian-n...@hotmail.comwrote:
James Kanze wrote:
On May 24, 9:47 am, Ian Collins <ian-n...@hotmail.comwrote:
[...]
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.
With public inheritance maybe, but you can't point a base* to
a derived object if derived uses private inheritance.
You can, and in fact, one frequent use of private inheritance
involves doing just that. But you need cooperation from the
derived class to do so: if the derived class doesn't give you
the Base*, you can't get it otherwise. (The "frequent use" is
to derive privately from something like "EventHander": for
normal client code, this is an implementation detail, and they
can't consider the class to be an EventHander. But the class
itself may register itself as an EventHander with some
EventNotifier, who will address the class through an
EventHander*.)
Private inheritance hides the fact that a derived is a base.
Except in the explicit cases where it wants to behave as a base.

None of which is really a problem when deriving privately from a
standard container, since you, the author of the class, have
total control, and of course, you will not expose the
derivation.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #14

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...
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
0
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...
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...
3
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...
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.