Quick question on Pointers!

Hey everyone,

Absolute beginner here. I would like to ask a quick question on pointers!

Am I right to say that if for eg. I have ClassA, ClassB, ClassC and when I am in a FunctionONE() of ClassB and would like to get access to ClassA's variables, I would do ClassA *pClassA; and when I am in FunctionTWO() of ClassB I should declare it again?? ClassA *pClassA;???

Is it correct to keep declaring a new pointer of the same ClassA over and over again??

1
2
3
4
5
6
7
8
9
10
11
12
13
//ClassB.cpp

void ClassB::FunctionONE()
{
    ClassA *pClassA;
    ABC = pClassA->GetABC();
}

void ClassB::FunctionTWO()
{
    ClassA *pClassA;
    XYZ = pClassA->GetXYZ();
}


Many thanks!!
To use a ClassA's member functions you need a ClassA object. The pointers in your code doesn't point to any valid objects so the code is likely to behave strange or crash.
Your example is too simplistic. It's also flawed.

Pointers by themselves don't do anything. They have to actually point to something. Attempting to use a pointer without pointing to anything will do "bad things" (if you're lucky, it will crash the program).

For instance, your FunctionONE makes no sense:

1
2
3
4
5
void ClassB::FunctionONE()
{
    ClassA *pClassA;  // a pointer, but what does it point to?
    ABC = pClassA->GetABC();  // whose ABC are you trying to get?
}


The 2nd question there is key. Whose ABC are you trying to get? Which bring up the bigger conceptual issue I think you're having: the difference between classes and objects.


A class is sort of like an outline for a "thing", whereas an object actually IS a "thing". For example, string is a class, and you can create individual strings by creating objects:

1
2
3
string foo;  // string is the class, but foo is the object.  foo actually is a string

int len = foo.length();  // whose length are we getting?  foo's length! 


Moving from that, when you make a pointer, you don't actually have an object. It's just a means by which to access another object:

1
2
3
4
5
6
7
8
string* ptr;

len = ptr->length();  // whose length are we getting?  It can't be ptr's because ptr isn't a string - it's a pointer
// in order for this to mean anything, ptr has to actually point to a string:


ptr = &foo  // now, ptr points to foo
len = ptr->length();  // now whose length are we getting?  foo's length! 


So your original question is somewhat flawed. You can't just "gain access" to ClassA's variables just by making a pointer. You have to specify which ClassA object you want to access. Which object's variables do you want to access?
HI both,

Thank you very much for your reply. Sorry! my beginners code is totally flaw and you guys pointed out right away!

But here it is I have amended the code... is it better now???

Back to my original question once again, is it correct to keep declaring a new pointer of the same ClassA over and over again??

Many thanks!!!

1
2
3
4
5
//ClassA.h
int GetABC(int i)
{
    return ABC;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
//ClassB.cpp

void ClassB::FunctionONE()
{
    ClassA *pClassA; // declared ptr for ClassA
    abc = pClassA->GetABC(); // get ABC from ClassA and store it in abc of ClassB
}

void ClassB::FunctionTWO()
{
    ClassA *pClassA; // declared ptr for ClassA again
    xyz = pClassA->GetXYZ(); // get XYZ from ClassA and store it in xyz of ClassB
}


1
2
3
4
5
6
7
8
9
10
11
12
13
// in main.cpp

int main()
{
    ClassA cA;
    ClassB cB;

    // Step 1 get ABC from ClassA
    cA.GetABC();

    // Step 2 get XYZ from ClassB
    cB.GetZYX();
}
You might want to reread my previous post more carefully. You still have the exact same problem.

1
2
3
4
5
void ClassB::FunctionONE()
{
    ClassA *pClassA;
    abc = pClassA->GetABC();
}


pClassA does not point to anything, so whose ABC are you trying to get? You can't just say "give me ClassA's ABC" because there might be multiple ClassAs. That'd be like saying "give me Person's weight". Which person? You have to tell the compiler which object you want to act on.

It looks like you might be wanting to get cA's ABC (since that's the only object you created in your example). If that's the case, you would need to make pClassA point to cA somehow. Typically this is done by passing it as a parameter to the function. Here's an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
void ClassB::FunctionONE(ClassA* pClassA)
{
  abc = pClassA->GetABC();  // get the ABC of whichever ClassA object was passed to this function
}

int main()
{
  ClassA cA;
  ClassB cB;

  cB.FunctionOne( &cA );  // pass a pointer to cA as the parameter
    //  this will make pClassA point to cA within FunctionONE
}
Last edited on
You're a champion Disch!

Thank you very much for clearing things out! This is the yet the best and simplest explanation so far that made me understand pointers despite after having to flip through so many books and online tutorials!!

So.. am I right to say that each time I would like to get data from ClassA I would have to declare a pointers between the brackets???

1
2
3
4
5
6
//in ClassB.h

void FunctionONE(ClassA* pClassA);
void FunctionTWO(ClassA* pClassA);
void FunctionTHREE(ClassA* pClassA);
...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//in ClassB.cpp

void ClassB::FunctionONE(ClassA* pClassA)
{
    abc = pClassA->GetABC();
}

void ClassB::FunctionTWO(ClassA* pClassA)
{
    xyz = pClassA->GeXYZ();
}

void ClassB::FunctionTHREE(ClassA* pClassA)
{
    def = pClassA->Getdef();
}


1
2
3
4
5
6
7
8
int main()
{
  ClassA cA;
  ClassB cB;

  cB.FunctionOne( &cA );  // pass a pointer to cA as the parameter
    //  this will make pClassA point to cA within FunctionONE
}
Topic archived. No new replies allowed.