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

using friend to enhance encapsulation?

Hi,

Consider the example below. A Device controls a number of Ports. I'd
like the Device to be in charge of creating/deleting Ports. I enforce
this by making the Ports constructor and destructor private and making
Device a friend of Port. In Device's destructor I'd like to use a
generic purge function to delete all Ports. Ofcourse that doesn't work
because the Port destructor is private for Purge.
So, where did I go wrong? I went for the private ctor/dtor + friend
approach to keep things as encapsulated as possible. I can make Port's
ctor/dtor public, but ideally I don't want clients to be able to create
Ports. Alternatively, I could duplicate purge's functionality inside the
Device destructor, but that's not my favourite kind of code reuse. How
do you fix this design?

Ruben.

#include <vector>
#include <iostream>

using namespace std;

/*From Bruce Eckel's Thinking in C++ Vol.2.
*Belongs in a separate purge.h file*/
template<class Seq> void purge(Seq& c) {
typename Seq::iterator i;
for(i = c.begin(); i != c.end(); ++i) {
delete *i;
*i = 0;
}
}

class Device {
public:
class Port {
public:
void doSomething() {
cout << "port " << id_ << " doing something." << endl;
};
private:
int id_;
Device& device_;

Port(int id, Device &device) : id_(id), device_(device) {}

~Port() {
cout << "destructing port" << endl;
}

friend class Device;
};

Device(int numPorts) {
for (int i=0; i<numPorts; i++) {
ports_.push_back(new Port(i, *this));
}
}

~Device() {
purge(ports_);
}

Port* getPort(int i) {
return ports_.at(i);
}
private:
vector<Port*> ports_;
};

int main() {
Device device(10);
return 0;
}
Jul 22 '05 #1
2 1592
"Ruben Lysens" <ru**********@pandora.be> wrote in message
news:xs*********************@phobos.telenet-ops.be...
Consider the example below. A Device controls a number of Ports. I'd like
the Device to be in charge of creating/deleting Ports. I enforce this by
making the Ports constructor and destructor private and making Device a
friend of Port. In Device's destructor I'd like to use a generic purge
function to delete all Ports. Ofcourse that doesn't work because the Port
destructor is private for Purge.
So, where did I go wrong? I went for the private ctor/dtor + friend
approach to keep things as encapsulated as possible. I can make Port's
ctor/dtor public, but ideally I don't want clients to be able to create
Ports. Alternatively, I could duplicate purge's functionality inside the
Device destructor, but that's not my favourite kind of code reuse. How do
you fix this design?

Ruben.

#include <vector>
#include <iostream>

using namespace std;

/*From Bruce Eckel's Thinking in C++ Vol.2.
*Belongs in a separate purge.h file*/
template<class Seq> void purge(Seq& c) {
typename Seq::iterator i;
for(i = c.begin(); i != c.end(); ++i) {
delete *i;
*i = 0;
}
}

class Device {
public:
class Port {
public:
void doSomething() {
cout << "port " << id_ << " doing something." << endl;
};
private:
int id_;
Device& device_;

Port(int id, Device &device) : id_(id), device_(device) {}

~Port() {
cout << "destructing port" << endl;
}

friend class Device; What about adding:
friend void purge(vector<Port*>&);
Not clean in terms of interdependencies, but safe.
};

Device(int numPorts) {
for (int i=0; i<numPorts; i++) {
ports_.push_back(new Port(i, *this)); NB: This will leak memory if the creation of a >0th port
fails. You probably should use some kind of smart pointer,
or handle the exception to free previously created entries. }
}

~Device() {
purge(ports_);
}

Port* getPort(int i) { NB: I'd rather return a reference than a pointer here,
unless the stored pointer is actually allows to be null. return ports_.at(i);
}
private:
vector<Port*> ports_;
};

int main() {
Device device(10);
return 0;
}


Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 22 '05 #2
Ivan Vecerina wrote:
"Ruben Lysens" <ru**********@pandora.be> wrote in message
news:xs*********************@phobos.telenet-ops.be...
Consider the example below. A Device controls a number of Ports. I'd like
the Device to be in charge of creating/deleting Ports. I enforce this by
making the Ports constructor and destructor private and making Device a
friend of Port. In Device's destructor I'd like to use a generic purge
function to delete all Ports. Ofcourse that doesn't work because the Port
destructor is private for Purge.
So, where did I go wrong? I went for the private ctor/dtor + friend
approach to keep things as encapsulated as possible. I can make Port's
ctor/dtor public, but ideally I don't want clients to be able to create
Ports. Alternatively, I could duplicate purge's functionality inside the
Device destructor, but that's not my favourite kind of code reuse. How do
you fix this design?

Ruben.

#include <vector>
#include <iostream>

using namespace std;

/*From Bruce Eckel's Thinking in C++ Vol.2.
*Belongs in a separate purge.h file*/
template<class Seq> void purge(Seq& c) {
typename Seq::iterator i;
for(i = c.begin(); i != c.end(); ++i) {
delete *i;
*i = 0;
}
}

class Device {
public:
class Port {
public:
void doSomething() {
cout << "port " << id_ << " doing something." << endl;
};
private:
int id_;
Device& device_;

Port(int id, Device &device) : id_(id), device_(device) {}

~Port() {
cout << "destructing port" << endl;
}

friend class Device;
What about adding:
friend void purge(vector<Port*>&);
Not clean in terms of interdependencies, but safe.


This would allow users to delete Port objects by entering Port object
pointers into a vector and feeding that to purge. Then I prefer
duplicating the purge function as a member function over this solution.
Either way, the solution is ugly. So I suspect the design's no good,
maybe because I'm going about encapsulation and this friend feature the
wrong way. I don't have a lot of experience as a C++ programmer.

R.
};

Device(int numPorts) {
for (int i=0; i<numPorts; i++) {
ports_.push_back(new Port(i, *this));


NB: This will leak memory if the creation of a >0th port
fails. You probably should use some kind of smart pointer,
or handle the exception to free previously created entries.
}
}

~Device() {
purge(ports_);
}

Port* getPort(int i) {


NB: I'd rather return a reference than a pointer here,
unless the stored pointer is actually allows to be null.
return ports_.at(i);
}
private:
vector<Port*> ports_;
};

int main() {
Device device(10);
return 0;
}

Regards,
Ivan

Jul 22 '05 #3

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

Similar topics

11
by: Christopher Benson-Manica | last post by:
#include <vector> class Bar; class Foo { friend class Bar; /* 1 */ protected: int myint;
43
by: Zeng | last post by:
It's so messy w/o the "friend" relationship. Does anyone know why it was not supported in C#. It's almost about as bad as it doesn't support the inheritance hierarchy and method reference...
7
by: Jesper | last post by:
I need to grant a class access to protected fields of another class in the way its possible in C++ with the friend keyword. However I would like to keep the class protected towards other class...
3
by: hartley_aaron | last post by:
Hi, What does it mean when VS automatically adds a line like this to code of my form: Friend WithEvents Label1 As System.Windows.Forms.Label Also, I noticed a modifier property for each...
4
by: Madhav | last post by:
Hi all, I am a newbie in c++. I want to know what is the philosophical reason behind the existence of friend functions. I thought giving access to private data to a function which is not a member...
2
by: Marco Spatz | last post by:
Hi, we moved our build platform to the "next" generation ;) (VS 2005 and gcc 4.0) and our code has build (with some minor changes) so far. But now I met a strange problem with the friend keyword...
19
by: subramanian100in | last post by:
Stroustrup, in his book TC++PL 3rd Edition, in page 16, under the section '1.8 Advice' has mentioned the following: Don't use global data(use members) Don't use global functions Don't use...
3
by: Rahul | last post by:
Hi Everyone, The following code works fine, class A { private: friend void sample(A& obj) { printf("in friend...\n"); printf("%d",obj.i);
4
by: Mike Peretz | last post by:
I am wondering if anyone out there is trying to use friend with C#, simular to the way C++ handles it. I wrote a blog about it and I wonder if someone can give me some feedback. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.