openFrameworks 学习笔记(二)

C++ 中的类是怎么工作的

头文件(.h文件)中主要包含下面的东西:

任何预处理语句,以防止有多个标题的定义
任何包含其他类的语句
任何类扩展语句
任何局部变量的类
将包含在类的任何函数的原型
这些函数和变量的安全设置

(.cpp)文件包含下面的东西

一个include语句引用.h文件
所有的代码中的函数原型

注意:下面程序结果是输出为2。

class Test
{
  int num=0;
}
 
void setup()
{
Test test = new Test();
test.num=1;
addOne(test);
print(test.num);
}
 
void addOne(Test test)
{
  test.num++;
}

类的另外使用方法,ofBall为ofBall.h中定义的类

//testApp.h

//teatApp.cpp

也可以这样

//testApp.h

//teatApp.cpp

指针什么的用法

//ofBall::ofBall (float _x, float _y, int _dim)

ofBall::ofBall(float _x, float _y, int _dim)
{
    x = _x;
    y = _y;
    dim = _dim;
 
    speedX = ofRandom(-1, 1);
    speedY = ofRandom(-1, 1);
}

//testApp.h

星号表明将为myBall保留内存

//testApp.cpp

如果是有两个星星的话

//testApp.h

//testApp.cpp

原文地址:https://www.cnblogs.com/hqqxyy/p/2977736.html