Understanding Pointers

Hi,

I'm having trouble getting the pointers idea (and I thought I finally had it right...).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
MyClass myClass; //This class only has a Letter* letters variable
myClass.StartClass(10); //This calls a letters = new Letter[10];

Letter myLetter; //A class with a single char value in it
myLetter.SetChar('a'); //Saving 'a' to it's value

myClass.SetLetter(0, &myLetter); //I'm assigning letters[0] the myLetter address

myLetter.GetChar(); //Returns 'a'
myClass.GetLetter(0)->GetChar(); //Returns 'a'

myLetter.SetChar('b');

myLetter.GetChar(); //Return 'b'
myClass.GetLetter(0)->GetChar(); //Returns 'a' -> I was expecting 'b' 


The way I'm assigning the letters array is: (tried two ways)

1
2
3
4
void MyClass::SetLetter(int index, Letter &letter)
{
   letters[index] = letter;
}


and...

1
2
3
4
void MyClass::SetLetter(int index, Letter *letter)
{
   letters[index] = *letter; //This gives me the value only right?
}


Why didn't it work, what am I understanding wrong here?
I was trying to make an array that holds the reference of a bunch of other variables that will be spread around the code, so I could get them easier with the array.
How is `letters' array defined?
Because both your methods

void MyClass::SetLetter(int index, Letter &letter)
{
letters[index] = letter;
}

and

void MyClass::SetLetter(int index, Letter *letter)
{
letters[index] = *letter; //This gives me the value only right?
}

create copies of arguments.

For example in the first method letter is copied to letters[index] The same is valid and for the second method.

Consider a simple example

1
2
3
int x = 10;
int *p = &x;
int y = *p;


Now y contains value 10. If you change x, for example,

x = 20;

the value of y is not changed.
1
2
3
Letters* letters;

letters = new Letter[int];


I think you're confusing yourself with the parameter names.

Ugh. Confused myself. Vlad is correct. Using a method that takes a reference does not somehow magically make the type contained in MyClass a reference type.
Last edited on
Setting it to Letter is fine as long as Letter has an assignment operator, no?
I thought that specifying the argument "letter" with a & before means I'll be using it's address, not it's value...

Though I'm not sure what "array[index] = address" would do.

Thanks for the replies, I'm going to study and test more on pointers.
I managed to get what I wanted, but I'm wondering if it's the best way.

I'm trying to make a program that will read a 3D object info, and so far I have:

class Vertex
- has x, y, z values
class Triangle
- has the address of 3 Vertexes
class Model
- has Vertex* vertexes = new Vertex[int], which I will only know during runtime
- has Triangle* triangles = new Triangle[int], which I will only know during runtime

The reason is that I'm going to use triangles to draw, and each must have 3 vertexes' info to be properly show.
The drawing code will loop through each triangle, and draw all of their 3 vertexes.
The vertexes (array), however, are subjected to change from other factors such as Model position and effects, but this wasn't being reflected before - because the values I had in each Triangle was copied.

I managed to get around with:

1
2
3
4
5
6
7
8
9
10
11
Triangle** ptrTriangles; //Creating an array of pointers?
ptrTriangles = &triangles; //Setting it to reference the original triangle array

ptrTriangles[0]->SetVertexes(&vertexes[0], &vertexes[1], &vertexes[2]); //Setting the first 3 vertexes to ptrTriangle, which will be triangles

vertexes[0]->X; //Returns 1
ptrTriangles[0]->GetVertex(0)->X; //Returns 1

vertexes[0]->X = 10;
triangles[0]->GetVertex(0)->X; //Returns 10!
ptrTriangles[0]->GetVertex(0)->X; //Also returns 10 


Is there a better way for this? Maybe without needing the second array of pointers?
Isn't my original "triangles" an array of pointers already?
Topic archived. No new replies allowed.