Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C#
i have two classes between which i need to have parent -child realtion.
Say there is a class A in project 1 and another class B in project 2.
Now i want A has list of type B as its child,and B has a variable of type A as its parent.
NOTE THAT THEY ARE IN DIFFERENT PROJECTS,which exists in the same solution of visual studio.Is this the correct appproach as its causing CIRCULAR REFERENCE.
Is there some other way in which a requirement like this can be handled without having circular reference ???
Circular reference is causing problems during build.
Posted 11-Nov-12 20:25pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

In C#, such problem does not exist in principle; in C++, "forward declaration" helps.
 
class Second; // this is forward declaration;
// without it, the class First will not compile

class First {
   Second* second;
};
 
class Second {
   First* first;
};
 
—SA
  Permalink  
Comments
n.manish at 12-Nov-12 4:02am
   
But do u agree the fact that the above scenario that i have mentioned creates circular reference,and because of that circular reference when i m building one of the two projects its throwing error stating reference problem
Sergey Alexandrovich Kryukov at 12-Nov-12 11:46am
   
Yes of course, and this circular reference is perfectly fine. You only need to take care of preventing infinite construction of objects, which is not the case in your situation. You need to use pointers for storing a collection of children, and for referencing a parent. This way, you can design an object graph which does not even have to be a tree (that is, it could contain any number of circular paths through references -- even this won't be a problem).
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Solution 1 will solve the compiler issue, but if I get you correctly yours is a linker problem: you cannot link either project, because the implementation references the API of the class in the other library, and therefore at the very least needs the .lib file.
 
One way to fix this would be:
 
1. Temporarily change the implementation in library 1 by removing the code that references library 2.
 
2. Also remove the #include and the reference to library 2 in the linker settings
 
3. build library 1 (it now no longer requires library 2)
 
4. copy the resulting .lib file to wherever library 2 expects it
 
5. build library 2
 
6. copy the library 2 .lib file to wherever library 1 expects it
 
7. Restore library 1 to its original state
 
8. Rebuild it
 
After this you should not need to repeat these steps again, unless you simultaneously change the API of both libraries. (so avoid that!)
 
The rationale behind this is that the .lib file is to the linker what the header files are to the compiler. So to link one library, you need the .lib file of the other, but you won't get that .lib if the link fails. Anyhow, the .lib file only contains the API information, and it doesn't matter if you change the implementation later.
 
Of course, the cleaner solution would be to put the two classes out of these projects and into a new, third project, then reference that one from the other two.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Maybe I'm missing something, but why not just use a common interface from a separate library that both could reference?
 
InterfaceLib.dll
namespace CircRefProb.Interfaces
{
    public interface IParent
    {
    }
}
namespace CircRefProb.Interfaces
{
    public interface IChild
    {
    }
}
 
ParentLib.dll
using System;
using System.Collections.Generic;
using CircRefProb.Interfaces;
 
namespace Parent
{
    public class ParentNode: IParent
    {
        public List<IChild> Children { get; set; }
    }
}
 
ChildLib.dll
using System.Collections.Generic;
using CircRefProb.Interfaces;
 
namespace Child
{
    public class ChildNode: IChild
    {
        public IParent Parent {get;set;}
    }
}
  Permalink  
v2
Comments
n.manish at 15-Nov-12 4:30am
   
i had read about this Interface solution at some place.Was thinking of doing this .For the time being inorder avoid circular reference i have made the parent subscribe to childs event which notifies the parent and the parent then does the needful. Thanks for sharing your solution guys ,will click on the Accept Solution button if take any of these solutions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 363
1 OriginalGriff 344
2 George Jonsson 238
3 Shemeemsha RA 148
4 Animesh Datta 130
0 OriginalGriff 6,179
1 Sergey Alexandrovich Kryukov 5,616
2 CPallini 4,770
3 George Jonsson 3,400
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 12 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100