CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 1999
    Posts
    7

    circular references



    Hi,


    How can a use a circular reference in Visual C++? I want to create a pointer

    of one class, classA, to point to another class, classB, (classB* ptrB) and set up a pointer from classB to point at classA (classA* ptrA). However, I can't

    seem include classB in classA as well as including classA in classB. In Object

    Pascal one can do this by including the other class in the variable uses clause.


    Thanks for your help

  2. #2
    Join Date
    Mar 1999
    Location
    Roswell, GA
    Posts
    73

    Re: circular references



    Use a forward declaration. OO purest forgive me for leading someone so young astray.

    class B; //Just a promise to the compiler that you will define a class B sooner or later;

    Class A : public CObject

    .

    .

    public:

    B* pB; //A pointer to class B, the compiler will not mind because you already

    //promised to define a class B.

    }; // end of class A

    Class B

    {

    }; // end of class b


    Then, in the constructor or OnInit of class B just

    pB = this;


    Good luck.



  3. #3
    Join Date
    Mar 1999
    Location
    Roswell, GA
    Posts
    73

    Re: circular references



    Use a forward declaration. OO purest forgive me for leading someone so young astray.

    class B; //Just a promise to the compiler that you will define a class B sooner or later;

    Class A : public CObject

    .

    .

    public:

    B* pB; //A pointer to class B, the compiler will not mind because you already

    //promised to define a class B.

    }; // end of class A

    Class B

    {

    }; // end of class b


    Then, in the constructor or OnInit of class B just

    pB = this;


    Good luck.



  4. #4
    Join Date
    Mar 1999
    Posts
    7

    Re: circular references



    Yes, but I didn't want the two classes in the same unit.

  5. #5
    Join Date
    Mar 1999
    Location
    Roswell, GA
    Posts
    73

    Re: circular references



    Sorry Mary. I guess I do not understand what you mean by "same unit". If same unit means same source file, then there is no problem. A forward reference just requires a single h file in common for both proto-types.

  6. #6
    Join Date
    Mar 1999
    Location
    Roswell, GA
    Posts
    73

    Re: circular references



    Sorry Mary. I guess I do not understand what you mean by "same unit". If same unit means same source file, then there is no problem. A forward reference just requires a single h file in common for both proto-types.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured