0

I have a code working like:

class base {
....
protected:
  typeA m_mem;
}

class mymodule: public base{
....
void function(){
  m_mem.call();
}
}

This was working OK before. Suddenly, I see it brokes saying "m_mem was not declared...." It might be some other people changed the namespace or other parts.

And I found it was working by just adding "this" then it compiles fine

 this->m_mem.call()

While I just would like to know what's the cases that I must use "this" ? I learned "this" can be used to point to distinguish between class member and argument names. While for my case, what can be the reason that I must use "this" for accessing a data member

thundium
  • 995
  • 4
  • 12
  • 30
  • 2
    Can you provide a minimal, complete sample source, to demonstrate this? – donjuedo Aug 24 '15 at 14:20
  • I know how `this->` can resolve ambiguity; I DON'T know how it can resolve a non-declared symbol. I guess, we would need at least an exact error message. – Vlad Feinstein Aug 24 '15 at 14:32

1 Answers1

1

This can occur for example when you use templates.

The compiler will issue an error that x is an undefined variable for these class definitions

template <class T>
struct A
{
    int x;
};

template <class T>
struct B : A<T>
{
    void set_x( int v ) { x = v; }
};

If you write

void set_x( int v ) { this->x = v; }

the code will compile.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335