Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. friend class ain't my friend

friend class ain't my friend

Scheduled Pinned Locked Moved C / C++ / MFC
visual-studiohelpquestion
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    D D de Kerf
    wrote on last edited by
    #1

    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

    D J 2 Replies Last reply
    0
    • D D D de Kerf

      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

      D Offline
      D Offline
      D D de Kerf
      wrote on last edited by
      #2

      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

      J 1 Reply Last reply
      0
      • D D D de Kerf

        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

        J Offline
        J Offline
        Joaquin M Lopez Munoz
        wrote on last edited by
        #3

        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?

        D 1 Reply Last reply
        0
        • D D D de Kerf

          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

          J Offline
          J Offline
          Joaquin M Lopez Munoz
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • J Joaquin M Lopez Munoz

            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?

            D Offline
            D Offline
            D D de Kerf
            wrote on last edited by
            #5

            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

            J 1 Reply Last reply
            0
            • D D D de Kerf

              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

              J Offline
              J Offline
              Joaquin M Lopez Munoz
              wrote on last edited by
              #6

              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

              D 1 Reply Last reply
              0
              • J Joaquin M Lopez Munoz

                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

                D Offline
                D Offline
                D D de Kerf
                wrote on last edited by
                #7

                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

                J 1 Reply Last reply
                0
                • D D D de Kerf

                  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

                  J Offline
                  J Offline
                  Joaquin M Lopez Munoz
                  wrote on last edited by
                  #8

                  irreproducible error --> no error! ;) cheers

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups