|
-
May 12th, 2001, 11:58 AM
#1
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.
-
May 12th, 2001, 01:16 PM
#2
Monday Morning Lunatic
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
-
May 12th, 2001, 01:28 PM
#3
Frenzied Member
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."
-
May 12th, 2001, 01:37 PM
#4
Frenzied Member
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."
-
May 12th, 2001, 01:39 PM
#5
Monday Morning Lunatic
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
-
May 12th, 2001, 01:45 PM
#6
Frenzied Member
Uhh maybe I'm having a blonde moment... why would that prevent excess copying?
Harry.
"From one thing, know ten thousand things."
-
May 12th, 2001, 01:48 PM
#7
Monday Morning Lunatic
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
-
May 12th, 2001, 01:48 PM
#8
Frenzied Member
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."
-
May 12th, 2001, 01:49 PM
#9
Frenzied Member
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."
-
May 12th, 2001, 02:01 PM
#10
Monday Morning Lunatic
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
-
May 12th, 2001, 02:05 PM
#11
Frenzied Member
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."
-
May 12th, 2001, 02:10 PM
#12
Monday Morning Lunatic
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
-
May 12th, 2001, 02:11 PM
#13
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|