2 questions releated to objects

Hi I can' t understand pointers to objects. Can you give me some examples?
Also I want to know why to use a reference in this code.
1
2
3
4
void board (const vector<char> &board)
{
//some code
}
I want to know why to use a reference in this code.
Copying a vector is an expensive operation, so we pass by reference to avoid it. Const reference so we won't accidetally change values in vector.

I can' t understand pointers to objects. Can you give me some examples?
What exactly do you have problem with? Pointer to objects differs little from pointers to primitive types.
Hi,

An object is simply a variable, nothing special. :)
A pointer (which is a variable, itself) will contain the address of that object, i.e. the location of where that object is in memory.

Also, to be more detailed, whenever you pass a vector by value, like this:
void board(const vector board) //notice there is no &

The ENTIRE vector is copied and that is a very expensive operation. When you pass by reference (that's the actual term), only the address of the vector is passed to that function, which can then use that reference to access the vector.

Passing a reference is simply passing a number (the address) which is way less expensive than copying an entire vector object!

If you need more help, feel free to ask me at:
sparkprogrammer@gmail.com

Hope I helped,
Joe :)

Last edited on
Thank you all . Especially to you little captain . Now I understand it :)
No worries :)
Topic archived. No new replies allowed.