-3

I'm new to C++, I want to understand why I can't use the dot (.) operator here, since the variable is declared inside the class.

class SessionController {
    private: SessionView view;
    private: Session model;
    public: SessionController (SessionView view, Session model) {
        this.view = view;
        this->model = model;
    }
};

I'm getting an error at this.view = view; saying Member reference type 'SessionController *' is a pointer; did you mean to use '->'?

Akash Sonthalia
  • 362
  • 2
  • 12
  • 5
    Two things: First of all, `this` is a *pointer* to an instance (object) of the current class; And using the "arrow" operator `->` is combining member access with pointer dereference. The expression `this->model` is equivalent to `(*this).model`. To be honest, any [decent book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), tutorial or class should have this information. – Some programmer dude Sep 14 '20 at 11:13
  • 1
    because `this` is a pointer and not an object/reference – phuclv Sep 14 '20 at 11:16
  • Because `this` is a pointer. The `->` works on pointers. The `.` operator works on references. The two are not generally interchangeable, except in special cases where a class provides overloads of both operators (which happens, but is unusual in practice, and is not what your code does). – Peter Sep 14 '20 at 11:18
  • 3
    Oh dear, it looks like you come from a Java/C# background. C++ is very different, so much that it can appear irrational if you expect certain things to be that same as in other languages. It's best to let those expectations go. Pick C+ up from the ground up. – StoryTeller - Unslander Monica Sep 14 '20 at 11:18
  • 1
    duplicate: [How do the “->” and “.” member access operations differ in C](https://stackoverflow.com/q/3424079/995714), [what's the difference about p.a and p->a where p is pointer?](https://stackoverflow.com/q/5649619/995714), [What is the rationale for difference between -> and . in c/c++?](https://stackoverflow.com/q/2936993/995714), [Why does C have a distinction between -> and .?](https://stackoverflow.com/q/1813865/995714) – phuclv Sep 14 '20 at 11:18
  • 1
    `a->b` is equivalent to `(*a).b` unless the operators are overloaded. – Bathsheba Sep 14 '20 at 11:18
  • 2
    @StoryTeller-UnslanderMonica: I see every departure from Java and C# as a good thing. – Bathsheba Sep 14 '20 at 11:19
  • There's nothing special about `this`. It's just like any other pointer. If you have `struct s {int x,y;}; s s0; s *p = &s0;` you can't use `p.x`, you have to use `p->x`. – einpoklum Sep 14 '20 at 11:37

1 Answers1

0

In this case you can avoid the problem by setting the values directly in the initialiser list.

class SessionController {
    private: SessionView view;
    private: Session model;
    public: SessionController (SessionView view, Session model) : view(view), model(model) {
    }
};

Or if you really need to do it in the body.

class SessionController {
    private: SessionView view;
    private: Session model;
    public: SessionController (SessionView pview, Session pmodel) {
        view = pview;
        model = pmodel;
    }
};
Surt
  • 15,501
  • 3
  • 23
  • 39