Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Simple const * question

Reply
Thread Tools

Simple const * question

 
 
Jaco Naude
Guest
Posts: n/a
 
      10-21-2008
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\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 in advance
Jaco

 
Reply With Quote
 
 
 
 
Andrew Koenig
Guest
Posts: n/a
 
      10-21-2008
"Jaco Naude" <> wrote in message
news:2fa82026-fb37-4fe6-8227-...

> 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."


 
Reply With Quote
 
 
 
 
.rhavin grobert
Guest
Posts: n/a
 
      10-21-2008
On 21 Okt., 17:14, "Andrew Koenig" <a...@acm.org> wrote:
> "Jaco Naude" <naude.j...@gmail.com> wrote in message
>
> news:2fa82026-fb37-4fe6-8227-...
>
> > 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:
DropBombOnWhiteHouse();
}
}

If you have a Obj const* it would still be apropriate to call the
PrettyFunction with the CMD_WRITE by const_cast'ing.
 
Reply With Quote
 
.rhavin grobert
Guest
Posts: n/a
 
      10-21-2008
On 21 Okt., 17:55, ".rhavin grobert" <cl...@yahoo.de> wrote:
> On 21 Okt., 17:14, "Andrew Koenig" <a...@acm.org> wrote:
>
>
>
> > "Jaco Naude" <naude.j...@gmail.com> wrote in message

>
> >news:2fa82026-fb37-4fe6-8227-....

>
> > > 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:
> * * DropBombOnWhiteHouse();
> * }
>
> }
>
> 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"
 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      10-21-2008
On Oct 21, 11:55 am, ".rhavin grobert" <cl...@yahoo.de> wrote:
> On 21 Okt., 17:14, "Andrew Koenig" <a...@acm.org> wrote:
>
>
>
> > "Jaco Naude" <naude.j...@gmail.com> wrote in message

>
> >news:2fa82026-fb37-4fe6-8227-...

>
> > > 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:
> DropBombOnWhiteHouse();
> }
>
> }
>
> 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.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      10-21-2008
On Oct 21, 12:53*pm, Jaco Naude <naude.j...@gmail.com> wrote:
> 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\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?


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:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re-using a simple type definition; with enumeration constraint andwithout enumeration constraint puvit82 XML 4 02-01-2008 03:46 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM
"error C2057: expected constant expression", "error C2466: cannot allocate an array of constant size 0". Why doesn't my simple program work??? hn.ft.pris@gmail.com C++ 13 01-22-2007 02:03 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57