Prototyping methods in a class

At the top of my class definition I would like to include a private list of all the class methods so that they can easily have guaranteed access to each other. But when I try to compile this code it give me errors about not being able to overload my functions. I tried adding void to the function definitions to differentiate them from the prototype declarations to no avail.

an abridged version:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

class board
{
  private:
    void draw();    //draw the current active player, the contents of the play area, and move count
    void wipe();    //clear values for a new game
    bool find_win();//returns true if a winner is detected
    bool update();  //returns true if the game will keep going
  public:
    void wipe (void)
      {return;}
  
     void draw (void)
      {return;}
  
     bool find_win (void)
      {return true;}     
  
     bool update (void)
      {return true;}
};

int main()
{
 return 0;
}
Last edited on
You do not need to forward-declare class members for use within the class. So even if lines 6-10 were correct, they would be entirely redundant and should be removed.
Last edited on
that is not what private does. methods in a class can already access ALL (public and private) methods and members of the class.
private marks some of the stuff as inaccessible to the user of the class.
lets say wipe were private, the rest public.
then
board b;
b.wipe(); //fails: can't call private member at this level.
b.draw(); //ok, its public, I can use it.

but inside the class you can still call wipe all you want.

if you want overloads they need to differ slightly in parameters.
you can put in a dummy variable, eg a void* or int that you pass nullptr or zero into and overload that, so that
wipe(0) is private while wipe() is public.
but that is not something I would recommend. I avoid having things with the same name at all costs. Even a simple wipe_private() and wipe_public() would be better IMHO. There is a time to overload, and that does share the name, but I would not do it here (its a style thing).
Last edited on
I would like to include a private list of all the class methods so that they can easily have guaranteed access to each other.
They already have access to each other. With global or static functions, you need to declare the function before you can use it, but within a class, there is is no such restriction:

1
2
3
4
5
6
7
8
9
10
11
12
void f() {
    g();  // compiler error. g() has not been declared yet
}

void g() {
   ...
}

class C {
    void x() { y(); }  // okay. All members of C know about all other members
    void y() { ... }
};


If your goal is to make the interface to the class clear (a noble goal), you can do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class board
{
  public:
    void draw();    //draw the current active player, the contents of the play area, and move count
    void wipe();    //clear values for a new game
    bool find_win();//returns true if a winner is detected
    bool update();  //returns true if the game will keep going
};

inline void board::wipe (void)
      {return;}
  
inline void board::draw (void)
      {return;}
  
inline bool board::find_win (void)
      {return true;}     
  
inline bool board::update (void)
      {return true;}
};

Notice that you need to explicitly define the methods as inline when you do it this way. If you define them within the class then the "inline" is implicit. Of course if you define the methods in a separate .cxx file, then don't define them inline.
Topic archived. No new replies allowed.