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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Inner classes

Inner classes

Scheduled Pinned Locked Moved C / C++ / MFC
helpcomtutorialquestion
9 Posts 4 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.
  • A Offline
    A Offline
    Alexandru Savescu
    wrote on last edited by
    #1

    Hello, I am declaring a class within another class. In VC 7.0 the inner class has access to protected members of the enclosing class, but in VC 6.0 it does not. Here is the example:

    #include <iostream>

    class Big
    {
    protected:
    static const int CONST_VALUE;

    class Inner
    {
    public:
    Inner ();
    int x;
    };

    public:
    Inner in;
    };

    const int Big::CONST_VALUE = 2;
    Big::Inner::Inner ()
    {
    x = Big::CONST_VALUE; /* <- error here in VC 6, cannot access protected member etc...
    If I declare Inner as fried of Big it works well */
    }

    int main ()
    {
    Big big;
    std::cout << big.in.x;
    }

    Can anyone tell me if there is a bug in the(se) compiler(s)? Thanks! Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

    A R M 3 Replies Last reply
    0
    • A Alexandru Savescu

      Hello, I am declaring a class within another class. In VC 7.0 the inner class has access to protected members of the enclosing class, but in VC 6.0 it does not. Here is the example:

      #include <iostream>

      class Big
      {
      protected:
      static const int CONST_VALUE;

      class Inner
      {
      public:
      Inner ();
      int x;
      };

      public:
      Inner in;
      };

      const int Big::CONST_VALUE = 2;
      Big::Inner::Inner ()
      {
      x = Big::CONST_VALUE; /* <- error here in VC 6, cannot access protected member etc...
      If I declare Inner as fried of Big it works well */
      }

      int main ()
      {
      Big big;
      std::cout << big.in.x;
      }

      Can anyone tell me if there is a bug in the(se) compiler(s)? Thanks! Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

      A Offline
      A Offline
      Alvaro Mendez
      wrote on last edited by
      #2

      I can't tell for certain, but it appears to me as a bug in the VC7 compiler. I could be wrong, of course, since VC7 is supposed to be more compliant -- I don't have the ARM with me. But it just seems to me like protected means that it's accessible to derived classes. There's no provision for it being accessible to nested classes. Regards, Alvaro


      Well done is better than well said. -- Benjamin Franklin (I actually prefer medium-well.)

      1 Reply Last reply
      0
      • A Alexandru Savescu

        Hello, I am declaring a class within another class. In VC 7.0 the inner class has access to protected members of the enclosing class, but in VC 6.0 it does not. Here is the example:

        #include <iostream>

        class Big
        {
        protected:
        static const int CONST_VALUE;

        class Inner
        {
        public:
        Inner ();
        int x;
        };

        public:
        Inner in;
        };

        const int Big::CONST_VALUE = 2;
        Big::Inner::Inner ()
        {
        x = Big::CONST_VALUE; /* <- error here in VC 6, cannot access protected member etc...
        If I declare Inner as fried of Big it works well */
        }

        int main ()
        {
        Big big;
        std::cout << big.in.x;
        }

        Can anyone tell me if there is a bug in the(se) compiler(s)? Thanks! Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

        R Offline
        R Offline
        Rickard Andersson20
        wrote on last edited by
        #3

        You've declared the Inner class as protected. declare it as public and you'll get it to work! Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C#, ASP.NET and C++!

        1 Reply Last reply
        0
        • A Alexandru Savescu

          Hello, I am declaring a class within another class. In VC 7.0 the inner class has access to protected members of the enclosing class, but in VC 6.0 it does not. Here is the example:

          #include <iostream>

          class Big
          {
          protected:
          static const int CONST_VALUE;

          class Inner
          {
          public:
          Inner ();
          int x;
          };

          public:
          Inner in;
          };

          const int Big::CONST_VALUE = 2;
          Big::Inner::Inner ()
          {
          x = Big::CONST_VALUE; /* <- error here in VC 6, cannot access protected member etc...
          If I declare Inner as fried of Big it works well */
          }

          int main ()
          {
          Big big;
          std::cout << big.in.x;
          }

          Can anyone tell me if there is a bug in the(se) compiler(s)? Thanks! Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          class Big
          {
          protected:
          static const int CONST_VALUE;
          class Inner
          {
          public:
          Inner ();
          int x;
          };
          friend class Inner;
          //...
          };

          Add the friend declaration, that should do the trick. --Mike-- Friday's GoogleFight results: Britney Spears 2,190,000 - Erica Weichers 23 :( 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click! My really out-of-date homepage Sonork-100.19012 Acid_Helm

          A 1 Reply Last reply
          0
          • M Michael Dunn

            class Big
            {
            protected:
            static const int CONST_VALUE;
            class Inner
            {
            public:
            Inner ();
            int x;
            };
            friend class Inner;
            //...
            };

            Add the friend declaration, that should do the trick. --Mike-- Friday's GoogleFight results: Britney Spears 2,190,000 - Erica Weichers 23 :( 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click! My really out-of-date homepage Sonork-100.19012 Acid_Helm

            A Offline
            A Offline
            Alexandru Savescu
            wrote on last edited by
            #5

            Hello, I know that if I declare Inner as friend it works on VC 6.0. My problem is: Which compiler's bug is this? Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

            R M 2 Replies Last reply
            0
            • A Alexandru Savescu

              Hello, I know that if I declare Inner as friend it works on VC 6.0. My problem is: Which compiler's bug is this? Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

              R Offline
              R Offline
              Rickard Andersson20
              wrote on last edited by
              #6

              But it is not a compiler bug! Protected members can ONLY be accessed through inheritance. Do it piblic or as Dunn said friend. Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C#, ASP.NET and C++!

              A 1 Reply Last reply
              0
              • R Rickard Andersson20

                But it is not a compiler bug! Protected members can ONLY be accessed through inheritance. Do it piblic or as Dunn said friend. Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C#, ASP.NET and C++!

                A Offline
                A Offline
                Alexandru Savescu
                wrote on last edited by
                #7

                Rickard Andersson wrote: Protected members can ONLY be accessed through inheritance. I understand that perfectly. My problem is why my sample compiles in VC 7.0! :confused: Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

                R 1 Reply Last reply
                0
                • A Alexandru Savescu

                  Rickard Andersson wrote: Protected members can ONLY be accessed through inheritance. I understand that perfectly. My problem is why my sample compiles in VC 7.0! :confused: Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

                  R Offline
                  R Offline
                  Rickard Andersson20
                  wrote on last edited by
                  #8

                  Alexandru Savescu wrote: My problem is why my sample compiles in VC 7.0 :wtf::wtf::wtf::wtf::wtf: That's a bug... or something... weird! Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C#, ASP.NET and C++!

                  1 Reply Last reply
                  0
                  • A Alexandru Savescu

                    Hello, I know that if I declare Inner as friend it works on VC 6.0. My problem is: Which compiler's bug is this? Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

                    M Offline
                    M Offline
                    Michael Dunn
                    wrote on last edited by
                    #9

                    Alexandru Savescu wrote: Which compiler's bug is this? That, I don't know. Needing the friend declaration just seemed like The Way It Was Done. You should probably consult the C++ spec for the definitive answer. --Mike-- Friday's GoogleFight results: Britney Spears 2,190,000 - Erica Weichers 23 :( 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click! My really out-of-date homepage Sonork-100.19012 Acid_Helm

                    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