Class and function question quick help!

what is the different between using ->out() and sub.out() to call the function out()?
Also, I would like to know why we use -> to call the function out() as well.
thanks!

by the way, the output is "bb" and I know that "virtual" keyword makes the Sup class' out function overridden by its subclass. Please correct me or add more explanations if there is something that I am misunderstanding.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include <iostream>
    using namespace std;
    class Sup {
    public: virtual void out() { cout << "p"; }
    };
    class Sub : public Sup {
    public: virtual void out() { cout << "b"; }
    };
    int main()
    {
            Sub sub;
            Sup *sup;
            sup = &sub;
            sup->out(); // what is the different between using ->out() and sub.out() to call the function out()? 
            sub.out();  //
            
            return 0;
   
Last edited on
sup->out() is the same thing as (*sup).out().
It's just syntactical sugar for when you want to call a member function on an object when you just have a pointer to it.
One more question.

I've seen another method involving " Sup::out() " ( or something very similar using :: method)
can you explain me how I could use that here?
Last edited on
anyone?
Topic archived. No new replies allowed.