Results 1 to 3 of 3

Thread: const Obj problem in C++

  1. #1
    Join Date
    May 2008
    Posts
    7

    const Obj problem in C++

    I have 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\ManagerViewer.cpp: In member function `void managerViewer::addLoggerWidget(const QWidget*)': source\ManagerViewer.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_ptr;

    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

  2. #2
    Join Date
    May 2008
    Posts
    59

    Re: const Obj problem in C++

    Code:
    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:
    DropBombOnWhiteHouse();
    }
    }

  3. #3
    Join Date
    May 2008
    Posts
    16

    Re: const Obj problem in C++

    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.

Similar Threads

  1. Sony VGNCS3 laptop 15 inch screen problem...LCD or CHIP problem?
    By Mick$Tyler in forum Hardware Peripherals
    Replies: 5
    Last Post: 31-10-2010, 06:49 AM
  2. Replies: 4
    Last Post: 10-04-2010, 04:19 PM
  3. Differentiate between const and static readonly
    By KALLIYAN in forum Software Development
    Replies: 3
    Last Post: 17-11-2009, 12:52 AM
  4. Problem of const variable and its reference
    By KABIRA16 in forum Software Development
    Replies: 3
    Last Post: 04-09-2009, 04:45 PM
  5. How to declare const variable in C++
    By AZUL in forum Software Development
    Replies: 3
    Last Post: 02-09-2009, 06:36 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,004,946.08189 seconds with 17 queries