Calling functions within a class

I noticed when practicing classes that when calling functions within a class I can either use the resolution scope operator or leave that off, and my program still works. Is it better to include it or not? Example down below calls the introduction function twice, which one to keep?

void GuessGame::run ()
{
GuessGame::introduction ();
introduction ();
Last edited on
you may also use this->

it would be necessary to use the scope resolution if you need to call a function of the base class, other than that use what's clearer.
Thanks for your answer. But I don't understand your top line. I may use what? Can you clarify please?
resolution scope operator

scope resolution operator
I may use what?

use whatever's clearer
And sometimes you may need scope resolution to actually get to the variable you're after:
1
2
3
4
5
6
7
8
9
10
#include <iostream>

const int value = 5;

int main()
{
    const int value = 10;
    std::cout << value << "\n"; //prints 10, 
    std::cout << ::value << "\n"; //prints 5
}
Last edited on
Ahhh....I see.

Also after seeing your example it just dawned on me that std::cout is also a scope resolution (if you want to avoid using namespace std), calling upon the standard library I think.

Thanks gunner.
Thanks ne555.
this is a pointer that points to the object to which you send the message.
For example if you do game.run();, inside `.run()' this would point to `game'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class foo{
public:
   virtual double value(){
      return 42;
   }
   virtual ~foo() = default;
};

class bar: public foo{
   double bonus;
public:
   virtual double value(){
      return foo::value() * this->bonus;
   }
}
True, gunner.

I know you want to avoid global things when possible, it's just that typing a lot of std::cout all over the place takes extra time. I can always take it out of global and make it local or something.

Thanks again for everybody's help.
Topic archived. No new replies allowed.