一、类和结构体的区别如下:
1、类的成员默认是private私有的
(1)错误的为:
#include <iostream>
#define LOG(x) std::cout << x << std::endl;
class player
{
int x, y;//如果我没有声明它是public,那么默认是private
int speed;
void move(int xa, int ya)
{
x += xa * speed;
y += ya * speed;
}
};
void main()
{
player player;
player.move(1, 1);//方法无法访问类的私有变量x,y。
}
当运行时会出现以下2个错误提示:
错误一:函数 “player::move”: 不可访问
错误二: “player::move”:无法访问private成员
(2)错误的为:
因为move方法默认是私有的private,只有类的内部的其它方法才能访问move。