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. Managed C++/CLI
  4. Nested class; accessing non-static members from the enclosing class

Nested class; accessing non-static members from the enclosing class

Scheduled Pinned Locked Moved Managed C++/CLI
tutorialquestion
5 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
    Douglas Dean
    wrote on last edited by
    #1

    Is there any way in a nested class to access non-static members from the current instance of the enclosing class? ex. class outer { public: int m_value; int m_value2 class inner { int getAccess() { // ?? How to do it ?? return **(??)**m_value; } } } Thanks Doug

    D 1 Reply Last reply
    0
    • D Douglas Dean

      Is there any way in a nested class to access non-static members from the current instance of the enclosing class? ex. class outer { public: int m_value; int m_value2 class inner { int getAccess() { // ?? How to do it ?? return **(??)**m_value; } } } Thanks Doug

      D Offline
      D Offline
      Douglas Dean
      wrote on last edited by
      #2

      Make that...__gc class outer { public: int m_value; int m_value2 __gc class inner { int getAccess() { // ?? How to do it ?? return (??)m_value; } } } ...forgot the managed qualifiers...

      2 1 Reply Last reply
      0
      • D Douglas Dean

        Make that...__gc class outer { public: int m_value; int m_value2 __gc class inner { int getAccess() { // ?? How to do it ?? return (??)m_value; } } } ...forgot the managed qualifiers...

        2 Offline
        2 Offline
        2bee
        wrote on last edited by
        #3

        Hi Douglas, I don't know exactly if this helps but I'll give it a try. Basically i have just converted the C# example of nested classes into C++/CLI. It seems to me that this is a way to get it working (it actually did work): public ref class Container { public: Container(void); private: int m_value; public: void SetValue(int pVal) { m_value = pVal; } public: ref class InnerClass { private: Container^ m_parent; public: InnerClass(void) {}; InnerClass(Container^ parent) { m_parent = parent; } public: int getAccess() { return m_parent->m_value; } }; }; Container^ outer = gcnew Container(); Container::InnerClass^ nested = gcnew Container::InnerClass(outer); Console::WriteLine(nested->getAccess()); // prints 4 outer->SetValue(5); Console::WriteLine(nested->getAccess()); // prints 5 hope this helps, best regards Tobias

        D 2 Replies Last reply
        0
        • 2 2bee

          Hi Douglas, I don't know exactly if this helps but I'll give it a try. Basically i have just converted the C# example of nested classes into C++/CLI. It seems to me that this is a way to get it working (it actually did work): public ref class Container { public: Container(void); private: int m_value; public: void SetValue(int pVal) { m_value = pVal; } public: ref class InnerClass { private: Container^ m_parent; public: InnerClass(void) {}; InnerClass(Container^ parent) { m_parent = parent; } public: int getAccess() { return m_parent->m_value; } }; }; Container^ outer = gcnew Container(); Container::InnerClass^ nested = gcnew Container::InnerClass(outer); Console::WriteLine(nested->getAccess()); // prints 4 outer->SetValue(5); Console::WriteLine(nested->getAccess()); // prints 5 hope this helps, best regards Tobias

          D Offline
          D Offline
          Douglas Dean
          wrote on last edited by
          #4

          Yeah, that's what I ended up doing. I wonder if there's a cleaner way, though...

          1 Reply Last reply
          0
          • 2 2bee

            Hi Douglas, I don't know exactly if this helps but I'll give it a try. Basically i have just converted the C# example of nested classes into C++/CLI. It seems to me that this is a way to get it working (it actually did work): public ref class Container { public: Container(void); private: int m_value; public: void SetValue(int pVal) { m_value = pVal; } public: ref class InnerClass { private: Container^ m_parent; public: InnerClass(void) {}; InnerClass(Container^ parent) { m_parent = parent; } public: int getAccess() { return m_parent->m_value; } }; }; Container^ outer = gcnew Container(); Container::InnerClass^ nested = gcnew Container::InnerClass(outer); Console::WriteLine(nested->getAccess()); // prints 4 outer->SetValue(5); Console::WriteLine(nested->getAccess()); // prints 5 hope this helps, best regards Tobias

            D Offline
            D Offline
            Douglas Dean
            wrote on last edited by
            #5

            Thanks, BTW...

            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