Was attempting to do some small program earlier, and came across the following type that was required for a function, but couldnt seem to get it to work: const Class & (more specifically const string &) is that a reference to a constant string? if i have a string how do i convert it into a const string & if i have a const string how do i convert it into a const string &
This maybe? I'm not familiar with working with reference types, so I'm not sure how to answer your question, but maybe you can narrow your question/searches now.
Its a constant reference. So instead of passing what could be a potentially large class by value, you can pass the reference to it. This speeds up execution. It should only be used if the function is not modifying the data that the reference is pointing to. If you've got the string, and you want to pass it in this way : void function(const string& a) { a.func(); //do whatever you need to do with a }
What would be the difference between a reference and a pointer, and why would one be used over the other in which situations?
A reference and a pointer are very similar. They both point to the address of the object. There is a slight cost to dereferencing pointers, but they can be cast to other classes where as a reference cannot. A lot of the times they can be used interchangeably and is just a matter of preference choosing between references and pointers. Example: Code: //classData is a member of MyClass void MyClass::GetIntRef( int& value ) { value = classData; } void MyClass::GetIntPtr( int* value ) { *value = classData; } //usages MyClass c; int x; c.GetIntRef( x ); c.GetIntPtr( &x ); The functions are for all intents and purposes equivalent, but using the pointer version it is more obvious that value will be receiving data. More info: http://www.parashift.com/c++-faq-lite/references.html
Oops, forgot I had posted this. Thanks for your help. Guess I'll have another attempt at this one day in the future.