![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 311
Rep Power: 3
![]() |
Simple Question about C++
Is it possible to edit private data members in C++? So if I had code like the following, would it be possible to edit it from another class ?
class myClass { private: float myFloat; }; My initial thought is that yes, it would be possible, because C++ does not offer protection for pointer-related access. So I could use a pointer to change the value stored in myFloat. But I'm not sure about this, I use Java generally, not C++. Hopefully someone can help. Thanks ![]() |
|
|
|
|
|
#2 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
Re: Simple Question about C++
There's two ways to change it, and one is supported by the language. That is to declare a friend class, as such:
class foo {
float myFloat;
friend class bar;
};
class bar {
public:
void DoFoo(foo& f) {
foo.myFloat = 0.0f;
}
};The other way is a complete hack and can't be trusted: just get the memory location of the object and start swapping bits ![]()
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
#3 |
|
Not a user?
Join Date: Sep 2007
Posts: 308
Rep Power: 2
![]() |
Re: Simple Question about C++
friend classes can access private data? I thought private data was only accessible to the owning class. I was taught in school to use public methods to access private data as this enforces data hiding.
|
|
|
|
|
|
#4 | |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
Re: Simple Question about C++
Quote:
cpp Syntax (Toggle Plain Text)
Just remember, friends can see each other's privates
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
|
#5 | |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3
![]() |
Re: Simple Question about C++
Quote:
The alternative would be to use getter methods. However, despite what teach you in school, not always is this the best programming approach. For small classes, where data hiding is not necessary, not making all of the variables public can result in code bloat. For the time being, stick to your school's approach. Later, you will see that not always will you use private members.
__________________
Project::Soulstorm (personal homepage) |
|
|
|
|
|
|
#6 | |
|
Professional Programmer
Join Date: Oct 2006
Posts: 311
Rep Power: 3
![]() |
Re: Simple Question about C++
Quote:
|
|
|
|
|
|
|
#7 |
|
hi: for(;;) goto hi;
|
Re: Simple Question about C++
Hahahaha.
__________________
How do you play Religious Roulette? Stand around in a circle and blaspheme till someone gets struck by lightning. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| very simple question. make button change number in edit box | nickm | Delphi | 2 | Apr 29th, 2006 11:47 PM |
| A simple programming question | punter | C# | 8 | Jan 5th, 2006 5:04 PM |
| Simple c# question | nez | C# | 8 | Jul 1st, 2005 7:29 PM |
| Simple (stupid cause I don't know it) basic question | massive-war | C++ | 17 | Apr 12th, 2005 12:03 AM |
| Simple Writing to Files. Newbie Question | kiaran | C++ | 1 | Feb 8th, 2005 4:22 AM |