Virtools脚本语言(VSL)教程 - 结构
来源:互联网 发布:oracle批量update数据 编辑:程序博客网 时间:2024/05/18 05:45
结构定义的语法
结构类型是一种用户定义的复合类型。结构可由一些不同类型的成员组成。
struct structName { memberList };
实例:
struct Person
{
String name;
int age;
float weight;
};
每个结构成员的类型可以是基本类型、其它VSL结构或C/C++结构/类。这些类型的数组成员也被支持。
struct Car
{
Entity3D entity;
Person passengers[4];
float weight;
Vector velocity;
};
要访问结构的成员,你得使用 "." 运算符。
void SetPerson(Person p, String name, int age, float weight)
{
// Set name
if (name == "")
p.name = "Unknown";
else
p.name = name;
// Set age
if (age < 0)
age = 1;
p.age = age;
// Set weight
if (weight < 0)
weight = 40;
p.weight = weight;
}
void main()
{
Car car;
car.entity = null;
car.velocity.Set(0,0,0);
car.weight = 1500;
// Set persons
for (int i = 0; i < 4; ++i)
{
String name("Passengers");
name += i;
SetPerson(car.passengers[i], name, rand()*20+20, rand()*30+60);
// Add person weight to the car weight
car.weight += car.passengers[i].weight;
}
}
在结构上使用运算符 =, == 及 !=
当在结构上使用 =, == 及 != 运算符时,VSL编译器为每个具有这种被重载运算符的成员调用合适的运算符。
对于那些没有重载运算符的成员,这个代码会产生:
Operator =
Code is generated to copy memory.
Operator == and !=
Code is generated to compare memory.
实例:
Person p1;
Person p2;
String name("john");
SetPerson(p1, name, 30, 75);
p2 = p1;
等价于:
p2.name = p1.name; // call the String operator =
p2.age = p1.age;
p2.weight = p1.weight;
p2 == p1;
等价于:
p2.name == p1.name; // call the String operator ==
p2.age == p1.age;
p2.weight == p1.weight;
p2 != p1;
等价于:
p2.name != p1.name; // call the String operator !=
p2.age != p1.age;
p2.weight != p1.weight;
- Virtools脚本语言(VSL)教程 - 结构
- Virtools脚本语言(VSL)教程 - 语句
- Virtools脚本语言(VSL)教程 - 函数
- Virtools脚本语言(VSL)教程 - 枚举
- Virtools脚本语言(VSL)教程 - 核心语言特性
- Virtools脚本语言(VSL)教程 - 使用 GUID
- Virtools脚本语言(VSL)介绍
- 理解Virtools脚本语言(VSL)
- Virtools脚本语言(VSL)教程 - 值、类型与变量
- Virtools脚本语言(VSL)教程 - 表达式与运算符
- Virtools脚本语言(VSL)教程 - 全局变量bc与ac
- 通过实例学习Virtools脚本语言VSL - 合并字符串
- 通过实例学习Virtools脚本语言VSL - 解二次方程
- 通过实例学习Virtools脚本语言VSL - 解析字符串
- 通过实例学习Virtools脚本语言VSL - 从数组读取字符串
- 通过实例学习Virtools脚本语言VSL - 将物体排列于圆周上
- 通过实例学习Virtools脚本语言VSL - 检索和修改数组的数据
- Virtools教程系列
- gcc 汇编器生成汇编代码
- 第六章:异常处理
- xmlDoc
- Math函数性能比较
- C# OFFICE 2007控件 第三方控件DotNetBar
- Virtools脚本语言(VSL)教程 - 结构
- Siebel Tools: How to program Browser Script & Server Script
- java连接DBF
- webservice接口测试
- 字符,字节和编码
- 第七章:常用类
- Siebel Admin: How to find the Component that associated with Application
- 090907(星期一)项目进展:学习MFC的消息映射
- 头文件和源文件的关系