circular dependency problem

A

abir

Hi,
I have some circular dependency problem while having these 3
classes.

1) class Context holds vector of Char & Word
2) Char class has index of Word to which it belongs, in the vector &
a ref to Context
3) Word class has 2 index denoting begin & end char for the Word in
the vector & a ref to context.

Now, Char class wants to return the pointer to Word (iterator)
and Word class wants to return a pair of iterator saying the set of
Char's for it.

eg,

#include "Word.h"
#include "Char.h"
struct Context{
std::vector<Word> words_;
std::vector<Char> chars_;
};

//Word.h
class Context;
struct Word{
std::size_t begin;
std::size_t end;
Context& ctx_;
Word(Context& ctx) :ctx_(ctx){}
std::pair<vector<Char>::iterator,vector<Char>::iterator > chars();//i
need to include Char.h for this
std::pair<Char*,Char*> chars();///This version works with
forward declaration.
};
//Char.h

struct Char{
std::size_t word;
vector<Word>::iterator word();///i need to include Word.h for
this
Word* word();///This works, and equiv to above function for
vector.
};

Now as I can't forward declare iterator for vector , Word & Char goes
for cyclic dependency,
However if i return a pointer to Word and Char (any one breaks
dependency problem), which works for case of vector, the problem is
not there.

Can anyone specify how to have it in terms of iterator ?

Thanks
abir
 
D

Diego Martins

Hi,
I have some circular dependency problem while having these 3
classes. [snip]
//Char.h

struct Char{
std::size_t word;
vector<Word>::iterator word();///i need to include Word.h for
this
Word* word();///This works, and equiv to above function for
vector.

};

Now as I can't forward declare iterator for vector , Word & Char goes
for cyclic dependency,
However if i return a pointer to Word and Char (any one breaks
dependency problem), which works for case of vector, the problem is
not there.

Can anyone specify how to have it in terms of iterator ?

Using iterators as indexes to vectors are quite dangerous
Use an integer or create a WordIndex class (wrapping an int or an
unsigned) and make word() function returning that:

class WordIndex;
struct Char{
std::size_t word; // size_t????? what for? why not using
unsigned or unsigned long?
WordIndex word();
};

......

and define WordIndex in other header file, included by the TU of Char
class code

Diego
 
D

dp1978x

Hi,
I have some circular dependency problem while having these 3
classes.
[snip]

Now as I can't forward declare iterator for vector , Word & Char goes
for cyclic dependency,

You only need to forward-declare the element type for your vectors in
order to use iterators:

class Word;
....
vector <Word>::iterator word();
 
A

abir

Hi,
I have some circular dependency problem while having these 3
classes.
[snip]

Now as I can't forward declare iterator for vector , Word & Char goes
for cyclic dependency,

You only need to forward-declare the element type for your vectors in
order to use iterators:

class Word;
...
vector <Word>::iterator word();

Thanks,
I "discovered" this method which i thought should not work....
Now, i see it is possible to write
class Word;
typedef std::vector<Word> WordVector; as a definition, only i cant do
it during linkage time (i.e inside a function etc) which is quite
logical.
So forward declaration of iterator of a container of unknown class is
possible, just as it is possible to forward declare a class when only
pointer or reference is needed.
Thanks for reminding me of a basic concept which i had forgot.

and to Diego,
I am doing just the opposite. I can't store iterator of Word in Char
class, as the std::vector<Word> is dynamic, and will invalidate
iterator. So i store word index to Char class, and while returning
construct a iterator out of it using the container, instead of
directly returning the index.
T think there is one typo mistake (and wrong naming ) which caused
the misunderstanding.

The correct thing is,

//Char.h
#include "Word_fwd.h"
struct Char{
std::size_t wordIndex_;
WordVector::iterator word();///now i can do it using the
forward declaration of Word
Context* ctx_;
Char(Context& ctx,std::size_t wordIndex) : ctx_(&ctx) ,
wordIndex_(wordIndex){}
};

//Word_fwd.h
struct Word;
#include <vector>
typedef std::vector<Word> WordVector;

so here wordIndex_ (previously wrongly stated as word) is the index
of the vector to which the word belongs (i.e distance between the
vector begin & this location)

Thanks to all for replying ....
abir
and same for Char class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top