C++中static成员函数的有关问题
C++中static成员函数的问题
//using namespace std;
#include <iostream.h>
#include <stdio.h>
class T{
private:
char name[20];
int age;
int Balance;
//static int code;
public:
T(){
cout < < "the private structure can be visited " < <endl;
}
public:
static int code;
int CheckBablance() const;
int getCode(){
return code;
}
static T* visit(){
return(new T);
}
static void display(){
cout < < "the static function be visited\n ";
}
};
int T::code=2004;
//T:: int code=2004; //error
int T::CheckBablance() const
{
if((Balance==100)||(Balance <100))
cout < < "你卡上的余额已近不足,请尽快冲值! " < <endl;
return Balance;
}
void main(){
char c1;
T* vis=T::visit();
cout < < "the value of static member is " < <vis-> getCode() < <endl;
(*vis).CheckBablance();
T::display();
delete vis;
cout < < "========================================= " < <endl;
T t1;
cout < < "the value of static member is " < <t1.getCode() < <endl;
t1.CheckBablance();
printf( "*******************TO END*******************\n ");
scanf( "%c ",&c1);
}
/*问题:1, 首先通过在class T中定义了一个static成员函数生成class T的指针,即 T* vis
我们知道,只有在创建对象的时候,系统才会分内存并且调用构造函数。但是,现在在没有创建任何对象的前提下,怎么会调用class T的构造函数?
2, 命名空间using namespace std的作用是什么?
为什么加上这句话后总是出错?
error C2871: 'std ' : does not exist or is not a namespace
Thanks for your help!
*/
------解决方案--------------------
静态成员函数是编译时就构造了,他不属于对象,而是属于类的,所以不随创建对象而创建,就想全局变量和景泰变量类似,编译时就构造了
命名空间是为了避免重复使用标识符 如果你要是USING NAMESPACE STD不报错 可以用C++的头文件 INCLUDE <IOSTREAM> 不要加.H 因为那是C语言格式的 在C语言中没用到STD 所以你要是用C格式的头文件加上STD声明当然就错了
------解决方案--------------------
要么用C++用法
#include <iostream>
using namespace std;
要么就用C语言用法
#include <iostream.h>
//using namespace std;
#include <iostream.h>
#include <stdio.h>
class T{
private:
char name[20];
int age;
int Balance;
//static int code;
public:
T(){
cout < < "the private structure can be visited " < <endl;
}
public:
static int code;
int CheckBablance() const;
int getCode(){
return code;
}
static T* visit(){
return(new T);
}
static void display(){
cout < < "the static function be visited\n ";
}
};
int T::code=2004;
//T:: int code=2004; //error
int T::CheckBablance() const
{
if((Balance==100)||(Balance <100))
cout < < "你卡上的余额已近不足,请尽快冲值! " < <endl;
return Balance;
}
void main(){
char c1;
T* vis=T::visit();
cout < < "the value of static member is " < <vis-> getCode() < <endl;
(*vis).CheckBablance();
T::display();
delete vis;
cout < < "========================================= " < <endl;
T t1;
cout < < "the value of static member is " < <t1.getCode() < <endl;
t1.CheckBablance();
printf( "*******************TO END*******************\n ");
scanf( "%c ",&c1);
}
/*问题:1, 首先通过在class T中定义了一个static成员函数生成class T的指针,即 T* vis
我们知道,只有在创建对象的时候,系统才会分内存并且调用构造函数。但是,现在在没有创建任何对象的前提下,怎么会调用class T的构造函数?
2, 命名空间using namespace std的作用是什么?
为什么加上这句话后总是出错?
error C2871: 'std ' : does not exist or is not a namespace
Thanks for your help!
*/
------解决方案--------------------
静态成员函数是编译时就构造了,他不属于对象,而是属于类的,所以不随创建对象而创建,就想全局变量和景泰变量类似,编译时就构造了
命名空间是为了避免重复使用标识符 如果你要是USING NAMESPACE STD不报错 可以用C++的头文件 INCLUDE <IOSTREAM> 不要加.H 因为那是C语言格式的 在C语言中没用到STD 所以你要是用C格式的头文件加上STD声明当然就错了
------解决方案--------------------
要么用C++用法
#include <iostream>
using namespace std;
要么就用C语言用法
#include <iostream.h>