I love pointers - they are greatest invention in the history of programming!!!

P

puzzlecracker

class Foo {
public:
void modify(); // make some modify to the this object
};

int main()
{
const Foo x;
Foo* p;
const Foo** q = &p; // q now points to p; this is (fortunately!) an
error
*q = &x; // p now points to x
p->modify(); // Ouch: modifies a const Foo!!
...
}

This is beautiful ... the POWER OF TODAY.... and many years ahead.
 
I

int2str

puzzlecracker said:
class Foo {
public:
void modify(); // make some modify to the this object
};

int main()
{
const Foo x;

Doesn't compile.
x needs to be initialized.
Foo* p;
const Foo** q = &p; // q now points to p; this is (fortunately!) an
error

Yes, doesn't compile either.
*q = &x; // p now points to x

What does "now" mean? Since the previous line doesn't compile, tihs
line will not work either.
p->modify(); // Ouch: modifies a const Foo!!

How? Present a case where it does.
That's not to say that you couldn't, but what exactly is your point?
This is beautiful ... the POWER OF TODAY.... and many years ahead.

Hope I'm not just feeding the troll here, but what exactly is your
point again?

Cheers,
Andre
 
C

Christopher Benson-Manica

puzzlecracker said:
class Foo {
public:
void modify(); // make some modify to the this object
};
int main()
{
const Foo x;

const Foo x();
Foo* p;
const Foo** q = &p; // q now points to p; this is (fortunately!) an > error

Yes, it's an error - so why do you seem to be upset?
*q = &x; // p now points to x

Also an error.
p->modify(); // Ouch: modifies a const Foo!!

Ouch, your compiler won't let you do this without explicit
instructions to the contrary. The tragedy!
 
B

benben

Christopher said:
const Foo x();

I think the OP wanted to create a const object, and he did it right. Why
do you try to trick him to declare a function returning a const Foo?

Ben
 
P

puzzlecracker

benben said:
I think the OP wanted to create a const object, and he did it right. Why
do you try to trick him to declare a function returning a const Foo?

Ben

most compilers are smart enough to catch that.
 
I

int2str

puzzlecracker said:
most compilers are smart enough to catch that.

"Most compilers" are also "smart enough" to not allow your inital line
(const Foo x;) since it doesn't initizlize the constant object. May I
suggest:

const Foo x = Foo();

But you have still not enlightened us what your little pointer rant
here is all about.

Cheers,
Andre
 
E

Earl Purple

puzzlecracker schrieb:

But you have still not enlightened us what your little pointer rant
here is all about.

It's actually in the FAQ:

why you cannot point a const Foo ** to a Foo ** (which is what &q is).

And the reason given in the FAQ is the exact example here to show how
if you could you could then modify a const object.

You should be allowed to point a const Foo * const * to a Foo **
although VC7 didn't allow me to do it.

http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.17
 
E

Earl Purple

Earl said:
You should be allowed to point a const Foo * const * to a Foo **
although VC7 didn't allow me to do it.
int main()
{
const Foo x = Foo(); // let's get this initialised
Foo* p;
const Foo* const * q = &p; // should be allowed
*q = &x; // this would now be illegal is q is pointer to
const.
p->modify(); // Ouch: modifies a const Foo!!
...
}
 
W

werasm

Doesn't compile.
x needs to be initialized.


Yes, doesn't compile either.


What does "now" mean? Since the previous line doesn't compile, tihs
line will not work either.


How? Present a case where it does.
That's not to say that you couldn't, but what exactly is your point?


Hope I'm not just feeding the troll here, but what exactly is your
point again?

I think his point is, that these guys (who though out not to allow
natural conversion from foo** to const foo**) really had foresight.
Mindsmacker - Initially I though the natural conversion looked ok! He
could make it more explicit though. Yes, I had thought him a troll too
in the past - can't make up my mind, though.

Werner
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top