class Point {
public:
int x;
int y;
};
class Site {
public:
Point coord;
int sitenbr;
int refcnt;
};
The last two lines are what my question is about. I want to set sites->coord.x but have no clue how to do this. I get that siteptr site_ptr( new Site); is creating a shared pointer that points to the class Site but within new Site do i need to set the value of sites->coord.x = subx like this:
siteptr site_ptr( new Site(sites->coord.x = subx);
or maybe:
siteptr site_ptr( new Site(this->coord.x = subx);
As you can probably tell I'm really not sure. Or can I initialize the class and set the value later?
You need to write constructors for your classes. e.g.
1 2 3 4 5 6 7 8
class Point {
public:
Point(int setx,int sety)
: x(setx), y(sety) { }
private:
int x;
int y;
};
I have put you variables in a private section since this is the normal thing to do (but not a requirement)
The constructor I have written uses an initialisation list (and has an empty body)
With this constructor you can now create a point like this