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. Managed C++/CLI
  4. Two classes

Two classes

Scheduled Pinned Locked Moved Managed C++/CLI
c++helpquestion
7 Posts 3 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.
  • M Offline
    M Offline
    Michael Mac
    wrote on last edited by
    #1

    I've got a problem which I can't solve. I want to have 2 classes and both of them to have a pointer to each other...

    public __gc class a
    {
    public:
    a::a()
    {
    m_b->bb();
    }

    int aa()
    {
        return 1;
    }
    

    private:
    b* m_b;
    };

    public __gc class b
    {
    public:
    b::b()
    {
    m_a->aa();
    }
    int bb()
    {
    return 1;
    }
    private:
    a* m_a;
    };

    The code isn't able to compile. One solution is to use Object* instead of b* in the class a but I'll have to move my declaration to a cpp file and cast the pointer every time I want to use it. Mayby another solution? :confused: :confused: 43 68 65 65 72 73 2c 4d 69 63 68 61 65 6c

    J M 2 Replies Last reply
    0
    • M Michael Mac

      I've got a problem which I can't solve. I want to have 2 classes and both of them to have a pointer to each other...

      public __gc class a
      {
      public:
      a::a()
      {
      m_b->bb();
      }

      int aa()
      {
          return 1;
      }
      

      private:
      b* m_b;
      };

      public __gc class b
      {
      public:
      b::b()
      {
      m_a->aa();
      }
      int bb()
      {
      return 1;
      }
      private:
      a* m_a;
      };

      The code isn't able to compile. One solution is to use Object* instead of b* in the class a but I'll have to move my declaration to a cpp file and cast the pointer every time I want to use it. Mayby another solution? :confused: :confused: 43 68 65 65 72 73 2c 4d 69 63 68 61 65 6c

      J Offline
      J Offline
      J Patel
      wrote on last edited by
      #2

      Can't you forward declare class b? Jignesh

      M 1 Reply Last reply
      0
      • M Michael Mac

        I've got a problem which I can't solve. I want to have 2 classes and both of them to have a pointer to each other...

        public __gc class a
        {
        public:
        a::a()
        {
        m_b->bb();
        }

        int aa()
        {
            return 1;
        }
        

        private:
        b* m_b;
        };

        public __gc class b
        {
        public:
        b::b()
        {
        m_a->aa();
        }
        int bb()
        {
        return 1;
        }
        private:
        a* m_a;
        };

        The code isn't able to compile. One solution is to use Object* instead of b* in the class a but I'll have to move my declaration to a cpp file and cast the pointer every time I want to use it. Mayby another solution? :confused: :confused: 43 68 65 65 72 73 2c 4d 69 63 68 61 65 6c

        M Offline
        M Offline
        Maciej Pirog
        wrote on last edited by
        #3

        Use __single_inheritance keyword! This is an example:

        //Declarations:
        public __gc class __single_inheritance b;
        public __gc class a;
        //Definitions:
        public __gc class a
        {
        public:
        a::a(){}
        public:
        b* m_b;
        System::String* stringInClassA;
        };

        public __gc class b
        {
        public:
        b::b(){}
        public:
        a* m_a;
        System::String* stringInClassB;
        };

        // This is the entry point for this application
        int _tmain(void)
        {
        a* A = new a();
        b* B = new b();
        A->stringInClassA = new String("This is string in class A");
        B->stringInClassB = new String("This is string in class B");
        A->m_b = B;
        B->m_a = A;
        Console::WriteLine(A->m_b->GetType()->ToString());
        Console::WriteLine(B->m_a->GetType()->ToString());
        Console::WriteLine("");
        Console::WriteLine("from class A:");
        Console::WriteLine(A->m_b->stringInClassB);
        Console::WriteLine("");
        Console::WriteLine("from class B:");
        Console::WriteLine(B->m_a->stringInClassA);
        int i = Console::Read ();

        return 0;
        }

        The matrix has you :suss: MP Maciej Pirog

        M 1 Reply Last reply
        0
        • M Maciej Pirog

          Use __single_inheritance keyword! This is an example:

          //Declarations:
          public __gc class __single_inheritance b;
          public __gc class a;
          //Definitions:
          public __gc class a
          {
          public:
          a::a(){}
          public:
          b* m_b;
          System::String* stringInClassA;
          };

          public __gc class b
          {
          public:
          b::b(){}
          public:
          a* m_a;
          System::String* stringInClassB;
          };

          // This is the entry point for this application
          int _tmain(void)
          {
          a* A = new a();
          b* B = new b();
          A->stringInClassA = new String("This is string in class A");
          B->stringInClassB = new String("This is string in class B");
          A->m_b = B;
          B->m_a = A;
          Console::WriteLine(A->m_b->GetType()->ToString());
          Console::WriteLine(B->m_a->GetType()->ToString());
          Console::WriteLine("");
          Console::WriteLine("from class A:");
          Console::WriteLine(A->m_b->stringInClassB);
          Console::WriteLine("");
          Console::WriteLine("from class B:");
          Console::WriteLine(B->m_a->stringInClassA);
          int i = Console::Read ();

          return 0;
          }

          The matrix has you :suss: MP Maciej Pirog

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

          Ok, but try to call a bb function on the m_b instance in the a class contructor. It won't compile. Any other tips? :cool: 43 68 65 65 72 73 2c 4d 69 63 68 61 65 6c

          M 1 Reply Last reply
          0
          • J J Patel

            Can't you forward declare class b? Jignesh

            M Offline
            M Offline
            Michael Mac
            wrote on last edited by
            #5

            It won't compile too because class b has a field m_a of class a. 43 68 65 65 72 73 2c 4d 69 63 68 61 65 6c

            1 Reply Last reply
            0
            • M Michael Mac

              Ok, but try to call a bb function on the m_b instance in the a class contructor. It won't compile. Any other tips? :cool: 43 68 65 65 72 73 2c 4d 69 63 68 61 65 6c

              M Offline
              M Offline
              Maciej Pirog
              wrote on last edited by
              #6

              Yoe'r right :-O But I've got an idea! Put: 1st classes declarations 2nd classes definitions and functions declarations 3rd functions definitions Example:

              // Classes declarations:
              public __gc class __single_inheritance b;
              public __gc class __single_inheritance a;
              // Classes definitions and functions *declarations*:
              public __gc class a
              {
              public:
              void AskClassBToWrite();
              void WriteA();
              b* m_b;
              };

              public __gc class b
              {
              public:
              void AskClassAToWrite();
              void WriteB();
              a* m_a;
              };
              // functions *definitions*
              void a::WriteA()
              {
              Console::Write("WriteA function was called");
              }

              void a::AskClassBToWrite()
              {
              a::m_b->WriteB();
              }

              void b::WriteB()
              {
              Console::Write("WriteB function was called");
              }

              void b::AskClassAToWrite()
              {
              b::m_a->WriteA();
              }

              // This is the entry point for this application
              int _tmain(void)
              {
              a* A = new a();
              b* B = new b();

              A->m\_b = B;
              B->m\_a = A;
              
              A->AskClassBToWrite();
              B->AskClassAToWrite();
              
              Console::Read();
              return 0;
              

              }

              This will work! :) MP Maciej Pirog

              M 1 Reply Last reply
              0
              • M Maciej Pirog

                Yoe'r right :-O But I've got an idea! Put: 1st classes declarations 2nd classes definitions and functions declarations 3rd functions definitions Example:

                // Classes declarations:
                public __gc class __single_inheritance b;
                public __gc class __single_inheritance a;
                // Classes definitions and functions *declarations*:
                public __gc class a
                {
                public:
                void AskClassBToWrite();
                void WriteA();
                b* m_b;
                };

                public __gc class b
                {
                public:
                void AskClassAToWrite();
                void WriteB();
                a* m_a;
                };
                // functions *definitions*
                void a::WriteA()
                {
                Console::Write("WriteA function was called");
                }

                void a::AskClassBToWrite()
                {
                a::m_b->WriteB();
                }

                void b::WriteB()
                {
                Console::Write("WriteB function was called");
                }

                void b::AskClassAToWrite()
                {
                b::m_a->WriteA();
                }

                // This is the entry point for this application
                int _tmain(void)
                {
                a* A = new a();
                b* B = new b();

                A->m\_b = B;
                B->m\_a = A;
                
                A->AskClassBToWrite();
                B->AskClassAToWrite();
                
                Console::Read();
                return 0;
                

                }

                This will work! :) MP Maciej Pirog

                M Offline
                M Offline
                Michael Mac
                wrote on last edited by
                #7

                Thanks man. It's working. :) :) :)


                43 68 65 65 72 73 2c 4d 69 63 68 61 65 6c

                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