Results 1 to 13 of 13

Thread: Class Accessor Methods...

  1. #1
    amac
    Guest

    Class Accessor Methods...

    Is there a way to access a private member variable... so that to the user of the class it seems just to be a public const... but in actually fact its just a private member...

    For example...

    class X { // this class definition doesnt compile... just
    private: // the best way to see what i am talkin about
    int y;

    public:
    int y() { return this->y; }
    };

    X z;

    cout << z.y; // this would display the contents of X::y.
    z.y = 4; // this would be an error.

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You don't need this->y -- just use y, since it assumes they'll be part of this.
    Code:
    class X {
    public:
        int y() { return m_y; }
    private:
        int m_y; // it's a good idea to prefix member variables
    };
    
    X z;
    
    cout << z.y(); // it's a function call so you need the brackets
    z.y() = 4; // this would be an error (obviously ;))
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Couldn't you use a member function to yield a pointer to the variable? Or actually, even better, a reference:

    Code:
    class X {
    public:
        int& y() { return m_y; }
    private:
        int m_y; // it's a good idea to prefix member variables
    };
    
    X z;
    
    cout << z.y(); // it's a function call so you need the brackets
    z.y() = 4; // would this still be an error?
    Harry.

    "From one thing, know ten thousand things."

  4. #4
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Seems to work fine. This compiled and ran as expected:
    Code:
    #include <iostream>
    
    class X 
    {
    public:
        int& y() { return m_y; }
    	X() { m_y = 2; }
    private:
        int m_y; 
    };
    
    X z;
    
    int main()
    {
    	std::cout << z.y() << std::endl; 
    	z.y() = 4; 
    	std::cout << z.y() << std::endl;
    
    	return 0;
    }
    Harry.

    "From one thing, know ten thousand things."

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You could, but I think what he wanted was just to get the value...although returning a const reference is probably better if your class has a member variable that's a class/struct to prevent excess copying.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Uhh maybe I'm having a blonde moment... why would that prevent excess copying?
    Harry.

    "From one thing, know ten thousand things."

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Okay...if it returns a value, it copies it. If it returns a reference, it doesn't copy it. However, returning a reference means you can change it, which isn't wanted. Therefore you return a const reference.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Ohh I was having a blonde moment. I didn't really understand what you were saying, I was thinking of the sentence in a different context.

    I take the point about the const thing, I forgot it was meant to be const.
    Harry.

    "From one thing, know ten thousand things."

  9. #9
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Heh, yeah I just realised what you were talking about before you posted I thought you meant a const reference was better than a normal reference because it prevented copying... that's what confused me.
    Harry.

    "From one thing, know ten thousand things."

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    No problem...just a tad confusing

    I suppose it's random things like this they want to fix in the new standard.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    I guess so. Have you heard anything specific about what they're looking at introducing? I read some general stuff but don't remember much of it.
    Harry.

    "From one thing, know ten thousand things."

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Nothing specific, but according to those notes they're taking out the parts that confuse people.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Heh, why didn't they think of that sooner?
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width