Sign in to follow this  

C++ Struct containing function question.

This topic is 2954 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
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

This topic is 2954 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.

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