Click here to Skip to main content
14,691,602 members
Home » Discussions » ATL / WTL / STL
   

ATL / WTL / STL

 
Questionusing friend Pin
bkelly139-Dec-14 8:29
Memberbkelly139-Dec-14 8:29 
AnswerRe: using friend Pin
Richard MacCutchan10-Dec-14 6:22
mveRichard MacCutchan10-Dec-14 6:22 
GeneralRe: using friend Pin
bkelly1311-Dec-14 11:24
Memberbkelly1311-Dec-14 11:24 
GeneralRe: using friend Pin
Richard MacCutchan12-Dec-14 0:07
mveRichard MacCutchan12-Dec-14 0:07 
GeneralRe: using friend Pin
Aescleal12-Dec-14 0:52
MemberAescleal12-Dec-14 0:52 
GeneralRe: using friend Pin
bkelly1312-Dec-14 13:12
Memberbkelly1312-Dec-14 13:12 
GeneralRe: using friend Pin
Aescleal15-Dec-14 6:18
MemberAescleal15-Dec-14 6:18 
AnswerRe: using friend Pin
Theo Buys22-Dec-14 5:25
MemberTheo Buys22-Dec-14 5:25 
friend can be used to give another class access to the protected and private members of the class that specified friend.

If C_Messages want to access the protected and private members of the class C_Configuration then C_Configuration specified C_Messages as friend.

If you aggregate the class C_Messages so that is has a C_Configuration, you can only access the public members of C_Configuration in C_Messages. But with the friend specifier you can also access the protected and private members.

Sample code:

class C2;
class C1
{
public:
	int public_var;
protected:
	int protected_var;
private:
	int private_var;
friend class C2;
};

class C2
{
public:
	class C1 member;
	void set_private_var (int v) { member.private_var = v; } 
	void set_protected_var (int v) { member.protected_var = v; } 
};

void Test()
{
	C2 obj;
	
	obj.member.public_var = 100; // ok

	// ERROR: can't access protected and private member
	// obj.member.protected_var = 100; 
	// obj.member.private_var = 200; 

	obj.set_protected_var(100); // ok
	obj.set_private_var(200);   // ok
}


Another solution is working with an intermediate class which specifies the friend. In this case you get only access to public and protected members of the base class.

Sample code:

class C1
{
public:
	int public_var;
protected:
	int protected_var;
private:
	int private_var;
};

class C1B : public C1
{
friend C2;
}

class C2
{
public:
	class C1B member;        
	void set_protected_var (int v) { member.protected_var = v; } 
};

void Test()
{
	C2 obj;
	
	obj.member.public_var = 100; // ok

	// ERROR: can't access protected and private member
	// obj.member.protected_var = 100; 
	// obj.member.private_var = 200; 

	obj.set_protected_var(100); // ok	
}


modified 5-Jan-15 6:07am.

QuestionHow to create a dll as Unbreakable in C#.Net? Pin
karthickbm7-Dec-14 21:37
Memberkarthickbm7-Dec-14 21:37 
AnswerRe: How to create a dll as Unbreakable in C#.Net? Pin
Richard MacCutchan7-Dec-14 23:52
mveRichard MacCutchan7-Dec-14 23:52 
QuestionTCP/API class basic design Pin
bkelly138-Nov-14 17:31
Memberbkelly138-Nov-14 17:31 
AnswerRe: TCP/API class basic design Pin
Garth J Lancaster8-Nov-14 17:56
professionalGarth J Lancaster8-Nov-14 17:56 
GeneralRe: TCP/API class basic design Pin
bkelly139-Nov-14 3:15
Memberbkelly139-Nov-14 3:15 
AnswerRe: TCP/API class basic design Pin
Garth J Lancaster8-Nov-14 18:06
professionalGarth J Lancaster8-Nov-14 18:06 
GeneralRe: TCP/API class basic design Pin
bkelly139-Nov-14 3:19
Memberbkelly139-Nov-14 3:19 
AnswerRe: TCP/API class basic design Pin
Albert Holguin10-Nov-14 5:58
professionalAlbert Holguin10-Nov-14 5:58 
GeneralRe: TCP/API class basic design Pin
bkelly1310-Nov-14 13:40
Memberbkelly1310-Nov-14 13:40 
AnswerRe: TCP/API class basic design Pin
Albert Holguin10-Nov-14 17:47
professionalAlbert Holguin10-Nov-14 17:47 
GeneralMultiple threads explained Pin
bkelly1311-Nov-14 7:30
Memberbkelly1311-Nov-14 7:30 
GeneralRe: Multiple threads explained Pin
Albert Holguin11-Nov-14 8:01
professionalAlbert Holguin11-Nov-14 8:01 
GeneralRe: Multiple threads explained Pin
bkelly1311-Nov-14 10:58
Memberbkelly1311-Nov-14 10:58 
AnswerRe: TCP/API class basic design Pin
xenotron10-Jan-15 12:06
Memberxenotron10-Jan-15 12:06 
QuestionTime of reading text file Pin
econy7-Nov-14 5:24
Memberecony7-Nov-14 5:24 
QuestionRe: Time of reading text file Pin
Richard MacCutchan8-Nov-14 22:33
mveRichard MacCutchan8-Nov-14 22:33 
AnswerRe: Time of reading text file Pin
econy11-Nov-14 3:06
Memberecony11-Nov-14 3:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.