I have some basic C++ design/syntax questions and would appreciate your reply.
- I have N number of regions
- Each region needs to store information about an object "element"
i.e. I want to achieve something like this:
region[i].elements
= list of all the elements for region i.
Question 1: Does the following syntax (see code below) / design look correct. Am I missing anything here?
EDIT
The instances of struct elem are created by some other class and its memory deallocation is handles by that class only I just want to access that object and its members using reg[i].elements list (vector) ... so, how should I add these element objects to the vector "elements" in class Region?
//Already have this stucture that I need to use
struct elemt {
int* vertex;
int foo1;
double foo2;
};
class Region{
public:
// I am not sure what should be the syntax here!
// is it correct?
std::vector <elemt*> elements;
}
// Following is the constructor of "class A"
A::A(){
// --header file defines: Region *reg;
// Let numOfRegions be a class variable. ( changes based on "Mac"'s suggestion)
numOfRegions = 100;
//allocate memory:
reg = new Region[numOfRegions];
}
A::~A(){
delete [] reg;
reg = NULL;
}
A::doSomething(){
// here I want to append the elements to the vector
// Let i be region 10.
// Let e1 be an element of "struct elemt" that needs to be added
reg[i].elements.push_back(e1);
}
Question 2:
Is the syntax in doSomething()
correct? Later I want to run an iterator over all the elements in reg[i]
and want to access, e1->foo1, e1->foo2
and like that.
Question 3: In do something method, how do I ensure that e1 is not already in the "elements"
UPDATE
Corrected some syntax errors, and hopefully fixed memory leak noticed by user 'Mac. '