-1

I am using wgetch function from curses.h and want to call e.g wgetch(handle) where handle is private member of my class. Is there any way to do it without defining new friend function of my class (like below) or maybe making it method somehow?

class foo {
    WINDOW *handle;
public:
    friend int wgetch(foo &t) { wgetch(t.handle); };
}
Mat3o
  • 5
  • 1
  • 4
  • Is `GUIkonsola` related to `class foo` somehow? Seems like a member is exactly what you want - perhaps `int wgetch() const { return ::wgetch(handle); }` or similar? – Toby Speight Jun 05 '18 at 14:49
  • Sorry, but i missed that, and in this example should be `foo` there, not `GUIkonsola`. And I'm trying to avoid declaring new functions, cuz there are many of this i just using same way - using `handle` member. – Mat3o Jun 06 '18 at 05:55

2 Answers2

1

Access to private data is restricted to the class implementation and friends (use friends only when necessary). So, no, as long as handle is private, there are no options for accessing it other than friends and members.

That being said, the access does not necessarily have to be in the function you are trying to write. If there is a real reason for not defining a wgetch member of your class, maybe you could define a member that returns the value of handle (read-only public access). This seems less convenient for the users of your class though.

JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • I was so tired finding solution about that when was writing this question and missed that not changed class name in above example (now it is edited and correct). "maybe you could define a member that returns the value of handle (read-only public access)" yeah, im doing it just right now, but it's "less convenient for the users" and searching a solution to figure it out in more comfortable way. – Mat3o Jun 06 '18 at 05:49
0

Given that handle is private, then the only access to it is from your class's members and its friends.

The code you have (which passes an instance of foo to a friend function) is convoluted and unconventional compared to simply having a member function:

class foo {
    WINDOW *handle;
public:
    int wgetch() { return ::wgetch(handle); }
};

It appears that you're writing a C++ wrapper for a Curses WINDOW*, so many small forwarding members would appear to be the natural approach. Note that we need the scoping operator :: to disambiguate the wgetch that we intend to call.

You probably ought to be aware that NCurses does include its own C++ wrappers. Although these are undocumented, we see that the definition of NCursesWindow::getch() looks exactly like the method above (see cursesw.h, line 953):

  int            getch() { return ::wgetch(w); }

You might save yourself a lot of work by using these classes.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
  • Here's a [useful link](https://xaizek.github.io/2016-04-24/ncurses-for-cpp/), describing the rough edges of the C++ bindings, and how to use them effectively. – Toby Speight Jun 06 '18 at 07:17