c ++静态方法输出使用cout总是1 [英] c++ static method output using cout is always 1

查看:174
本文介绍了c ++静态方法输出使用cout总是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;
}

输出为1.
即使我更改 stu :: no ,以便它只会 {return 12;}
为什么会发生?如何修复它

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??

推荐答案

将其更改为 std :: cout< stu :: no()<< std :: endl;

没有()

编辑:正如@Loomchild所指出的,使用 g ++ -Wall 将提供进一步的洞察,为什么它总是1.在这个上下文中,静态函数的指针总是被评估为 true ,因此被打印的值。

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.

这篇关于c ++静态方法输出使用cout总是1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆