Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 31st, 2008, 10:49 PM   #1
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 311
Rep Power: 3 Fall Back Son is on a distinguished road
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
Fall Back Son is offline   Reply With Quote
Old Mar 31st, 2008, 11:00 PM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
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>
Jimbo is offline   Reply With Quote
Old Apr 1st, 2008, 3:19 AM   #3
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 308
Rep Power: 2 Jabo is on a distinguished road
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.
Jabo is offline   Reply With Quote
Old Apr 1st, 2008, 11:09 AM   #4
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
Re: Simple Question about C++

Quote:
Originally Posted by Jabo View Post
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.
What you were taught is the recommended practice, but sometimes you want something to access the private data. For instance, if you write a custom string class and you want to overload the input operator, which is a global function, you'll want it to access you private data, but you don't want to make said data available via a public method. You'll have something like this (forgive me if the syntax is off, I haven't actually used C++ for a long time):
cpp Syntax (Toggle Plain Text)
  1. class MyString {
  2. char* underlyingData;
  3. friend istream& operator>> (istream&, MyString&);
  4. // more implementation here
  5. };
  6.  
  7. istream& operator>> (istream& in, MyString& str) {
  8. // get input and put it in str.underlyingData; resize as necessary
  9. // ...
  10. return in;
  11. }

Just remember, friends can see each other's privates (note that friendship is a one-way relationship though)
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Apr 3rd, 2008, 9:21 AM   #5
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3 Soulstorm is on a distinguished road
Re: Simple Question about C++

Quote:
Originally Posted by Jabo View Post
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.
Everything exists for a reason. Friend functions can be friends to more than one class, so they can have access to the private members of objects of different classes. That makes the, very useful in situations where accessing private members of many classes are the target.

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)
Soulstorm is offline   Reply With Quote
Old Apr 3rd, 2008, 2:47 PM   #6
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 311
Rep Power: 3 Fall Back Son is on a distinguished road
Re: Simple Question about C++

Quote:
Originally Posted by Soulstorm View Post
Everything exists for a reason. Friend functions can be friends to more than one class, so they can have access to the private members of objects of different classes. That makes the, very useful in situations where accessing private members of many classes are the target.
Yes... it seems that a lot of things exist for the purpose of a headache.
Fall Back Son is offline   Reply With Quote
Old Apr 4th, 2008, 2:30 PM   #7
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 115
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
Re: Simple Question about C++

Quote:
Originally Posted by Jimbo View Post
Just remember, friends can see each other's privates (note that friendship is a one-way relationship though)
Hahahaha.
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:08 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC