static数据成员、成员函数的问题

static数据成员、成员函数的问题

静态成员变量与静态成员函数的声明与定义

===================

static数据成员、成员函数的问题

错误提示:pure specifier can only be specified for functions

问题原因:不能在类里边赋static数据成员的初值

错误提示:'static' should not be used on member functions defined at file scope

问题原因:static 不能在文件区域内定义!也就是说不能在类里面定义,必须先在类里面声明然后在类外定义!

总结(转载如下)

1. 静态数据成员static data member 是 类的,而不是仅属于某个对象的,

该类的所有对象都可以调用它,类也可以调用。对象可调用它,但不是独占。

生存期:编译时就分配,程序结束才释放空间。它只能在类体外初始化。

作用域:与定义它的类的作用域相同,即与类同在。他的存在是为了对象间的共同交流。

2. 静态成员函数Static member function 是为了调用 static data member 而存在。

Compile 不为他分配this 指针,所以它不能调用对象 的非static data member。

当你非要调用 非Static data member 时,只要 指明对象就行了(因为没有compile 没有为类的static member function 指定 this 指针,所以引用 非静态成员变量时分不清是哪个对象的,当你指明对象来调用不就行了。)

加了static 限制符的 data member or member function 只是限制了调用的权利,并没有限制 被调用的权利。非static 的对象和 非static的member function 都可以调用他们。

------------------

#include <iostream>
using namespace std;

class Student
{
public :
    Student(int n=3, int a=20,float s=100):num(n),age(a),score(s){}; //define the constructervoid total();//这个构造函数可以代替 构造函数的重载(三个参数,两个参数,一个参数都可以调用它。但0个参数不可以,以为 C++ 为了兼容C 的语法,Student std() 是定义了一个返回Student 类的函数。可以用 Stduent std 来代替。即去掉括号)。


    static float average(); // define static member function
private : // 变量尽量设置为 private ,用成员函数 intialize and get, set
    int num;
    int age;
    float score;
    static float sum;
    static int count;
    
};// this semicolon must be added.


void Student::total()
{
    sum+=score;// clculate the sun of secores // 普通函数可以调用 static 定义的sum ,count
    count++; // count the number of students
}


float Student::average()  // 如果你在函数前加static 会出错:

//'static' should not be used on member functions defined at file scope
{

        return (sum/count);
       // num = 0; // 非静态函数不可被static members function 调用
}


float Student::sum = 0;// static data member defined must be outside the class
int   Student::count = 0;//


int main()
{
    Student std[3]={Student(1001,18,70),Student(1002,19,80),Student(1003,20,90)};
    int n;
    cout<< "input the number of student";
    cin>>n;
    
    for(int i= 0;i<n;i++)
    {
        std[i].total();
    }


    cout<< "the average score of "<<n<<"student is "<< Student::average()<<endl;//invoke static member function


    cout<< "the average score of "<<n<<"student is "<< Student::average()<<endl;//invoke static member function
    
    cout<<" "<<std[0].average()<<endl;// 类的对象可以调用 类的static data member
    
    std[1].outputAverage();// 类的对象可以调用普通函数who invoke static member function
    return 0;
}

————————————————
版权声明:本文为CSDN博主「NewVitamin」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zxxyyxf/article/details/6432612

==========================

静态成员变量与静态成员函数的声明与定义

类的静态成员变量在类中只能声明,定义和初始化要在类外:

    class Myclass
    {
    private:
        static int a;      // 类内声明
    }
     
    int Myclass::a = 10;        // 类外定义和初初化,注意,这时不需要带static

也有些编译器支持在类内定义并初始化,但大多数编译器不支持,所以安全的做法还是类内声明,类外定义和初始化。

类的静态成员函数的声明和定义与普通的成员函数相同,既可以在类内声明和定义,也可以在类内声明,类外定义:

    class Myclass
    {
    public:
        static int fun();
    }
     
    int Myclass::fun()
    {
        return 1;
    }

需要注意的是,在类外的函数定义前不能写static
————————————————
版权声明:本文为CSDN博主「mwill」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/mwill/article/details/38064551

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值