Having a hard time picking up pointers...

I understand how pointers point to a memory address, that much makes sense. I've used pointers to simple variables and modified those, or worked with them. My problem is understanding how objects and pointers and classes all work together. Once I get a class made, then make a pointer to it, and try to use it things get hazy. Take that a step further and toss in some inheritance and I'm lost.

I've read over the pointer page on here, and on another site, and gone through my C++ books chapter a few times, it just doesn't convey it very well. Does anyone have any recommendations on books or something to go over to try to clarify things?

Thanks!
Everyone has a hard time picking up pointers. I suppose experience is the best cure, but since nobody has time for that :), I'll suggest visualising it.

Example 1:
1
2
int a[3] = {7, 2, 13};
int* ptr = a;
becomes
name    : ... |   a   |   _   |   _   |  ptr  | ...
value   : ... |   7   |   2   |   13  | 0xf00 | ...
address : ... | 0xf00 | 0xf01 | 0xf02 | 0xf03 | ...
It could be healthy to draw an arrow from ptr to a.

Example 2:
1
2
3
4
5
6
7
8
9
10
11
class List{
public:
   int val;
   List* next;
};

List* ptr = new List;
ptr->val = 1;
ptr->next = new List;
ptr->next->val = 2;
ptr->next->next = 0;
becomes
name    : ... |  ptr  | ... |  val  |  next | ... |  val  |  next | ...
value   : ... | 0xba2 | ... |   1   | 0xf00 | ... |   2   |   0   | ...
address : ... | 0x123 | ... | 0xba2 | 0xba3 | ... | 0xf00 | 0xf01 | ...


Example 3:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Foo{
public:
   int a;
};

class Bar : public Foo{
public:
   Bar(int fa, int bb) {
      a = fa;
      b = bb;
   }
   int b;
};

Foo* ptr = new Bar(5, 7);
becomes
name    : ... |  ptr  | ... |   a   |   b   | ...
value   : ... | 0xabc | ... |   5   |   7   | ...
address : ... | 0x555 | ... | 0xabc | 0xabd | ...
Note here that Foo* only knows about member a. If it was a Bar*, you could access member b too. That does not affect how the object looks in memory or what the value of ptr is, though (unless multiple inheritance is involved).
hamsterman has some great examples here but am I right in thinking that you mean you're having trouble understanding polymorphism? If that's the case I'll leave this here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>

inline void pause()
{
    std::cin.sync();
    std::cin.ignore();
}

struct SHAPE
{
    public:
        SHAPE ()  {}
        SHAPE(int A, int B): Side_A(A), Side_B(B)  {}
    
    virtual int Area()
    {
        return Side_A * Side_B;   
    }
          
    int Side_A;
    int Side_B;
};

struct SQUARE: SHAPE 
{
    public:
        SQUARE(int A, int B)
        {
            Side_A = A;
            Side_B = B;    
        }
};//SQUARE uses the definition of Area() inherited from SHAPE


struct TRIANGLE: SHAPE
{
    public:
        TRIANGLE(int A, int B)
        {
            Side_A = A;
            Side_B = B;
        }
    
    int Area()
    {
        return (Side_A * Side_B) / 2;
    }
    
};//TRIANGLE redefines the Area() function

int main(int argc, char *argv[])
{
    SHAPE* Shapes[2];
    
    Shapes[0] = new SQUARE(6, 6);
    Shapes[1] = new TRIANGLE(6, 6);
    
    //C++ doesn't care which one you use
    std::cout << "Area Of Square Is: " << Shapes[0]->Area() << std::endl << "Area Of Triangle Is: " << Shapes[1]->Area();
    
    delete Shapes[0];
    delete Shapes[1];

    pause();
    return EXIT_SUCCESS;
}


Compiled and working with Mingw.

EDIT: Fixed the size of the SHAPE pointer array, Deleted the array at the end of the program (even though in a simple example like this the garbage collector would do just fine in this case, deleting them your self is the correct way to do this). Thanks Grey Wolf!
Last edited on
closed account (z05DSL3A)
1
2
3
4
SHAPE* Shapes[1];
    
Shapes[0] = new SQUARE(6, 6);
Shapes[1] = new TRIANGLE(6, 6);
0_0
Did I do something wrong there?
closed account (z05DSL3A)
An array of one SHAPE* to store two shape pointers!


Edit:
You didn't delete the newed shapes either.
Last edited on
AARRGGGGHHH!!! I realized that as soon as I hit submit and left my desk. One of the hazards of posting at work I guess.
delete [] Shapes;
That's still not right. It's not Shapes that you allocated, but Shapes[0] and [1].

As for the thread, if he did mean polymorphism,

Example 4:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct Foo{
   int i;
   virtual void print() {
      std::cout << "foo";
   }
};

struct Bar : Foo{
   virtual void print() {
      std::cout << "bar";
   }
};

Foo *myfoo = new Foo, *mybar = new Bar;
myfoo->i = 13;
mybar->i = 17;
becomes
name    : ... | myfoo | mybar | ... | vftab |   i   | ... | vftab |   i   | ...
value   : ... | 0xba2 | 0xca2 | ... | 0xd00 |   13  | ... | 0xf00 |   17  | ...
address : ... | 0x123 | 0x124 | ... | 0xba2 | 0xba3 | ... | 0xca2 | 0xca3 | ...
Note there isn't really a member named vftab (short for virtual function pointer table). That first word isn't meant for you to access. 0xd00 and 0xf00 are memory locations where functions Foo::print and Bar::print reside (or rather, tables of pointers to all virtual functions a class has).
Last edited on
Updated again, I guess I count on the Operating System too much. I'll have to brush up on my dynamic allocation.
Last edited on
Actually seeing it like that really makes sense, just read about virtual functions and pointers and that makes more sense now that I've seen it like that. The polymorphism and creating a pointer to a class from another class is the confusing part, for example the project I've been working on:

BlackJack!

First thing I created was a "PlayingCard" class and header, tested it with a driver.cpp file to make sure everything worked, then I created a "PlayingCardDeck" class and header using the PlayingCard class, tested it again. Created another class called "BlackJackHand", same as above, tied everything together, so far so good, nothing too crazy, everything makes sense.

Now the fun part, created a "Player" and a "Dealer" class for each, where Dealer inherits from Player and player has a BlackJackHand from above. The problem here is that I can't directly access the functions from my previous 3 classes the way I did before since i created an object for the hand and not a pointer(?) I guess this one confused me because it asked for an object, and not a pointer. Which I guess makes sense, the player would actually have a deck in his hand, so I'd call that an object, rather than try to reference a deck in the previous three classes, that way the dealer can also have a separate hand.

Also had two virtual functions in the Dealer class override the Player methods, like displaying the first card hidden, and special rules on hitting/staying. Thats where which function is getting called gets hazy, I thought it was always the most specific, but I guess its really whichever one is being pointed to?

Sounds like I just need to work with them more and try things out to figure out how they all work together! :D
Topic archived. No new replies allowed.