Reputation: 227
I have some C++ code interfacing with 'X' code (as in X11). For one of the calls it wants to have a param be the address of a pointer to a string. The call is setup to take a counted array of strings. In this case, it's only taking 1 string (a winname derived from the hostname). What is currently in place is something along the lines of:
char winname_[257]; // to hold Cstr returned by gethostname()
string windowName{}; // holding string version (prefer strings, but need
// Cstr's for C-compat in places)
Have functions:
string XWin::WindowName(string name) {
if (name.size()) windowName = name;
if (!windowName.size()) {
gethostname(winname_, sizeof(winname_)-1);
windowName = getResourceOrUseDefault(string("title"), string(winname_));
strcpy(winname_, windowName.c_str());
}
return windowName;
}
inline char * winname() {if (!winname[0]) WindowName();return winname;}
To pass to the Xfunc, I need:
char * winname_p = winname_;
As the X function takes:
Xfunc(&winname_p, 1, et, al.)
What I'd like to do is be able to encapsulate the "winname_p" reference in a function, so I can check that winname_p[0] != 0
, and have the function return a pointer, but so far, I am flailing in having a function return
an lvalue so I can have something like:
Xfunc(&winname_ptr(),1, et,al.)...
I haven't figured out the return syntax for the function so that it returns an lvalue to winname_p. I'm sure this is simple for many, but I seem to have some blocks when it comes to pointers & functions.
Upvotes: 0
Views: 69
Reputation: 227
Like many things, this is easy once you know the answer. Initialize the pointer as a const (to indicate it shouldn't be changed):
char * const winname_p{winname_};
Then the wrapper:
char * const &XWin::winname_ptr(void) {
if (!winname_[0]) WindowName();
return winname_p;
}
And the usage:
if (XStringListToTextProperty(
const_cast<char **>(&winname_ptr()), 1, &title_) == 0) {...}
Would have been simpler to not use const, but it seems more accurate this way.
Upvotes: 1