• Advertisement
Sign in to follow this  

C++ Struct containing function question.

This topic is 3022 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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 :)

Share this post


Link to post
Share on other sites
Though I am curious about the reasoning behind not trying it out yourself in the first place ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement