Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C++ I Cannot Grasp Pointers and Classes

I'm fresh out of college and have been working in C++ for some time now. I understand all the basics of C++ and use them, but I'm having a hard time grasping more advanced topics like pointers and classes. I've read some books and tutorials and I understand the examples in them, but then when I look at some advanced real life examples I cannot figure them out. This is killing me because I feel like its keeping me from bring my C++ programming to the next level. Did anybody else have this problem? If so, how did you break through it? Does anyone know of any books or tutorials that really describe pointers and class concepts well? or maybe some example code with good descriptive comments using advanced pointers and class techniques? any help would be greatly appreciated.

like image 817
timmyg Avatar asked Sep 18 '08 19:09

timmyg


1 Answers

Pointers and classes aren't really advanced topics in C++. They are pretty fundamental.

For me, pointers solidified when I started drawing boxes with arrows. Draw a box for an int. And int* is now a separate box with an arrow pointing to the int box.

So:

int foo = 3;           // integer
int* bar = &foo;       // assigns the address of foo to my pointer bar

With my pointer's box (bar) I have the choice of either looking at the address inside the box. (Which is the memory address of foo). Or I can manipulate whatever I have an address to. That manipulation means I'm following that arrow to the integer (foo).

*bar = 5;  // asterix means "dereference" (follow the arrow), foo is now 5
bar = 0;   // I just changed the address that bar points to

Classes are another topic entirely. There's some books on object oriented design, but I don't know good ones for beginners of the top of my head. You might have luck with an intro Java book.

like image 153
nsanders Avatar answered Oct 13 '22 04:10

nsanders