-
Constant pointers
If I have this
Code:
const HermitCrab* t = &b;
b.GetShellSize();
how can I make sure it compiles. Also if I have this
Code:
HermitCrab a(1);
const HermitCrab b(3);
Will Compile Code…
(Yes/No)?
____________ 1 HermitCrab* const q = &a;
q->MoveIntoShell(8);
____________ 2 const HermitCrab* p = &a;
p->MoveIntoShell(8);
____________ 3 HermitCrab* const m = &b;
m->MoveIntoShell(2);
____________ 4 const HermitCrab* r = &b;
r->MoveIntoShell(8);
____________ 5 const HermitCrab* p = &b;
p = &a;
are these answers right
1) No because the pointer is read only
2) Yes
3) No because the object is constant therefore it most have a const object pointer
4) Yes
5) Yes
-
>how can I make sure it compiles.
Hmm, define HermitCrab with a GetShellSize member function and define b to be an object of type HermitCrab.
>1) No because the pointer is read only
And what does that have to do with calling a member function on the pointed to object?
>2) Yes
Why?
>4) Yes
Why?
>5) Yes
Why?
-
Erm, it takes what - 30 seconds to actually type that into a small program and see if you get any 'you can't do that it's const' type error messages.