473,473 Members | 1,491 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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\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

Oct 21 '08 #1
8 2111
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\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?
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<QWidget*>(const_widget_ptr).
Oct 21 '08 #2
Sam
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\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?
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)

iEYEABECAAYFAkj9u8UACgkQx9p3GYHlUOJ/jACfe+QwzKvH4NEHYMDBnZLySENj
Z1cAnjFMdSUvyaKho3upbI+wH8SSjbrN
=cH9p
-----END PGP SIGNATURE-----

Oct 21 '08 #3
"Jaco Naude" <na********@gmail.comwrote in message
news:2f**********************************@e17g2000 hsg.googlegroups.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."
Oct 21 '08 #4
On 21 Okt., 17:14, "Andrew Koenig" <a...@acm.orgwrote:
"Jaco Naude" <naude.j...@gmail.comwrote in message

news:2f**********************************@e17g2000 hsg.googlegroups.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:
DropBombOnWhiteHouse();
}
}

If you have a Obj const* it would still be apropriate to call the
PrettyFunction with the CMD_WRITE by const_cast'ing.
Oct 21 '08 #5
On 21 Okt., 17:55, ".rhavin grobert" <cl...@yahoo.dewrote:
On 21 Okt., 17:14, "Andrew Koenig" <a...@acm.orgwrote:
"Jaco Naude" <naude.j...@gmail.comwrote in message
news:2f**********************************@e17g2000 hsg.googlegroups.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:
* * 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" ;-)
Oct 21 '08 #6
On Oct 21, 11:55 am, ".rhavin grobert" <cl...@yahoo.dewrote:
On 21 Okt., 17:14, "Andrew Koenig" <a...@acm.orgwrote:
"Jaco Naude" <naude.j...@gmail.comwrote in message
news:2f**********************************@e17g2000 hsg.googlegroups.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:
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.
Oct 21 '08 #7
..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:
DropBombOnWhiteHouse();
}
}

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
Oct 21 '08 #8
On Oct 21, 12:53*pm, Jaco Naude <naude.j...@gmail.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\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:ja*********@gmail.com
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
Oct 21 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: C++fan | last post by:
Hi all: Below is simple code: class bs { public: virtual void handle() = 0; }; class driv : public bs { public:
2
by: shaun roe | last post by:
A basic question, inspired by code in 'exceptional c++ style' pg. 124: The example code illustrates accessor functions instead of making data public. To paraphrase: Class X { public: T1&...
6
by: JonathanOrlev | last post by:
Hello everyone, I have a newbe question: In Access (2003) VBA, what is the difference between a Module and a Class Module in the VBA development environment? If I remember correctly, new...
2
by: shuisheng | last post by:
Dear All, Assume I have a class for a cuboid domain. The domain is defined by the cuboid's lower corner, such as (0, 0, 0), and upper corner, such as (1, 1, 1). The upper corner should be always...
176
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
1
by: nsdevelop12 | last post by:
Is "const Object* pA = new Object(1)" the same as "const Object A(1)" in terms of creating const objects that should not be modified through cast-to-non-const pointers. See the following example:...
6
by: Ronald Raygun | last post by:
I want to be able to randomly select the following from an array: 1). An image 2). A piece of text (name of tge image) 3). A piece of text (description of the image) I want to be able to...
3
by: stdlib99 | last post by:
Hi, I have a simple question regarding templates and meta programming. I am going to try and work my way through the C++ Template Metaprogramming, a book by David Abrahams and Aleksey...
17
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.