#include<stdio.h>classPoint{public:voidinit(){}staticvoidoutput(){printf("%d\n",m_x);}private:int m_x;};voidmain(){Point pt;pt.output();} 编译出错:error C2597: illegal reference to data member ‘Point::m_x’ in a static member function 因为静态成员函数属于整个类,在类实例化对象之...
其它文件中可以定义相同名字的变量,不会发生冲突(自然了,因为static隔离了文件,其它文件使用相同的名字的变量,也跟它没关系了); 3.静态函数:准确的说,静态函数跟静态全局变量的作用类似: //file a.c#include <stdio.h>voidfn() { printf("this is non-static func in a"); }//file b.c#include <stdio....
classPoint{public:voidinit(){output();}staticvoidoutput(){}};voidmain(){Pointpt;Pt.init();pt.output();} 编译通过。 结论4:类的非静态成员函数可以调用用静态成员函数,但反之不能。 使用类的静态成员变量。 #include<stdio.h>classPoint{public:Point(){m_nPointCount++;}~Point(){m_nPointCount--...
就可以用到静态数据成员. 在这里面, static既不是限定作用域的, 也不是扩展生存期的作用, 而是指示变量/函数在此类中的唯一性. 这也是”属于一个类而不是属于此类的任何特定对象的变量和函数”的含义. 因为它是对整个类来说是唯一的, 因此不可能属于某一个实例对象的. (针对静态数据成员而言, 成员函数不管是...
classPoint{public:voidinit(){}staticvoidoutput(){}};voidmain(){Pointpt;pt.init();pt.output();} 编译通过。 结论2:类的对象可以使用静态成员函数和非静态成员函数。 在类的静态成员函数中使用类的非静态成员。 #include<stdio.h>classPoint{public:voidinit(){}staticvoidoutput(){printf('%d\n',m_...
字符串常量属于静态存储类别(static storage class),这说明如果在函数中使用字符串常量,该字符串只会被存储一次,在整个程序的生命期内存在,即使该函数被调用多次。 2.字符串数组和初始化 定义字符串数组时,必须让编译器直到需要多少空间。一种方法是用足够空间的数组存储字符串: ...
注:+(void)前的加号,就表示这一个是类方法(static 方法),而-(void)表示这是一个实例方法 实现部分: 注意:下面的 -(id) init 即为构造函数。对应的,还有一个-(void)dealloc方法用来释放资源(类似于析构函数或c#中的dispose()方法)-注:dealloc方法以后在内存管理中详细学习,这里先不管它。
方法前面的 +/- 号代表函数的类型:加号(+)代表类方法(class method),不需要实例就可以调用,与C++ 的静态函数(static member function)相似。减号(-)即是一般的实例方法(instance method)。 这里提供了一份意义相近的C++语法对照,如下: classMyObject:publicNSObject{protected:intmemberVar1;// 实体变量void*membe...
usingSystem;namespaceConsoleEnum{classhost{ [STAThread]staticvoidMain(string[] args){// Create an array of Car objects.Car[] arrayOfCars=newCar[6] {newCar("Ford",1992),newCar("Fiat",1988),newCar("Buick",1932),newCar("Ford",1932),newCar("Dodge",1999),newCar("Honda",1977) };//...
classCMyClass{public:explicitCMyClass(intiBar)throw(){ }staticCMyClassget_c2(); };intmain(){ CMyClass myclass =2;// C2440// try one of the following// CMyClass myclass{2};// CMyClass myclass(2);int*i;floatj; j = (float)i;// C2440, cannot cast from pointer to int to ...