According to the answer https://stackoverflow.com/a/11842442/5835947, if you code like this, function parameter Bubble * targetBubble
will be copied inside the function.
bool clickOnBubble(sf::Vector2i & mousePos, std::vector<Bubble *> bubbles, Bubble * targetBubble) {
targetBubble = bubbles[i];
}
However, I made a test, finding that a pointer as function parameter will be the same as the outside one, until I changed it's value:
// c++ test ConsoleApplication2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "c++ test ConsoleApplication2.h"
using namespace std;
#include<iostream>
int main()
{
int a= 1;
int* pointerOfA = &a;
cout << "address of pointer is" << pointerOfA << endl;
cout << *pointerOfA << endl;
func(pointerOfA);
cout << *pointerOfA << endl;
}
void func(int *pointer)
{
cout << "address of pointer is " << pointer <<" it's the same as the pointer outside!"<<endl;
int b = 2;
pointer = &b;
cout << "address of pointer is" << pointer <<" it's changed!"<< endl;
cout << *pointer<<endl;
}
Output is below:
address of pointer is0093FEB4
1
address of pointer is 0093FEB4 it's the same as the pointer outside!
address of pointer is0093FDC4 it's changed!
2
1
So, the truth is that, a pointer as function parameter will not be copied, until it's changed, right? Or am I missing something here?