Simple const * question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jaco Naude

    Simple const * question

    Hi

    I've been struggling with something that should be very simple to
    solve... Basically, I get a const Obj* from a function and I need to
    send a pointer to this object to a function accepting only a Obj*. I
    get compiler errors as shown below:

    source\ManagerV iewer.cpp: In member function `void
    managerViewer:: addLoggerWidget (const QWidget*)':
    source\ManagerV iewer.cpp:26: error: invalid conversion from `const
    QWidget*' to `QWidget*'

    I understand that the following will result in a pointer which can be
    changed, and the compiler stops:
    QWidget* widget = const_widget_pt r;

    But the function accepting the pointer is not my own and I need to
    send a non-const pointer to it.

    Is there a way to work around this?
    Thanks in advance
    Jaco

  • Jeff Schwab

    #2
    Re: Simple const * question

    Jaco Naude wrote:
    Hi
    >
    I've been struggling with something that should be very simple to
    solve... Basically, I get a const Obj* from a function and I need to
    send a pointer to this object to a function accepting only a Obj*. I
    get compiler errors as shown below:
    >
    source\ManagerV iewer.cpp: In member function `void
    managerViewer:: addLoggerWidget (const QWidget*)':
    source\ManagerV iewer.cpp:26: error: invalid conversion from `const
    QWidget*' to `QWidget*'
    >
    I understand that the following will result in a pointer which can be
    changed, and the compiler stops:
    QWidget* widget = const_widget_pt r;
    >
    But the function accepting the pointer is not my own and I need to
    send a non-const pointer to it.
    >
    Is there a way to work around this?
    It depends. If the function accepting the QWidget* modifies the
    addressed widget, it could break reasonable assumptions in other parts
    of the code. Welcome to debugging hell.

    A common work-around for this problem is to pass a non-const copy (or
    clone) of the const object. If you're using the QWidget from Qt,
    though, copying isn't an option.

    If you're willing to accept the consequences of your actions, try
    const_cast: const_cast<QWid get*>(const_wid get_ptr).

    Comment

    • Sam

      #3
      Re: Simple const * question

      Jaco Naude writes:
      Hi
      >
      I've been struggling with something that should be very simple to
      solve... Basically, I get a const Obj* from a function and I need to
      send a pointer to this object to a function accepting only a Obj*. I
      get compiler errors as shown below:
      >
      source\ManagerV iewer.cpp: In member function `void
      managerViewer:: addLoggerWidget (const QWidget*)':
      source\ManagerV iewer.cpp:26: error: invalid conversion from `const
      QWidget*' to `QWidget*'
      >
      I understand that the following will result in a pointer which can be
      changed, and the compiler stops:
      QWidget* widget = const_widget_pt r;
      >
      But the function accepting the pointer is not my own and I need to
      send a non-const pointer to it.
      >
      Is there a way to work around this?
      You can use const_cast<>, however you'll probably wind up with another bug
      on your hands. There is a reason why your first function returns a const
      pointer, it does not expect the object to be modified. If you go ahead and
      have it modified, you'll likely end up with more problems later down the
      road.



      -----BEGIN PGP SIGNATURE-----
      Version: GnuPG v1.4.9 (GNU/Linux)

      iEYEABECAAYFAkj 9u8UACgkQx9p3GY HlUOJ/jACfe+QwzKvH4NE HYMDBnZLySENj
      Z1cAnjFMdSUvyaK ho3upbI+wH8SSjb rN
      =cH9p
      -----END PGP SIGNATURE-----

      Comment

      • Andrew Koenig

        #4
        Re: Simple const * question

        "Jaco Naude" <naude.jaco@gma il.comwrote in message
        news:2fa82026-fb37-4fe6-8227-4307c80d3c6b@e1 7g2000hsg.googl egroups.com...
        I've been struggling with something that should be very simple to
        solve... Basically, I get a const Obj* from a function and I need to
        send a pointer to this object to a function accepting only a Obj*.
        No you don't. Or at least you don't need to ask advice about how to do it.

        If you have a const Obj*, that means you have a pointer to an object that
        you have promised not to modify.

        If you have a function that accepts only an Obj*, it means that the function
        reserves the right to modify the object.

        So what you are trying to do is to figure out how to break your promise not
        to modify the object. The best advice -- and the only advice I can advocate
        until you convince me that it is really important to do otherwise -- is
        "Don't do it."


        Comment

        • .rhavin grobert

          #5
          Re: Simple const * question

          On 21 Okt., 17:14, "Andrew Koenig" <a...@acm.orgwr ote:
          "Jaco Naude" <naude.j...@gma il.comwrote in message
          >
          news:2fa82026-fb37-4fe6-8227-4307c80d3c6b@e1 7g2000hsg.googl egroups.com...
          >
          I've been struggling with something that should be very simple to
          solve... Basically, I get a  const Obj* from a function and I need to
          send a pointer to this object to a function accepting only a Obj*.
          >
          No you don't.  Or at least you don't need to ask advice about how to doit.
          >
          If you have a const Obj*, that means you have a pointer to an object that
          you have promised not to modify.
          >
          If you have a function that accepts only an Obj*, it means that the function
          reserves the right to modify the object.
          >
          So what you are trying to do is to figure out how to break your promise not
          to modify the object.  The best advice -- and the only advice I can advocate
          until you convince me that it is really important to do otherwise -- is
          "Don't do it."
          Guess the following:

          int Obj::Read() const;
          int Obj::Write(int iWhatever);

          int PrettyFunction( int iCommand, Obj* pObject, int iAux)
          {
          switch(iCommand )
          {
          case CMD_READ:
          return pObject->Read();
          case CMD_WRITE:
          return pObject->Write(iAux);
          default:
          DropBombOnWhite House();
          }
          }

          If you have a Obj const* it would still be apropriate to call the
          PrettyFunction with the CMD_WRITE by const_cast'ing.

          Comment

          • .rhavin grobert

            #6
            Re: Simple const * question

            On 21 Okt., 17:55, ".rhavin grobert" <cl...@yahoo.de wrote:
            On 21 Okt., 17:14, "Andrew Koenig" <a...@acm.orgwr ote:
            >
            >
            >
            "Jaco Naude" <naude.j...@gma il.comwrote in message
            >
            news:2fa82026-fb37-4fe6-8227-4307c80d3c6b@e1 7g2000hsg.googl egroups.com....
            >
            I've been struggling with something that should be very simple to
            solve... Basically, I get a  const Obj* from a function and I need to
            send a pointer to this object to a function accepting only a Obj*.
            >
            No you don't.  Or at least you don't need to ask advice about how to do it.
            >
            If you have a const Obj*, that means you have a pointer to an object that
            you have promised not to modify.
            >
            If you have a function that accepts only an Obj*, it means that the function
            reserves the right to modify the object.
            >
            So what you are trying to do is to figure out how to break your promisenot
            to modify the object.  The best advice -- and the only advice I can advocate
            until you convince me that it is really important to do otherwise -- is
            "Don't do it."
            >
            Guess the following:
            >
            int Obj::Read() const;
            int Obj::Write(int iWhatever);
            >
            int PrettyFunction( int iCommand, Obj* pObject, int iAux)
            {
              switch(iCommand )
              {
              case CMD_READ:
                return pObject->Read();
              case CMD_WRITE:
                return pObject->Write(iAux);
              default:
                DropBombOnWhite House();
              }
            >
            }
            >
            If you have a Obj const* it would still be apropriate to call the
            PrettyFunction with the CMD_WRITE by const_cast'ing.
            err, i meant "CMD_READ" and forgot a "return" ;-)

            Comment

            • Salt_Peter

              #7
              Re: Simple const * question

              On Oct 21, 11:55 am, ".rhavin grobert" <cl...@yahoo.de wrote:
              On 21 Okt., 17:14, "Andrew Koenig" <a...@acm.orgwr ote:
              >
              >
              >
              "Jaco Naude" <naude.j...@gma il.comwrote in message
              >
              news:2fa82026-fb37-4fe6-8227-4307c80d3c6b@e1 7g2000hsg.googl egroups.com...
              >
              I've been struggling with something that should be very simple to
              solve... Basically, I get a const Obj* from a function and I need to
              send a pointer to this object to a function accepting only a Obj*.
              >
              No you don't. Or at least you don't need to ask advice about how to do it.
              >
              If you have a const Obj*, that means you have a pointer to an object that
              you have promised not to modify.
              >
              If you have a function that accepts only an Obj*, it means that the function
              reserves the right to modify the object.
              >
              So what you are trying to do is to figure out how to break your promise not
              to modify the object. The best advice -- and the only advice I can advocate
              until you convince me that it is really important to do otherwise -- is
              "Don't do it."
              >
              Guess the following:
              >
              int Obj::Read() const;
              int Obj::Write(int iWhatever);
              >
              int PrettyFunction( int iCommand, Obj* pObject, int iAux)
              {
              switch(iCommand )
              {
              case CMD_READ:
              return pObject->Read();
              case CMD_WRITE:
              return pObject->Write(iAux);
              default:
              DropBombOnWhite House();
              }
              >
              }
              >
              If you have a Obj const* it would still be apropriate to call the
              PrettyFunction with the CMD_WRITE by const_cast'ing.
              That you could do it, yes, thats its appropriate, no.
              The problem here is you are passing a pointer to a constant and
              looking for a way to break a promise.
              To drive the point a little further, the parameter Obj* pObject should
              really be
              Obj* const pObject
              where that crucial pointer can't be reseated (or better yet Obj&
              r_obj).

              If the client of your code sees a constant pointer to a constant (ie:
              const Obj* const pObject), the last thing the client will expect is
              that the pointee gets modified or reseated. Either would break the
              contract and in essence: the interface. At that point your client (who
              might very well be yourself in the near future) will then face the
              horror of having to read and consult every line of code because the
              interface can no longer be trusted.

              Comment

              • Victor Bazarov

                #8
                Re: Simple const * question

                ..rhavin grobert wrote:
                [..]
                Guess the following:
                >
                int Obj::Read() const;
                int Obj::Write(int iWhatever);
                >
                int PrettyFunction( int iCommand, Obj* pObject, int iAux)
                {
                switch(iCommand )
                {
                case CMD_READ:
                return pObject->Read();
                case CMD_WRITE:
                return pObject->Write(iAux);
                default:
                DropBombOnWhite House();
                }
                }
                >
                If you have a Obj const* it would still be apropriate to call the
                PrettyFunction with the CMD_WRITE by const_cast'ing.
                The point is to find out *why* in some scope where you've got the
                pointer to the const Obj, the object is const, and only then speak about
                working around that. 'const_cast' is the last resort. You can only use
                it if you're damn sure what you're doing. Without seeing a damn good
                argument why this *has* to be done, "do not do it" is the right choice,
                and means you need to redesign your software. It's possible that when
                you think that you have to call 'PrettyFunction ' and all you got is a
                const Obj*, you probably should split the 'PrettyFunction ' in two. One
                would take a const Obj* and the other - a pointer to a regular object.

                Without seeing more code it's impossible to pass judgement.

                V
                --
                Please remove capital 'A's when replying by e-mail
                I do not respond to top-posted replies, please don't ask

                Comment

                • James Kanze

                  #9
                  Re: Simple const * question

                  On Oct 21, 12:53 pm, Jaco Naude <naude.j...@gma il.comwrote:
                  I've been struggling with something that should be very simple
                  to solve... Basically, I get a  const Obj* from a function and
                  I need to send a pointer to this object to a function
                  accepting only a Obj*. I get compiler errors as shown below:
                  source\ManagerV iewer.cpp: In member function `void
                  managerViewer:: addLoggerWidget (const QWidget*)':
                  source\ManagerV iewer.cpp:26: error: invalid conversion from `const
                  QWidget*' to `QWidget*'
                  I understand that the following will result in a pointer which
                  can be changed, and the compiler stops:
                  QWidget* widget = const_widget_pt r;
                  But the function accepting the pointer is not my own and I
                  need to send a non-const pointer to it.
                  Is there a way to work around this?
                  Others have already pointed out many of the issues, but I'll
                  jump on one additional point. I don't know what QWidgit is, but
                  from it's name, it sounds like a windowing component. And
                  normally, there's never any reason to put const on a pointer to
                  a windowing component. The design error is likely in the
                  signature of addLoggerWidget , which should take a QWidgit*, and
                  not a QWidgit const*.

                  --
                  James Kanze (GABI Software) email:james.kan ze@gmail.com
                  Conseils en informatique orientée objet/
                  Beratung in objektorientier ter Datenverarbeitu ng
                  9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                  Comment

                  Working...