private data member with public set member function

This is a discussion on private data member with public set member function within the C++ Programming forums, part of the General Programming Boards category; Isn't it somehow an avoidable contradiction? I mean, Code: class D { private: int myproperty; public: D(int mp=1):myproperty(mp){;} void SetMyproperty(int ...

  1. #1
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317

    private data member with public set member function

    Isn't it somehow an avoidable contradiction?

    I mean,
    Code:
    class D {
        private:
            int myproperty;
        public:
            D(int mp=1):myproperty(mp){;}
            void SetMyproperty(int setvar) {myproperty = setvar;}
    };
    Why do I want to this? Why did I declared myproperty private if I then create a public member function to change it?
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Why did I declared myproperty private if I then create a public member function to change it?
    So that the class can regulate how myproperty is changed. If you made it public then any other code in the program could change the value to anything and the class would have no idea whether or not myproperty is actually a valid value.

    Code:
    void SetMyproperty(int setvar) {myproperty = setvar;}
    This is as simple as it gets, but not very realistic. If this is all you are doing then the set method is a waste of time, this method is where you can and should thoroughly check the contents of setvar and make sure that everything is kosher before assigning it to myproperty. If you do this then you can use myproperty in the program without having to validate it since you can be sure that it's not a wacky value, it was checked before being assigned.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Thanks Prelude

    I knew I was missing something.
    Much easier to place the validation code and/or some needed processing under the class definition than to do it everytime the data member is changed.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  5. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM

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