| zoemayne (15) | |||
|
im trying to read some C++ code but i dont understand the constructor can someone please explain what the ampersand is doing(i know its not the address operator in this case I assume its same as && in java) in this constructor. I dont see the variable s is created any where so I guess that the constructor is creating a const string s?? im coming from programing in java so ..... I also dont understand how is it possible the shapetype variable is being used before it is declared. maybe someone can comment this code up for me please explain it from a java programmer perspective... thanks in advance!
| |||
|
|
|||
| eker676 (427) | |||
|
I know very little java but I understand some of that. Ampersand is the character used to define a reference in this case.
Sorry if that's a bad explanation. Maybe someone else knows both languages good and can explain it. | |||
|
Last edited on
|
|||
| Fififox (9) | |||
|
Hi ! Hope this helps:
| |||
|
Last edited on
|
|||
| Bazzy (6281) | |
Constructor:const string & s The & means that the argument is passed by reference, not by value. Doing so you avoid to copy the object passes. const means that even if the argument is passed by reference, it can't be modified in the function: shapeType( s ) this is the initializer list, the class members are initialized with the value given (in this case shapeType is initialized with the value of s (the constructor argumentconst string & getType( ) const
const string & The return type is a const reference of a string (same meaning of the argument in the constructor but in this case is the return type)getType( ) const const means that the function doesn't modify the class instance (so it can be called for variable of type const Shape )virtual double getArea( ) const = 0;
virtual means that the function can have overrides (different bodies) on derived classconst is same as above= 0; means that the virtual function hasn't the body in the base class, this makes the class abstract and it can't have objectsvirtual void print( ostream & out ) constEverything is as was for the function except for the fact that this has the body in the base class ostream & out streams don't have the copy constructor so they must be passed by referenceIn classes all members are visible not depending in which order they were declared Some documentation: http://www.cplusplus.com/doc/tutorial/functions2.html http://www.cplusplus.com/doc/tutorial/polymorphism.html [Edit] Many answers at the same time... | |
|
Last edited on
|
|
| zoemayne (15) | |||
ok i understand. basically in the constructor a constant string s object is created. than it is passed to a shapetype variable by reference.. its easier to read in this form
and i didnt know that the spacing with the ampersand didnt matter ie.. Shape(const string & s) = Shape(const string& s)thanks for the link to the functions2 it all helps... | |||
|
Last edited on
|
|||
| eker676 (427) | |
|
I don't think the space matters. It's just style. i.e. Some people write pointers like this int *ptr = &a;and some people write them like this int* ptr = &a;
| |
|
Last edited on
|
|
| zoemayne (15) | |||
|
true, the spacing doesn't matter both versions compiled and run fine. another question: in this code is the << being passed by reference as an ostream type and overloaded at the same time?
| |||
|
|
|||
| grcunning (117) | |
|
I think you are having difficulty understanding how arguments are passed. If you pass by value (string s), a copy of the original argument is made, and all changes in the function are made on that copy. When the function exits, all those changes are lost, which means the original variable isn't changed. This works fine on strings or basic variables, but when you pass large objects, making a copy can slow things down. When you pass an argument by reference ( string &s), a copy doesn't have to be made, all changes are made on the original variable. This speeds up things, but the original variable can get messed up. When you pass an argument by constant reference(const string &s), you get the best of both worlds, you get the speed of passing by reference( a copy doesn't have to be made), but you don't have to worry about the parameter getting changed by the function(consistency). This is what Bazzy was telling you, I just thought a simpler explanation would help. | |
|
|
|
| seymore15074 (449) | ||
|
In C++, the example posted serves as a blue-print for a type. Alone, there are no class objects it only describes how objects of this class (once created) will "act". It just so happens that the class is an abstract base class (identified by the pure virtual method). Classes that only contain pure virtual methods are similar to the interface concept in Java.
The use of the spoken language is very important here. Remember that the class represents a type, not an object. In the constructor, the pass-by-const-reference argument means to NOT create an object; it expects one to already be in existence and it can work on it directly (although, because of the const keyword, it is read-only). I think const is similar to the final keyword in Java. Hope that helps. | ||
|
|
||
| zoemayne (15) | ||
eker676 you said that:
are you sure you meant to say that? I understand pretty much everything here but I dont agree with the above quote. how is this print function constant the keyword const isn't in front of the print function so I dont see how its a constant function... anyone....?? | ||
|
Last edited on
|
||
| eker676 (427) | |
| Now that I think about it... I am not sure if it means the return data is constant or the function is constant. | |
|
|
|
| seymore15074 (449) | |
| const used after the declaration of a function in that regard simply means that the member function will not modify any of the class members. It is a function that may be used in a read-only context. | |
|
|
|
| zoemayne (15) | |||
can someone comment this code up for me?
an important thing i figured is that the less is being used and can be used to represent <,>,<=,&>=. The main thing i want to understand is templates. I'm new to templates. Comments from multiple users are welcome. Comment as much as you know. | |||
|
|
|||
| LacViet (82) | |
| Thank for the good insight guys. I have never even thought of doing that. Just pass everything either as a reference, or const reference. O_o lolz. | |
|
|
|