how to realize a friend interface?

  • Thread starter Davide M3xican Coppola
  • Start date
D

Davide M3xican Coppola

Hi, I would realize an interface (interface) for a pair of concrete
classes (concrete & concrete2), but I have a problem because I have to
access to a third class (element) and if I try to declare interface
class as friend, I obtain this (right) error:

[err]
main2.cpp: In member function `virtual void
concrete::method(element&)':
main2.cpp:8: error: `int element::a_int' is protected
main2.cpp:31: error: within this context
main2.cpp: In member function `virtual void
concrete2::method(element&)':
main2.cpp:8: error: `int element::a_int' is protected
main2.cpp:39: error: within this context
[/err]

So the question is, there is a way to realize this?
I don't would use the interface as a wrapper, nor solve the problem
with a design pattern, any ideas?

This is the example code:

Code:
#include <iostream>

using namespace std;

class element
{
protected:
int a_int;
public:
element(int a) { a_int = a; };
~element();

//  friend class interface; // --> I would declare just this line!
friend class concrete;
friend class concrete2;
};

class interface
{
public:
interface() {};
~interface() {};
virtual void method(element & e) = 0;
};

class concrete : public interface
{
public:
concrete() {};
~concrete() {};
void method(element & e) { cout << "[1] " << e.a_int <<
endl;};
};

class concrete2 : public interface
{
public:
concrete2() {};
~concrete2() {};
void method(element & e) { cout << "[2] " << e.a_int <<
endl;};
};


int main()
{
element * e = new element(10);
interface * i;
concrete * c = new concrete();
concrete2 * c2 = new concrete2();

i = c;
i->method(*e);

i = c2;
i->method(*e);

return 0;
}

Thanx in advance to all.
 
R

Rolf Magnus

Davide said:
Hi, I would realize an interface (interface) for a pair of concrete
classes (concrete & concrete2), but I have a problem because I have to
access to a third class (element) and if I try to declare interface
class as friend, I obtain this (right) error:

[err]
main2.cpp: In member function `virtual void
concrete::method(element&)':
main2.cpp:8: error: `int element::a_int' is protected
main2.cpp:31: error: within this context
main2.cpp: In member function `virtual void
concrete2::method(element&)':
main2.cpp:8: error: `int element::a_int' is protected
main2.cpp:39: error: within this context
[/err]


friendship is not inheritable.
So the question is, there is a way to realize this?

You could put a protected member function in the interface class that does
the actual access.
I don't would use the interface as a wrapper, nor solve the problem
with a design pattern, any ideas?

Define "design pattern". I guess my solution with that access function in
the interface can be considered a design pattern, and it probably already
has a name.
This is the example code:

Code:
#include <iostream>

using namespace std;

class element
{
protected:
int a_int;
public:
element(int a) { a_int = a; };
~element();

//  friend class interface; // --> I would declare just this line!
friend class concrete;
friend class concrete2;
};

class interface
{
public:
interface() {};[/QUOTE]

No need to define an empty constructor and clutter up the code with it. The
compiler generates one automatically for you.
[QUOTE]
~interface() {};[/QUOTE]

Shouldn't this be virtual?
[QUOTE]
virtual void method(element & e) = 0;
};

class concrete : public interface
{
public:
concrete() {};
~concrete() {};
void method(element & e) { cout << "[1] " << e.a_int <<
endl;};
};

class concrete2 : public interface
{
public:
concrete2() {};
~concrete2() {};
void method(element & e) { cout << "[2] " << e.a_int <<
endl;};
};


int main()
{
element * e = new element(10);
interface * i;
concrete * c = new concrete();
concrete2 * c2 = new concrete2();

i = c;
i->method(*e);

i = c2;
i->method(*e);

return 0;
}

Thanx in advance to all.
 
D

Davide M3xican Coppola

Rolf Magnus said:
friendship is not inheritable.
Yes, I see, I'll email Stroustrup to report the problem :)

Ok, coming back serious...
You could put a protected member function in the interface class that does
the actual access.

Hmm.. I don't understand :(
Could you explain this using an example?

However keep in mind that interface should be a pure virtual class, no
implementation in it.

Define "design pattern". I guess my solution with that access function in
the interface can be considered a design pattern, and it probably already
has a name.

I don't have an exact design pattern in mind, now I'm thinking about
Adapter, but now I don't now yet if this is the right solution.
No need to define an empty constructor and clutter up the code with it. The
compiler generates one automatically for you.

This is a my habit, then in a second stage of develop I'll remove the
brackes if the method doesn't require an implementation.
Shouldn't this be virtual?

Yes, this is just an oversight.

Thanx for the help and for the patience with me.
 
D

Davide M3xican Coppola

I don't have an exact design pattern in mind, now I'm thinking about
Adapter, but now I don't now yet if this is the right solution.

A possible solution could be also the Strategy dp, but there is the
problem of lack of friend inheritance...
 

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,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top