Let's start with the class. It's written in a compact style which is not helpful. If someone wrote this I'd zap them with a taser and tell them to do it again properly.
1 2 3 4 5 6 7 8 9 10
|
class A {
public:
float v;
A() : v(1.0) {}
A(A &a) : v(2.0) {}
A(float f) : v(3.0) {}
float get() {
return A::v;
}
};
|
Line 1: This is a new kind of object that can be created. These objects are known b the type identified "A".
Line 2: Everything that follows is accessible (so functions can be called, variables can be read/written) by things that are
not of type "A"
Line 3: This class contains a float variable, named v
Line 4: This is a constructor function, all written on one line. You can tell it's a constructor function because it has no return type and the function has the same name ("A") as the class. This function has an initialiser list
v(1.0)
so when this constructor is called, v is set to 1.0
Line 5: This is a constructor function, all written on one line. You can tell it's a constructor function because it has no return type and the function has the same name ("A") as the class. This constructor accepts a reference to an object of type "A". This is the copy constructor, although it should really use a const reference. This constructor will be called if we make an object of type A by copying an existing object of type A.
http://www.cplusplus.com/forum/articles/18749/
Line 6: This is a constructor function, all written on one line. You can tell it's a constructor function because it has no return type and the function has the same name ("A") as the class. This constructor accepts a float parameter (although it doesn't actually do anything with that float parameter). This constructor also has an initialiser list.
Line 7-9: This is a class function. It returns the class variable v.
This line creates three objects of type A. This first one is created using the constructor on line 4. This second one is created using the constructor on line 5. This third one is created using the constructor on line 6.