0

Sorry if it a trivial question:

Implementation 1:

class Foo
{
    protected: int bar;

    public: Foo(int bar)
      {
        this->bar =bar;
      }
};

Implementation 2:

class Foo
{
    protected: int bar;

    public: Foo(int bar)
      {
        this.bar =bar;
      }
};

Output from implementation 2:

request for member ‘x’ in ‘this’, which is of pointer type ‘Foo* const’ (maybe you meant to use ‘->’ ?)

so this is a pointer, and this question has syntax error in code

Community
  • 1
  • 1
aiao
  • 4,621
  • 3
  • 25
  • 47

1 Answers1

6

The question you are referring to contains code samples written in C#, not C++. Yes, in C++, this is a pointer and must be dereferenced to access any members of the object it points to.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • sorry about that it was tagged with c++, so assumed they wold be the same. Thanx – aiao Dec 07 '12 at 16:50
  • Actually this is a pointer in C# too, but C# has got a sort of automatic dereference :) – BlackBear Dec 07 '12 at 16:50
  • @BlackBear Thanks, I don't know any C#. I think my edit doesn't suggest anything about C# now. – Joseph Mansfield Dec 07 '12 at 16:51
  • @BlackBear: no, it's not "a sort of automatic dereference". The `.` operator in Java and C# is simply how you write the C++ `->` operator in those languages. Different languages write their operators differently. – newacct Dec 08 '12 at 02:49
  • @newacct in C# you use the dot operator both for structs (which value types) and classes (reference types). If the dot is applied to a struct it is equivalent to the C++'s dot while if it's applied to a class it actually operates as the C++'s arrow. – BlackBear Dec 08 '12 at 14:05