If I have a class declared in class.h and defined in header.h like below.
I'm just not sure how it is implemented and accessed.
class.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class myclass
{
public:
int a;
int b;
myclass(a,b);
~myclass();
//somewhere in here I put my structure like below.
struct mystruct
{
int x;
int y;
}instance;
};
class.cpp
myclass::myclass(int,int):a(5),b(4){}
main.cpp
1 2 3 4 5
int main()
{
//I'm not sure how I would access this struct? Completely lost actually?
//is my syntax in the class declaration right?
}
You have a class member called instance, which you can access using class member access operator . (dot), and you have a nested type called mystruct, which you can access using the scope resolution operator ::
Okay, I see.
Like Cubbi said, you can use :: to access what you declared in the class.
But I didn't use it too much, then sorry that I misunderstand it.
Usually, I'll use namespace or just create struct outside and call it as an instance in class.
Because in class, I think what's in it should be default private so class does everything itself. if outside want to do something, they have to call by functions.