C++ And XtGetSubresources()

J

james.p.williams

I'm using Young's book on mixing C++ and Motif. In it, he says to use
XtGetSubresources() to be able to configure member data using the X
resource database. When I try this, based on Young's example,

class MyComponent: public UIComponent
{
...
private:
unsigned char _orientation
static XtResource _customResources[];
...
}

XtResource MyComponent::_customResources[]={
{
"orientation",
"Orientation",
XmROrientation,
sizeof(unsigned char),
XtOffsetOf(MyComponent,_orientation),
XmRString,
(XtPointer)"HORIZONTAL"
},{
...
}
};

I get compiler warnings about the above call to the XtOffsetOf()
macro.

% g++ -Wall -Wunused -W -ansi -pedantic -c MyComponent.C
MyComponent.C:116: warning: invalid offsetof from non-POD type
`class MyComponent'; use pointer to member instead

I understand the pointer to member syntax, but am not sure how to use
it here to appease the compiler. Is there a correct, portable, safe
way to do this?

Thanks,

Jim Williams
 
W

WW

I'm using Young's book on mixing C++ and Motif. In it, he says to use
XtGetSubresources() to be able to configure member data using the X
resource database. When I try this, based on Young's example, [SNIP]
I get compiler warnings about the above call to the XtOffsetOf()
macro.

% g++ -Wall -Wunused -W -ansi -pedantic -c MyComponent.C
MyComponent.C:116: warning: invalid offsetof from non-POD type
`class MyComponent'; use pointer to member instead

I understand the pointer to member syntax, but am not sure how to use
it here to appease the compiler. Is there a correct, portable, safe
way to do this?

You cannot use offsetof: since you have a class which is not a POD. You
cannot use member pointers either, since you have a C interface, which has
no idea about member pointers. The best I could come up with is to create a
POD type and embed that one into your class. Register that thing using the
Motif functions.
 
S

Simon Saunders

I'm using Young's book on mixing C++ and Motif. In it, he says to use
XtGetSubresources() to be able to configure member data using the X
resource database. When I try this, based on Young's example,

class MyComponent: public UIComponent {
...
private:
unsigned char _orientation
static XtResource _customResources[]; ...
}
}
XtResource MyComponent::_customResources[]={
{
"orientation",
"Orientation",
XmROrientation,
sizeof(unsigned char),
XtOffsetOf(MyComponent,_orientation), XmRString,
(XtPointer)"HORIZONTAL"
},{
...
}
};

I get compiler warnings about the above call to the XtOffsetOf() macro.

% g++ -Wall -Wunused -W -ansi -pedantic -c MyComponent.C
MyComponent.C:116: warning: invalid offsetof from non-POD type `class
MyComponent'; use pointer to member instead

I understand the pointer to member syntax, but am not sure how to use it
here to appease the compiler. Is there a correct, portable, safe way to
do this?
class MyComponent : public UIComponent {

private:
struct Resources
{
unsigned char orientation_;
};
Resources res_;
static XtResource customResources_[];
};

XtResource MyComponent::customResources_[]= {
{
"orientation",
"Orientation",
XmROrientation,
sizeof(unsigned char),
XtOffsetOf(MyComponent::Resources, orientation_), XmRString,
(XtPointer)"HORIZONTAL"
}
};

(By the way, I prefer to use trailing underscores as it avoids potential
conflicts with identifiers reserved for use by implementors.)
 
G

Gianni Mariani

I'm using Young's book on mixing C++ and Motif. In it, he says to use
XtGetSubresources() to be able to configure member data using the X
resource database. When I try this, based on Young's example,

class MyComponent: public UIComponent
{
...
private:
unsigned char _orientation
static XtResource _customResources[];
...
}

XtResource MyComponent::_customResources[]={
{
"orientation",
"Orientation",
XmROrientation,
sizeof(unsigned char),
XtOffsetOf(MyComponent,_orientation),
XmRString,
(XtPointer)"HORIZONTAL"
},{
...
}
};

I get compiler warnings about the above call to the XtOffsetOf()
macro.

% g++ -Wall -Wunused -W -ansi -pedantic -c MyComponent.C
MyComponent.C:116: warning: invalid offsetof from non-POD type
`class MyComponent'; use pointer to member instead

I understand the pointer to member syntax, but am not sure how to use
it here to appease the compiler. Is there a correct, portable, safe
way to do this?

Yes ... and no.

Yes in the sense that C++ supports "pointer to member" types but No in
the sense that to do what Doug is doing here it will still be somewhat
non-portable.

I would find a way to silence the warning and live with it. A possible
way is to re-interpret cast a "pointer to member". If I'm not mistaken,
XtResource is defined in X Toolkit and you would have a helluva time
changing all the code that depends on that.
 

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
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top