Sign in to follow this  
highowl

C++ Struct containing function question.

Recommended Posts

highowl    118
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
mattd    1078
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this