I have this code

CPoint* points;

int numPoints (200);
points = new CPoint[200];

I have another class called CPlanet, and a bunch of pointers like:
CPlanet* pSun = new CPlanet;

... and CPoint and CPlanet call the same functions, so I would like to use -> to call their functions.
pSun->Move() is ok, but for points I have to use points.Move().

The thing is I want to use -> for both the single planets and the array of points.
So I tried this, but I get a break:

CPoint** points;

int numPoints (200);
*points = new CPoint[200];

I'm just not used to double pointers being used as arrays.. can't really figure out what I'm doing wrong though..

I also tried points = new CPoint* [200] but that also gives a break...
so what should I do?

Thanks a lot,

I think this will work

CPoint** points;

make points a pointer to an array of pointers of type Cpoint*:

points = new CPoint*[200];

each points in points is a pointer to type CPoint so now you can call member functions using the ->

points->Move();

if that's what you really want to do.

As I said, I tried this and I'm getting breaks..

You are both mixing between the pointer and the pointee. CPoint foo; is an actual CPoint. It exists. CPoint *foo; is a pointer to a CPoint. So far, no CPoint actually exists. CPoint **foo; is a pointer to a pointer to a CPoint. So far, neither a "pointer to a CPoint" nor any Cpoint exists.

So, Lerner started out right: you need to create the things that exist one step at a time.

CPoint **points;

// Create 200 "pointers to CPoint"
points = new CPoint*[ 200 ];
// (So far, no CPoints exist)

// Create 200 CPoints
for (int i = 0; i < 200; i++)
  points[ i ] = new CPoint;
// (Now you have 200 pointers to 200 CPoints)

points[ 42 ]->Move();  // Move the 43rd planet

I'm still not sure I understand exactly what you are trying to do, or even why you are dinking around with pointers in C++ (just use a vector or a deque or a list), but I...

Hope this helps.

Thanks a million :)

I am dinking around with pointers because C++ is a hobby and I like to learn about these things.. deques are just too easy aren't they?

Heh heh, yeah.

Nowadays people like to call pointers "references" and pretend they're all beautiful. Same bully thing.

I think it is a really good idea to have a clear understanding of what pointers are and how they work, because you will always need them at some point. (Well, unless you're playing with an interpreted language like Tcl, or a functional language like Scheme...) And because they are pretty basic to understanding how a computer actually works...

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.