I'm 99% sure that the answer to this dazzling no . Please acknowledge my suggestion that the following code will cause a memory leak.
Data &getData()
{
Data *i = new Data();
return *i;
}
void exampleFunc()
{
Data d1 = getData();
Data d2;
}
Note that this is a simplified example, so you obviously would not use this code. Therefore, do not point out this.
Update 1:
To add to this, what if I assign a pointer to a link? In this case, I assume that the data is not copied ...
Data &getData()
{
Data *i = new Data();
return *i;
}
void exampleFunc()
{
Data &d1 = getData();
delete &d;
}
Update 2:
I think in order to answer my own question (in Update 1), the following code proves that assigning a link to a link does not cause a copy ...
#include <iostream>
#include <string>
using namespace std;
class Data
{
public:
string mName;
Data(const string &name) : mName(name)
{ cout << mName << " default ctor" << endl; }
Data(const Data& other)
{
mName = other.mName + " (copy)";
cout << mName << " copy ctor" << endl;
}
~Data()
{ cout << mName << " dtor" << endl; }
static Data &getData(const string &name)
{
Data *d = new Data(name);
return *d;
}
};
int main()
{
cout << "d1..." << endl;
Data d1 = Data::getData("d1");
cout << "d2..." << endl;
Data d2("d2");
cout << "d3..." << endl;
Data &d3 = Data::getData("d3");
cout << "return..." << endl;
return 0;
}
Gets the following result ...
d1...
d1 default ctor
d1 (copy) copy ctor
d2...
d2 default ctor
d3...
d3 default ctor
return...
d2 dtor
d1 (copy) dtor
Eric Melski ( 2 - exmaple).