variables inside class help

This is a discussion on variables inside class help within the C++ Programming forums, part of the General Programming Boards category; I have a class made that has function and variables and all the usual stuff. But I want so if ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    variables inside class help

    I have a class made that has function and variables and all the usual stuff.

    But I want so if the user changes the x varaible like:

    object.x = 50;

    the class will perform a function that changes the x like this:

    object.changepos(x, y);

    so it will change the object to the new x the user set. How would I do that?

  2. #2
    He's trying.
    Join Date
    Apr 2005
    Location
    Missouri, US
    Posts
    70
    Don't make x public - use something like a public setter function, so they have to use the function instead of changing it directly. Then make your setter function called changepos.

    E.g.
    Code:
    public:
    void setX(int newx);
    private:
    int x;
    //..........
    void classname::setX(int newx)
    {
    x = newx;
    changepos(x, y);
    }
    Well, that's the general idea, anyway.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    so basicly I can't/shoudln't do what I wanted to. Ok well sounds good.

    thanks

  4. #4
    He's trying.
    Join Date
    Apr 2005
    Location
    Missouri, US
    Posts
    70
    Ahh, yes, nothing that would be remotely practical.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    umm well is there a way to make x public but read only?

  6. #6
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    umm well is there a way to make x public but read only?
    http://duramecho.com/ComputerInforma...wCppConst.html

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    umm const isn't really helping, it is throwing a bunch of errors sayign I can't make x static.

    plus I searched and thought there was a readonly keyword, but I guess I am wrong.

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    ok well what I mean is if I declare it a const I can't change x. Declaring it mutable will (in my case) just make it a ragular variable.

  9. #9
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    ok well what I mean is if I declare it a const I can't change x.
    That is what you just asked for:
    "umm well is there a way to make x public but read only?"

    You will have to be a little more clear, and maybe post some of the code you are having trouble with.

  10. #10
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    I totally interpretted his problem differently.

    It seems like you might be asking if you should have x public, or private and use SetX function in public. However, at second glance you seem to be asking if the person at the console changes x, you want the position to change also.

    These types of things you, the programmer, have to do yourself, you can just have it auto update or anything. Do it like this:

    Code:
    class Object
    {
    private:
      int x;
    
    public:
    
      void SetX(int);
      void SetY(int);
      //there should be a version where you only want to change x or y
      //if you enter 0 in the bool, it changes x, if you enter 1 in the bool, it changes y
      ChangePos(bool, int); 
      ChangePos(int, int);
    
    };
    
    void Object::SetX(int p_x)
    {
      x = p_x;
      ChangePos(0, x);
    }
    
    void Object::SetY(int p_y)
    {
      y = p_y;
      ChangePos(1, y);
    }
    
    int main()
    {
      Object object;
      object.SetX(50);
      object.SetY(20);
    }
    So if you fill that into your program, whenever x or y is updated, the position is updated also.

    I dont understand why you would want it readonly, since x changes, or .. whatever you're trying to do above, so maybe I have the wrong idea here
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  11. #11
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    If you want it read-only you could make it private but declare a public function to access it:
    Code:
    class Object
    {
    public:
     int GetX() const { return x; }
    private:
      int x;
    };
    Of course, if you also has a SetX function that changes the value of x to anything the caller wants, then it might as well be public. If the SetX function did something like, for instance, checking to see if the new x value is within a certain range, then it could still make sense.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  12. #12
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    >>I dont understand why you would want it readonly, since x changes, or .. whatever you're trying to do above, so maybe I have the wrong idea here

    I said that cause I was all confused from the first couple of posts. I actaully still don't mind the idea of x being read only but the class being able to change it, but the last 2 posts are great! Basicly just what I wanted.

    Thanks!

  13. #13
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    Quote Originally Posted by Rune Hunter
    >>I dont understand why you would want it readonly, since x changes, or .. whatever you're trying to do above, so maybe I have the wrong idea here

    I said that cause I was all confused from the first couple of posts. I actaully still don't mind the idea of x being read only but the class being able to change it, but the last 2 posts are great! Basicly just what I wanted.

    Thanks!
    If something is 'read only', you can't change it, logically.

    It looks like Dae gave a good code sample that should solve your problem, or atleast point you in the right direction.

    Good luck with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring SDL_Rect variables in a class?
    By pwnz32 in forum C++ Programming
    Replies: 10
    Last Post: 07-27-2008, 07:12 PM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Unresolved external on vector inside class constructor
    By Mario F. in forum C++ Programming
    Replies: 13
    Last Post: 06-20-2006, 12:44 PM
  4. Cleaning variables for a CGI app (as a derived class?)
    By drrngrvy in forum C++ Programming
    Replies: 1
    Last Post: 02-01-2006, 11:34 AM
  5. Class inside Class
    By Travis Dane in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2002, 05:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21