GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-May-2007, 18:00
meili100 meili100 is offline
Junior Member
 
Join Date: Feb 2007
Posts: 37
meili100 has a little shameless behaviour in the past

A simple question of a C++ function


Here is a class

CPP / C++ / C Code:
class Complex{
private:
	int real, image;
public:
	Complex(int r=0, int i=0):real(r),image(i){}

	int GetReal() {return real;}
	int GetImage() {return image;}

	void foo(const Complex & rhs){
		cout<<rhs.image<<endl;
		return;
	}
};



It works, but my question is that 'image' is private of 'rhs', how can you visit a private variable of rhs outside (rhs.image)?
  #2  
Old 08-May-2007, 09:42
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: A simple question of a C++ function


Quote:
Originally Posted by meili100
how can you visit a private variable of rhs outside

The function foo that is being defined here is a member function of the class (since it is inside the class Complex {}; braces), so it has access to all class members.

If "outside" functions want to print the value of the image member, they can use the foo function. If "outside" functions want to get the value of the image member, thay can use the GetImage function.

Regards,

Dave
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB 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
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
[Tutorial] Function Pointers aaroncohn C++ Forum 4 17-Feb-2006 12:33
question about inline function bchasco C++ Forum 1 22-Jul-2005 16:52
non-member function question crq C++ Forum 1 03-Feb-2005 22:59
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 05:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:11.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.