C++ And XtGetSubresources()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • james.p.williams@usa-spaceops.com

    C++ And XtGetSubresources()

    I'm using Young's book on mixing C++ and Motif. In it, he says to use
    XtGetSubresourc es() 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 _customResource s[];
    ...
    }

    XtResource MyComponent::_c ustomResources[]={
    {
    "orientatio n",
    "Orientatio n",
    XmROrientation,
    sizeof(unsigned char),
    XtOffsetOf(MyCo mponent,_orient ation),
    XmRString,
    (XtPointer)"HOR IZONTAL"
    },{
    ...
    }
    };

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

    % g++ -Wall -Wunused -W -ansi -pedantic -c MyComponent.C
    MyComponent.C:1 16: 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
  • WW

    #2
    Re: C++ And XtGetSubresourc es()

    james.p.william s@usa-spaceops.com wrote:[color=blue]
    > I'm using Young's book on mixing C++ and Motif. In it, he says to use
    > XtGetSubresourc es() to be able to configure member data using the X
    > resource database. When I try this, based on Young's example,[/color]
    [SNIP][color=blue]
    > I get compiler warnings about the above call to the XtOffsetOf()
    > macro.
    >
    > % g++ -Wall -Wunused -W -ansi -pedantic -c MyComponent.C
    > MyComponent.C:1 16: 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?[/color]

    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.

    --
    WW aka Attila


    Comment

    • Simon Saunders

      #3
      Re: C++ And XtGetSubresourc es()

      On Mon, 22 Sep 2003 08:44:43 -0700, james.p.william wrote:
      [color=blue]
      > I'm using Young's book on mixing C++ and Motif. In it, he says to use
      > XtGetSubresourc es() 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 _customResource s[]; ...
      > }
      > }
      > XtResource MyComponent::_c ustomResources[]={
      > {
      > "orientatio n",
      > "Orientatio n",
      > XmROrientation,
      > sizeof(unsigned char),
      > XtOffsetOf(MyCo mponent,_orient ation), XmRString,
      > (XtPointer)"HOR IZONTAL"
      > },{
      > ...
      > }
      > };
      >
      > I get compiler warnings about the above call to the XtOffsetOf() macro.
      >
      > % g++ -Wall -Wunused -W -ansi -pedantic -c MyComponent.C
      > MyComponent.C:1 16: 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?
      >
      >[/color]
      class MyComponent : public UIComponent {

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

      XtResource MyComponent::cu stomResources_[]= {
      {
      "orientatio n",
      "Orientatio n",
      XmROrientation,
      sizeof(unsigned char),
      XtOffsetOf(MyCo mponent::Resour ces, orientation_), XmRString,
      (XtPointer)"HOR IZONTAL"
      }
      };

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

      Comment

      • Gianni Mariani

        #4
        Re: C++ And XtGetSubresourc es()

        james.p.william s@usa-spaceops.com wrote:[color=blue]
        > I'm using Young's book on mixing C++ and Motif. In it, he says to use
        > XtGetSubresourc es() 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 _customResource s[];
        > ...
        > }
        >
        > XtResource MyComponent::_c ustomResources[]={
        > {
        > "orientatio n",
        > "Orientatio n",
        > XmROrientation,
        > sizeof(unsigned char),
        > XtOffsetOf(MyCo mponent,_orient ation),
        > XmRString,
        > (XtPointer)"HOR IZONTAL"
        > },{
        > ...
        > }
        > };
        >
        > I get compiler warnings about the above call to the XtOffsetOf()
        > macro.
        >
        > % g++ -Wall -Wunused -W -ansi -pedantic -c MyComponent.C
        > MyComponent.C:1 16: 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?[/color]

        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.





        Comment

        Working...