0

So I found this

public:
    ListElem(string s);
    
    string     getData(void) const;
    ListElem*  getNext(void) const;
    ListElem*  getPrev()     const;

Btw this is a snippet of a list in a header file written in cpp

Can someone tell me whats that "(void)" for? Where is the difference between methodName() and methodName(void)?

Cpp_Newbie
  • 41
  • 4
  • 3
    Looks like a relic of C, where there was a difference between those two signatures. See [this question](https://stackoverflow.com/questions/51032/is-there-a-difference-between-foovoid-and-foo-in-c-or-c) for more. – Nathan Pierson Jan 21 '21 at 17:00
  • @NathanPierson _absolutely_ a duplicate, in my opinion... (And then Asteroids beats me to it) – Drew Dormann Jan 21 '21 at 17:06

3 Answers3

3

Can someone tell me whats that "(void)" for? Where is the difference between methodName() and methodName(void)?

In C++, there is no difference. Both are functions that take no parameters.

In C, only functionName(void) claims to take no parameters. functionName() indicates that the parameter list isn't being specified.

It should also be noted that C++ does not recognize the term method to mean anything. You are talking about functions.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
1

In C++ void foo() is equal to void foo(void), so they both mean that foo should take no arguments. You can use latter to highlight the lack of arguments, but they work pretty much the same.

whiskeyo
  • 873
  • 1
  • 9
  • 19
1

methodName() and methodName(void) both are same its just that you are telling compiler i dont want anything as a paramer if you dont write void then also it take void.

Both are same.