When is 'this' const?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Elliott

    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 DoSomethingWith Foo(foo* fooPtr);

    virtual void foo::doStuff(vo id)
    {
    DoSomethingWith Foo(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







  • Ron Natalie

    #2
    Re: When is 'this' const?


    "Simon Elliott" <simon@nospam.d emon.co.uk> wrote in message news:$BBqlCAe8V o$IwLj@courtlan ds.demon.co.uk. ..[color=blue]
    > 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 DoSomethingWith Foo(foo* fooPtr);
    >
    > virtual void foo::doStuff(vo id)
    > {
    > DoSomethingWith Foo(this); // error: can't convert foo* const to foo*
    > }
    >[/color]
    By any chance is "doStuff()" really defined:

    virtual void foo::doStuff() const { ...

    Inside a const method, the this pointer is of type "const foo*."


    Comment

    • Simon Elliott

      #3
      Re: When is 'this' const?

      Simon Elliott <simon@nospam.d emon.co.uk> writes[color=blue]
      >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 DoSomethingWith Foo(foo* fooPtr);
      >
      >virtual void foo::doStuff(vo id)
      >{
      > DoSomethingWith Foo(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?[/color]

      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







      Comment

      • Micah Cowan

        #4
        Re: When is 'this' const?

        "Ron Natalie" <ron@sensor.com > writes:
        [color=blue]
        > "Simon Elliott" <simon@nospam.d emon.co.uk> wrote in message news:$BBqlCAe8V o$IwLj@courtlan ds.demon.co.uk. ..[color=green]
        > > 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 DoSomethingWith Foo(foo* fooPtr);
        > >
        > > virtual void foo::doStuff(vo id)
        > > {
        > > DoSomethingWith Foo(this); // error: can't convert foo* const to foo*
        > > }
        > >[/color]
        > By any chance is "doStuff()" really defined:
        >
        > virtual void foo::doStuff() const { ...
        >
        > Inside a const method, the this pointer is of type "const foo*."[/color]

        But the error message indicates that this is of type "foo *
        const". Weird.

        --
        Micah J. Cowan
        micah@cowan.nam e

        Comment

        Working...