Understanding references

Hi.
I'm trying to understand references in order to know when I should use references instead of pointers to objects.
I've created a basic example so I can expose my doubt:

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cstdlib>
#include "Class1.hpp"

using namespace std;

void testFunction()
{
	Class1 class1;
	
    Class2 class2Instance = class1.createClass2();
    
    cout << "class2Instance has Id " << class2Instance.getId() << endl;
}

int main(int argc, char *argv[])
{
	testFunction();
    system("PAUSE");
    return EXIT_SUCCESS;
}


Class1.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "Class2.hpp"

using namespace std;

class Class1
{
	
    public:
	Class1();
	~Class1();
	Class2& createClass2();
	
};


Class1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "Class1.hpp"

Class1::Class1()
{
	cout << "Constructing a Class1 instance" << endl;
}

Class1::~Class1()
{
   cout << "Deleting a Class1 instance" << endl;
}

Class2& Class1::createClass2()
{
	cout << "Creating Class2" << endl;
	Class2 localClass2Instance;
	return c;
}


Class2.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

class Class2
{
	
	static int refCount;
	
	int id;
	
	public:
	Class2();
	~Class2();
	
	int getId();
};


Class2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "Class2.hpp"

int Class2::refCount = 1;

Class2::Class2()
{
	id = refCount;
	cout << "Constructing a Class2 instance with id " << id << endl;
	refCount += 1;
}

Class2::~Class2()
{
	cout << "Deleting  a Class2 instance with id " << id << endl;
}

int Class2::getId()
{
	return id;
}


output
Constructing a Class1 instance
Creating Class2
Constructing a Class2 instance with id 1
Deleting a Class2 instance with id 1
class2Instance has Id 1

Deleting a Class2 instance with id 1
Deleting a Class1 instance

The lines in bold are what I don't understand. Class1::createClass2() function creates an object of type Class2. After that returns its reference, so know class2Instance and localClass2Instance are the same object. But when Class1::createClass2() returns, localClass2Instance is deleted, as shown in first line in bold. So, why I can still have access to the object, as shown in second line in bold?.

Thanks guys.
You really want to avoid returning a reference to something you created inside a function. The second the function exits the variable you are referencing is destroyed and you are left holding a dangling reference.

A good place to use references is for parameters to functions.
1
2
3
4
int func(const MyType& mytype)
{
    // Use mytype.
}

Don't use const when you want to modify the variable you pass in:
1
2
3
4
int func(MyType& mytype)
{
    // Change mytype.
}

Don't bother using a reference with basic types like int, float etc...
but why am I still able to call its method? is this a kind of "undefined behavior"?.
Thanks for your recommendation. It'll make my code a lot cleaner than that I would get using pointers.
Your last line makes me suppose that you say that because what is copied is at least the same size of the value stored in an int, float, etc.
So, suppose I call your function like this:
1
2
MyType* var = new MyType;
int funcValue = func(*var);


What is it copied from *var to mytype argument? is it its memory address?.

Thanks for your reply.
How the compiler manages the reference is undefined in the standard. It is free to use whatever mechanism it wants. However I have heard that reference parameters are often implemented as pointers under the covers. So in your example, where you de-reference your pointer to get to the object and then pass the object by reference, a new pointer might be be generated under the covers. However that would not necessarily be less efficient that passing by pointer because if you passed by pointer then the pointer would be copied. On top of that the optimiser may well erase any unnecessary steps.
Last edited on
Thanks for your explanation. It really helps me out on this.
Topic archived. No new replies allowed.