a nicer way to create structs in a loop

Posted by sandra on Stack Overflow See other posts from Stack Overflow or by sandra
Published on 2010-05-10T09:48:52Z Indexed on 2010/05/10 9:54 UTC
Read the original article Hit count: 218

Filed under:
|
|
|
|

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?

© Stack Overflow or respective owner

Related posts about c++

Related posts about new