Online education: C++

I’m teaching myself C++ from a book, and in a previous post some board members helped me out with a question. I have another question, but it occurs to me that what I really need is some C++ authority I can e-mail on a regular basis to help me when I get hung up. Are there any online education resources for helping people with C++?

BTW, my question is this: I’m now learning about pointers, and how they can be used to create and access objects on the free store. In one exercise, the book demonstrates that such an object can itself have pointers as it’s data members. I understand how that works, I just don’t see what it’s for. Since the object itself and all it’s data members are already on the free store, why bother having some of those data members simply be pointers to somewhere else on the free store?

There’s a hundred different reasons why you’d want member variables to be pointers, but two come to mind.

The first is when a class contains another class. For example, I may have a “Family” class that includes “People” classes. As the “People” classes can be of various length (a family may contain anywhere from 1 to an infinite number of people) the People class might be a pointer to an array of people. Does that make sense?
Another example is when you may not have the information at the time the original class is instantiated to create a member variable. For example, let’s say you have the above Family class. A family may or may not contain a dog. You might want to include a pointer to a dog object, but not actually instantiate it until the family gets a dog. The code would look something like:

class Family
People * m_people;
Dog * m_dog;

m_dog = new Dog;

Make sense?

If I’m understanding the OP correctly, it would also be useful for a linked list. Each object in a linked list contains some piece or pieces of data, and a pointer to the next object in the list (the last element points either to null or the first element). You can get a lot of flexibility this way, for things like adding a new element in the middle of a list. To do that in an array, you’d need ot move all of the elements after it ahead one space, but with a linked list, you just create the new element, point it towards another element of the list, and change one existing pointer in the list to point to it.

Arrays are powerful, but suppose you don’t know how many occurences of an object you will have until run time or that the number of occurences varies. You could define an array with a very large upper bound, but this may waste space. What you can do is create a linked list, with a root defined in static or auto storage.

class Node
    Node *Next;
    // stuff
Node *Root=NULL;

Then in your process loop any time you want to create a new occurence of a node,

Node *tmp=new Node;

To process every Node:

Node *Curr;
// process Curr->stuff

Why does text in [****code] blocks double space?
Deleting the list is left as an exercise for the reader. Hint: replacing “//process Curr->stuff” with “delete Curr;” is not correct.

If you really want to get me started, ask about binary trees.

I just saw Chronos’s post. I am going to post this 'cuz this goes into more detail.

The OP was asking about having pointers as member data of a class.

If we got onto this subject of link lists because of my example of having a pointer to an array, I simplified my example because Lumpy described himself as a beginner to C++. Obviously, a link list is more appropriate than an array, for all the reasons stated.

Thanks for all your help. I do seem to gather that a big part of object-oriented programming is an almost infinite regression of abstraction: You give the direction to the description of the alias of the address of the data you want.

The funny thing is, this sorta makes sense in terms of the underlying machine code. C++ is the first language I’ve learned that makes extensive use of manipulating memory directly. You can almost “see” what the commands are doing at the machine level.

It’s actually a lot simpler once you get it. I remember learning pointers, and yes, they seemed complicated and it was hard to see why you’d actually use them. When you start writing real code, though, they become simple and powerful.

BTW, you CAN “see” what the commands are doing at the machine level - or at least, assembler level. Most debuggers for C++ have a mode where you can drop to assembler, and see the low level commands that are running. Very handy when you want to impress VB coders.

Wait until you get into such things as design patterns and COM… it’ll knock your socks off. BTW, feel free to email if you have other questions. I sorta like doing this.