friend class ain't my friend
-
Hi, I've tried to define a friend class for the first time in my life, but it didn't work out the way I wanted it to... There's a class of some sort and I defined another class (a dialog) as friend of that class, let's say: class SomeOldClass() { friend class dlg_ShowPrivates; public: private: CList m_list; } class dlg_ShowPrivates() { void SetClass(SomeOldClass * pClass) {m_pClass = pClass}; void DoSomethingWithList() { m_pClass->m_list.blablabla; // Do something with the class } protected: SomeOldClass * m_pClass; } The only error I get is on the line where I try to access m_list from m_pClass. I get the old; cannot access private member declared in class SomeOldClass But how come? The dialog is a friend, so should be able to access the privates of SomeOldClass, right? :confused: :confused: Structured programming vs. chaotic mind boggling
-
Hi, I've tried to define a friend class for the first time in my life, but it didn't work out the way I wanted it to... There's a class of some sort and I defined another class (a dialog) as friend of that class, let's say: class SomeOldClass() { friend class dlg_ShowPrivates; public: private: CList m_list; } class dlg_ShowPrivates() { void SetClass(SomeOldClass * pClass) {m_pClass = pClass}; void DoSomethingWithList() { m_pClass->m_list.blablabla; // Do something with the class } protected: SomeOldClass * m_pClass; } The only error I get is on the line where I try to access m_list from m_pClass. I get the old; cannot access private member declared in class SomeOldClass But how come? The dialog is a friend, so should be able to access the privates of SomeOldClass, right? :confused: :confused: Structured programming vs. chaotic mind boggling
OK, I've found the cause of this problem. Apparantly the friend-class definition is valid JUST for the section in front of which you define it! So this definition: class SomeOldClass() { friend class dlg_ShowPrivates; public: private: CList m_list; } defines dlg_ShowPrivates as a friend of the next section in the definition which is a PUBLIC section (which makes it a pretty stupid line of code). For the desired effect you should define it as follows: class SomeOldClass() { public: friend class dlg_ShowPrivates; private: CList m_list; } Structured programming vs. chaotic mind boggling
-
Hi, I've tried to define a friend class for the first time in my life, but it didn't work out the way I wanted it to... There's a class of some sort and I defined another class (a dialog) as friend of that class, let's say: class SomeOldClass() { friend class dlg_ShowPrivates; public: private: CList m_list; } class dlg_ShowPrivates() { void SetClass(SomeOldClass * pClass) {m_pClass = pClass}; void DoSomethingWithList() { m_pClass->m_list.blablabla; // Do something with the class } protected: SomeOldClass * m_pClass; } The only error I get is on the line where I try to access m_list from m_pClass. I get the old; cannot access private member declared in class SomeOldClass But how come? The dialog is a friend, so should be able to access the privates of SomeOldClass, right? :confused: :confused: Structured programming vs. chaotic mind boggling
I've compiled your code (with some syntactic errors cleared out) and it works fine. Are you sure the piece of code you're trying to put to work resembles what you've posted?
-
OK, I've found the cause of this problem. Apparantly the friend-class definition is valid JUST for the section in front of which you define it! So this definition: class SomeOldClass() { friend class dlg_ShowPrivates; public: private: CList m_list; } defines dlg_ShowPrivates as a friend of the next section in the definition which is a PUBLIC section (which makes it a pretty stupid line of code). For the desired effect you should define it as follows: class SomeOldClass() { public: friend class dlg_ShowPrivates; private: CList m_list; } Structured programming vs. chaotic mind boggling
Your explanation of the problem is wrong. Friendship grant access to every section of the class, regardless of where the "friend" line is laid down. You better recheck your code to see if you've made any other change than that
-
I've compiled your code (with some syntactic errors cleared out) and it works fine. Are you sure the piece of code you're trying to put to work resembles what you've posted?
aparently it doesn't, because the example works with you... here's a stripped out version of the REAL class that wanted te have a friend.. class Class_Profile : public Class_Object { // PUTTING THE FRIEND DECLARATION HERE IT DIDN'T WORK public: Class_Profile(Class_Main * pMain); ~Class_Profile(); /* interface */ public: // lots of functions here /* end of interface */ /* methods */ protected: // lots of methods private: // more methods /* attributes */ protected: // a couple of members // PUTTING THE FRIEND DECLARATION HERE IT WORKED private: // lots of members, one of which the one I wanted to acces: Class_Aggregation m_Display; }; Structured programming vs. chaotic mind boggling
-
aparently it doesn't, because the example works with you... here's a stripped out version of the REAL class that wanted te have a friend.. class Class_Profile : public Class_Object { // PUTTING THE FRIEND DECLARATION HERE IT DIDN'T WORK public: Class_Profile(Class_Main * pMain); ~Class_Profile(); /* interface */ public: // lots of functions here /* end of interface */ /* methods */ protected: // lots of methods private: // more methods /* attributes */ protected: // a couple of members // PUTTING THE FRIEND DECLARATION HERE IT WORKED private: // lots of members, one of which the one I wanted to acces: Class_Aggregation m_Display; }; Structured programming vs. chaotic mind boggling
well ,it definitely beats me. I see no reason why the code will work locating the friend declaration in a different line. It'd help if you could post two real (ie, compilable) projects showing both behaviors. good luck joaquín
-
well ,it definitely beats me. I see no reason why the code will work locating the friend declaration in a different line. It'd help if you could post two real (ie, compilable) projects showing both behaviors. good luck joaquín
Hmmm, I tried to make a sample project which didn't work (in the way I described), but I couldn't do it, because it just works... Ridiculously enough I then tried to restore my own code in the original project, so that it would produce the error again, but now it just works! I'm absolutely sure I'm doing everything in the same way I did it before, but I just can't get it to not-work anymore... :confused: X| I must have seem a weird anomaly here, but thankt for the remarks! I was making wrong assumptions there, which was wrong. I'm glad it works now the way it should! Structured programming vs. chaotic mind boggling
-
Hmmm, I tried to make a sample project which didn't work (in the way I described), but I couldn't do it, because it just works... Ridiculously enough I then tried to restore my own code in the original project, so that it would produce the error again, but now it just works! I'm absolutely sure I'm doing everything in the same way I did it before, but I just can't get it to not-work anymore... :confused: X| I must have seem a weird anomaly here, but thankt for the remarks! I was making wrong assumptions there, which was wrong. I'm glad it works now the way it should! Structured programming vs. chaotic mind boggling
irreproducible error --> no error! ;) cheers