 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Simon Elliott Guest
|
Posted: Thu Oct 30, 2003 6:57 pm Post subject: When is 'this' const? |
|
|
In a class function body, I'm trying to pass 'this' to another function.
I'm getting an error because 'this' seems to have become const. Any
ideas as to why this has happened?
extern void DoSomethingWithFoo(foo* fooPtr);
virtual void foo::doStuff(void)
{
DoSomethingWithFoo(this); // error: can't convert foo* const to foo*
}
Very weirdly, in another very similar class, exactly the same idiom has
worked. The only difference is that the function is inlined into the
header in the case which doesn't work. Is there some rule that says that
'this' is const when inlined into the header?
--
Simon Elliott
http://www.ctsn.co.uk/
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Thu Oct 30, 2003 7:04 pm Post subject: Re: When is 'this' const? |
|
|
"Simon Elliott" <simon (AT) nospam (DOT) demon.co.uk> wrote
| Quote: | In a class function body, I'm trying to pass 'this' to another function.
I'm getting an error because 'this' seems to have become const. Any
ideas as to why this has happened?
extern void DoSomethingWithFoo(foo* fooPtr);
virtual void foo::doStuff(void)
{
DoSomethingWithFoo(this); // error: can't convert foo* const to foo*
}
By any chance is "doStuff()" really defined: |
virtual void foo::doStuff() const { ...
Inside a const method, the this pointer is of type "const foo*."
|
|
| Back to top |
|
 |
Simon Elliott Guest
|
Posted: Thu Oct 30, 2003 7:10 pm Post subject: Re: When is 'this' const? |
|
|
Simon Elliott <simon (AT) nospam (DOT) demon.co.uk> writes
| Quote: | In a class function body, I'm trying to pass 'this' to another function.
I'm getting an error because 'this' seems to have become const. Any
ideas as to why this has happened?
extern void DoSomethingWithFoo(foo* fooPtr);
virtual void foo::doStuff(void)
{
DoSomethingWithFoo(this); // error: can't convert foo* const to foo*
}
Very weirdly, in another very similar class, exactly the same idiom has
worked. The only difference is that the function is inlined into the
header in the case which doesn't work. Is there some rule that says that
'this' is const when inlined into the header?
|
I've now discovered that this issue was due to a silly mistake I made
further down the inheritance chain. I may have misunderstood the error
message as it said "foo* const" which I read as "const foo*"...
--
Simon Elliott
http://www.ctsn.co.uk/
|
|
| Back to top |
|
 |
Micah Cowan Guest
|
Posted: Fri Oct 31, 2003 8:53 am Post subject: Re: When is 'this' const? |
|
|
"Ron Natalie" <ron (AT) sensor (DOT) com> writes:
| Quote: | "Simon Elliott" <simon (AT) nospam (DOT) demon.co.uk> wrote
In a class function body, I'm trying to pass 'this' to another function.
I'm getting an error because 'this' seems to have become const. Any
ideas as to why this has happened?
extern void DoSomethingWithFoo(foo* fooPtr);
virtual void foo::doStuff(void)
{
DoSomethingWithFoo(this); // error: can't convert foo* const to foo*
}
By any chance is "doStuff()" really defined:
virtual void foo::doStuff() const { ...
Inside a const method, the this pointer is of type "const foo*."
|
But the error message indicates that this is of type "foo *
const". Weird.
--
Micah J. Cowan
[email]micah (AT) cowan (DOT) name[/email]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|