1

So i have a piece of code with a class like that:

#include<iostream>
#include<cstring>

class stu
{
    static int proba;
public:
    stu();
    static int no(){
        return proba;
    }
};

int stu::proba=0;

stu::stu()
{
    proba=proba+1;
}

int main()
{
    std::cout<< stu::no << std::endl;
}

The output is 1. It does so even if i change stu::no so that it would be only {return 12;} Why does it happen? How do I fix it??

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
mmajewsk
  • 99
  • 11
  • 2
    is this is a typo because stu::no is a function and it should print something that looks like a pointer. Did you mean std::cout << stu::no() << std::endl; ? – CashCow Oct 24 '12 at 13:51

4 Answers4

7

Change it to std::cout<< stu::no() << std::endl;

Without the (), I believe it's evaluating as a pointer, and not doing what you're expecting.

Edit: As pointed out by @Loomchild, using g++ -Wall will provide further insight as to why it's always 1. The pointer to the static function is always evaluated as true in this context, hence the value being printed.

Dave S
  • 20,507
  • 3
  • 48
  • 68
  • 3
    `g++ -Wall` warns with the following : `warning: the address of 'static int stu::no()' will always evaluate as 'true'` – Loomchild Oct 24 '12 at 13:54
  • @Loomchild: Ah, that explains it. – Dave S Oct 24 '12 at 13:55
  • Oh, i can see that, could you give me any hint how i could do that without additional "()", i mean without changing main ? – mmajewsk Oct 24 '12 at 14:00
  • @user1267757: Not really. If you want `main()` to call a function, you have to use the function call syntax, which involves putting the () after the function name. Any workaround would still involve a change to main to make it call a function. – Dave S Oct 24 '12 at 14:02
  • oh, i got it now, i just changed name of "proba" into "no" and then deleted former "no" – mmajewsk Oct 24 '12 at 14:06
2

std::cout<< stu::no << std::endl; prints the address of the function, you're not actually calling it.

std::cout<< stu::no() << std::endl;

calls the function and prints the return value.

In MSVS, this indeed produces a pointer value, with the overload operator << (void*).

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1

Use stu::no() instead of stu::no. Also, a minor thing really but if you put

using namespace std;

below the #includes you won't have to use std::

Just makes things a little more readable.

Big Bream
  • 152
  • 3
0

stu::no is a function that takes no arguments and returns int.

There is no operator<< that takes functions with your particular signature, so the available overloads are considered. Long story short, the operator<<(ostream&, bool) is the closest match, after function-to-pointer and pointer-to-bool conversions.

Since the function actually exists, its address is definitely non-zero, so the pointer to bool conversion always yields true, which you see as 1.

Make it std::cout<< std::boolalpha << stu::no << std::endl; to see for yourself that it's really a boolean output.

Make it std::cout<< stu::no() << std::endl; to print the result of the function call.

See How to print function pointers with cout? if you want to know what happened in more detail.

Community
  • 1
  • 1
Cubbi
  • 46,567
  • 13
  • 103
  • 169