C++ Struct containing function question.

Started by
5 comments, last by phresnel 14 years, 5 months ago
I was wondering if a function contained in a struct could modify variables of the struct. Example

struct OBJECT
{
     int coord_x;
     move_right(int move_distance)
     {
          coord_x += move_distance;
     }
};

main()
{
     OBJECT example;
     example.coord_x = 200;
     
     example.move_right(25);
}
Would something like that work?
Advertisement
Yes.
Very Yes.
[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]
Thank god....that'll make things alot easier; and thanks for answering so quick.
Yes, a struct is exactly the same as a class in C++ (except that it's members are "public:" by default).
Quote:Original post by Hodgman
Yes, a struct is exactly the same as a class in C++ (except that it's members are "public:" by default).

and it defaults to public inheritance instead of private :)
Though I am curious about the reasoning behind not trying it out yourself in the first place ;)

This topic is closed to new replies.

Advertisement