Prev | Index | Next |
At their most basic, classes in C++ can be used the same as structs in C - a container to hold multiple values:
class Point { int x; int y; } p;
...
p.x := 1; p.y := 2;
This works because class members are public by default in C*, unlike C++. public, protected, and private blocks can also be declared in the class to restrict access, like in C++.
When a variable of class type (an object) is declared, the values of its members are initially undefined. It's an error to use a member before it has been initialized.
Note that classes can have a list of objects declared at the end of the class body ("p" in the example above). This is especially useful for declaring singletons - there are future plans to allow the class name to be omitted. It also means that class bodies must always end with a semicolon, to prevent parsing difficiulties.
A member that's declared static applies to the class, like in C++. Classes, objects, and pointers are all dereferenced with the . operator in C*, where in C++ they were variously dereferenced with ., ::, and ->.
Members can also be declared friend, which works the same as in C++. Friend functions are really global functions that are declared within a class, but which can access the private data in objects of the class. Friend classes are not yet implemented.
In C++, member function implementations usually went outside the class body in a separate file, with the name being qualified. In C*, implementations go in a second class body, called the class implementation (which normally also goes in a separate file). For example:
class Hello { function hello(); //Declaration }; class Hello { function hello() { //Definition << "Hello World"; } };
The advantage of this scheme is that you don't have to repeatedly qualify function implementations with the class name Hello, which becomes especially burdensome in C++ when the class is a template.
Of course, "this" is also present, and, like in C++, has pointer type. (But members are still dereferenced as this.x and this.y, since the -> operator has been replaced with ".".)
const member functions, which prevent "this" from being modified in their body, are declared with the "const" keyword at the beginning:
const function hello();
Namespaces
Namespaces work the same as in C++, except that members are dereferenced with ".", rather than "::". A namespace is just a class where all the members are static. That is, it provides a nested scope to refer to a collection of symbols, but you don't create objects of the type of a namespace. This is useful if you have a library and you want all the classes and functions in the library to be interoperable with any other library, without worrying about the other library using names you've already used. If each library is within a namespace, the only way they could clash is if they both name the namespace the same.
namespace my_library { function f(); class C { ... }; }; //Now there's no danger if there's another thing named f or C elsewhere in the program my_library.f(); //Call f (my_library::f doesn't work anymore)
Prev | Index | Next |