473,394 Members | 1,797 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.

how to realize a friend interface?

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:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class element
  6. {
  7. protected:
  8. int a_int;
  9. public:
  10. element(int a) { a_int = a; };
  11. ~element();
  12.  
  13. //  friend class interface; // --> I would declare just this line!
  14. friend class concrete;
  15. friend class concrete2;
  16. };
  17.  
  18. class interface
  19. {
  20. public:
  21. interface() {};
  22. ~interface() {};
  23. virtual void method(element & e) = 0;
  24. };
  25.  
  26. class concrete : public interface
  27. {
  28. public:
  29. concrete() {};
  30. ~concrete() {};
  31. void method(element & e) { cout << "[1] " << e.a_int <<
  32. endl;};
  33. };
  34.  
  35. class concrete2 : public interface
  36. {
  37. public:
  38. concrete2() {};
  39. ~concrete2() {};
  40. void method(element & e) { cout << "[2] " << e.a_int <<
  41. endl;};
  42. };
  43.  
  44.  
  45. int main()
  46. {
  47. element * e = new element(10);
  48. interface * i;
  49. concrete * c = new concrete();
  50. concrete2 * c2 = new concrete2();
  51.  
  52. i = c;
  53. i->method(*e);
  54.  
  55. i = c2;
  56. i->method(*e);
  57.  
  58. return 0;
  59. }
  60.  
Thanx in advance to all.
Jul 23 '05 #1
3 3343
Davide M3xican Coppola wrote:
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:

Expand|Select|Wrap|Line Numbers
  1.  #include <iostream>
  2.  using namespace std;
  3.  class element
  4.  {
  5.      protected:
  6.          int a_int;
  7.      public:
  8.          element(int a) { a_int = a; };
  9.          ~element();
  10.      //  friend class interface; // --> I would declare just this line!
  11.          friend class concrete;
  12.          friend class concrete2;
  13.  };
  14.  class interface
  15.  {
  16.      public:
  17.          interface() {};
  •  
  • No need to define an empty constructor and clutter up the code with it. The
  • compiler generates one automatically for you.
  •          ~interface() {};
  •  
  • Shouldn't this be virtual?
  •          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.

    Jul 23 '05 #2
    Rolf Magnus <ra******@t-online.de> wrote in message news:<d4*************@news.t-online.com>...

    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.

    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.


    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.
    interface() {};


    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.
    ~interface() {};


    Shouldn't this be virtual?


    Yes, this is just an oversight.

    Thanx for the help and for the patience with me.
    Jul 23 '05 #3
    > 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...
    Jul 23 '05 #4

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

    Similar topics

    4
    by: Justin Miller | last post by:
    Ok, I tried to make that subject as descriptive as possible. What I'm trying to do: I'm attempting to use policies to create a generic memento (design pattern) template. My Memento template so...
    1
    by: Tony Johansson | last post by:
    Hello experts! Assume I have this class definition of class ListElem. I'm a bit unsure how to interpret when you put friend declaration in public, protected and private section of a class...
    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...
    3
    by: Mark | last post by:
    Hi... Maybe I'm just being especially obtuse today, but I've been perplexed by how some of the protection levels have been working with nested classes in C#. I have a parent class that wants to...
    8
    by: toton | last post by:
    Hi, I have a parser interface , which is class IParser{ public: void readHeader(Document& doc)= 0; void readCC(CC& cc) = 0; }; Now, there are 3 kinds of parser which extends (implements)...
    5
    by: Gary Wessle | last post by:
    Hi I am trying to call a function of a class which declares an object of another class and return its private member. thanks ...
    5
    by: Amit Gupta | last post by:
    How do I do it (either by friend or by any other hack). I have three class, I am just writing derivation below: class A{} class B{} class C : public class B{} class D: public class B ()
    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...
    2
    by: Immortal Nephi | last post by:
    I design a class for general purpose. I do not allow a client to read or modify interface and implemention. I allow them to write a new non- member function outside of class' interface and...
    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
    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
    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.