views:

159

answers:

5

Hi guys, I haven't coded in C++ in ages. And recently, I'm trying to work on something involving structs. Like this

typedef struct{
    int x;
    int y;
} Point;

Then in a loop, I'm trying to create new structs and put pointers to them them in a list.

Point* p;
int i, j;
while (condition){
    // compute values for i and j with some function...
    p = new Point;
    p* = {i, j}; //initialize my struct.
    list.append(p); //append this pointer to my list. 
} 

Now, my question is it possible to simplify this? I mean, the pointer variable *p outside of the loop and calling p = new Point inside the loop. Isn't there a better/nicer syntax for this?

+9  A: 

Sure:

Point * p  = new Point;

You should probably also give your Point class a constructor:

struct Point {      // note no need for typedef
    int x;
    int y;
    Point( int ax, int ay ) : x( ax ), y( ay ) {}
};

so that you can say:

Point * p  = new Point( i, j );

You may also want to make your list a list of Point values, rather than pointers, in which case you can avoid using dynamic allocation with new - always something to be avoided wherever possible in C++.

anon
Oh! Thanks, Niel! So that will reserve the new p each time inside the loop? (sorry if it seems idiotic, I do mainly C all the time)
sandra
WAIT! I can add a method to a struct?! Wow! Thanks!
sandra
@sandra C is exactly the same in this regard. It's best to think of pa s being re-created each time through the loop. And structs and classes are basically the same thing in C++.
anon
As Niel said, you can just do it like this:while( condition ){ list.push_back( Point( i,j ) );}If you don't need pointers
Nikko
Thank you so much @Niel and @Nikko!
sandra
@sandra Structs are essentially the same as classes - just with public access by default. So yes, you can add constructors, methods, destructors etc. just like classes
AshleysBrain
good answer "Niel"
Nick D
Totally, I tried it and it worked like a charm! Thanks again guys (especially @Niel) :)
sandra
+2  A: 

As structs are classes with public members by default, you could even create a constructor within the struct and initialize your point object within the loop like this:

Point* p = new Point(i,j);
Simon
Thanks @Simon, is the constructor in @Niel's solution required?
sandra
Yes of course. You have to explicitly declare the constructor, it won't be done automatically.
Simon
Thanks, Simon! In fact, that's what I've discovered by commenting out the constructor! :)
sandra
+3  A: 

The struct can have a constructor like:

struct Point{
    Point(int ax, int ay):x(ax), y(ay){}
    int x;
    int y;
};

and then the function can look like:

int i, j;
while (condition)
{
    list.append(new Point(i,j));
} 
Akanksh
Thanks @Akanksh! :)
sandra
+1  A: 

I would venture a guess that it is extremely unlikely you really need to allocate something like a Point dynamically.

Most likely you want to add a constructor and store them by value:

list<Point> list;
list.append(Point(x, y));
visitor
A: 

I recommend the Factory approach. Assuming that "Point" will be the base class of many objects, you can have a "Factory" that would return pointers.

Ex:

struct Point
{
        Point(int mx, int my):x(mx),y(my) {}
        int x;
        int y;
};

// Circle, Polygon, etc.

class Factory
{
public:
        static Point *getPoint(int mx, int my) { return new Point(mx, my); }
// Circle, Polygon, etc
};

Then in code someplace:

while(cond)
{
 list.append(Factory::getPoint(i, j));
}
Daniel
Why would you assume that Point is a base class? If it were, it would be called Shape, wouldn't it? This is ridiculously over-engineered.
anon