473,771 Members | 2,297 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::metho d(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::meth od(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 3370
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::metho d(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::meth od(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
    2446
    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 far is pretty simple: using an idea of Andrei Alexandrescu (at least that's where I first read it) to create a lightweight way of overloading member functions since member function specialization is not possible.
    1
    2157
    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 definition. If you insted have declared a primitive type or a class type then I would understand it completely. I assume that having a friend declaration in the private section make no sense. I assume that you always put friend declaration in the...
    43
    2847
    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 (calling tree) browsing that is supported in C++. I don't know how some could write a large scale object-oriented application w/o those. If you have overcome these limitations in C#, please share your thoughts and ideas. Thanks!
    3
    3788
    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 be able define sub-object to return to consumers but wants to really clamp down on how much is exposed to the consumer - for example, I wanted to make consumers unable to instantiate the sub-objects on their own. Originally, i'd hoped...
    8
    1484
    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) IParser, one for XML, ne for Config, & one for binary. They need to get the Document & CC class and fill the data, in the
    5
    5312
    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 **************************************************************** int main(){
    5
    2163
    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
    2723
    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 public data members. Don't use friends, except to avoid or . Consider the following scenarios: Scenario 1:
    2
    2431
    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 implmention. The problem is that non-member function cannot access private data member. The friend keyword is not the solution. I am aware that friend keyword allows the non-member function to access private data member. If you define more than...
    0
    9619
    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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
    0
    10102
    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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
    1
    10038
    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,...
    0
    9910
    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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
    0
    8933
    agi2029
    by: agi2029 | last post by:
    Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
    1
    7460
    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
    6712
    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();...
    1
    4007
    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
    3609
    muto222
    by: muto222 | last post by:
    How can i add a mobile payment intergratation into php mysql website.

    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.