473,320 Members | 1,926 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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/


Jul 19 '05 #1
3 4390

"Simon Elliott" <si***@nospam.demon.co.uk> wrote in message news:$B**************@courtlands.demon.co.uk...
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*."
Jul 19 '05 #2
Simon Elliott <si***@nospam.demon.co.uk> writes
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/


Jul 19 '05 #3
"Ron Natalie" <ro*@sensor.com> writes:
"Simon Elliott" <si***@nospam.demon.co.uk> wrote in message news:$B**************@courtlands.demon.co.uk...
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
mi***@cowan.name
Jul 19 '05 #4

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

Similar topics

1
by: H.L Bai | last post by:
hi, everybody i meet a parse error when i used the xml4c. any proposal is helpful. The error is following .../XMLRegionHandler.h:59 parse error before '*' .../XMLRegionHandler.h:60 parse...
24
by: Steven T. Hatton | last post by:
In the following code, at what point is S::c fully defined? #include <iostream> using std::cout; using std::endl; using std::ostream; class C { int _v;
3
by: Firstname Lastname | last post by:
In a function call, for example function(Class(), &Class::memfunc), my compiler cannot match the 2nd argument when: (1) Class is a template parameter, and (2) the base class of Class defines...
7
by: groleo | last post by:
Hi. I have some problems with variables :D I have the classes given below: The problem is when I try to acces Commands().sock->m_sock, from inside Message(); When I'm using m_sock inside...
6
by: Matthias Kaeppler | last post by:
Hello, during a discussion on a C++ internet forum, some question came up regarding references and the lifetime of the objects they alias. I can't find any clear wording on that in the draft...
2
by: WindAndWaves | last post by:
Can anyone tell me when to use ENUM and when to use Public Const? TIA - Nicolaas
4
by: Aaron Queenan | last post by:
When I build a C++ library to .NET using the managed C++ compiler, I get the following error message: Linking... LINK : error LNK2020: unresolved token (0A000005) _CrtDbgReport LINK : error...
12
by: Alex Vinokur | last post by:
Why was the size_t type defined in compilers in addition to unsigned int/long? When/why should one use size_t? Alex Vinokur email: alex DOT vinokur AT gmail DOT com...
9
by: sebastian | last post by:
I've simplified the situation quite a bit, but essentially I have something like this: struct foo { }; void bar( foo const & lhs, foo const & rhs )
39
by: Leonardo Korndorfer | last post by:
Hi, I'm litle confused by the const modifier, particularly when use const char* or char*. Some dude over here said it should be const char when you dont modify it content inside the function, I...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.