M
mike3
Hi.
I've got this problem. I'm trying to implement a funny kind of "graph"
composed of "beads" which are strung together to form "strings" that
are in turn attached at beads to form the full "graphs". I've got
something like this:
---
class BeadString;
struct Connection {
int dstBeadIdx;
BeadString *dstString;
};
class Bead {
private:
// ...
std::vector<Connection> connections; // connections from this
bead to
// other beads
public:
// ...
void addConnection(int, BeadString *); // Adds a connection to
other beads
// ...
};
class BeadString {
private:
std::vector<Bead> beads;
std::string label;
public:
// ...
BeadString(int, std::string);
// ...
};
class Web {
private:
std::vector<BeadString> strings;
std::string label;
// ...
public:
// ...
void addBeadString(int, std::string);
};
// ...
void Bead::addConnection(int dstBeadIdx, BeadString *dstString)
{
// ...
}
// ...
BeadString::BeadString(int numBeads, std::string label_)
: beads(numBeads),
label(label_)
{
// Connect the beads together.
if(numBeads > 1)
{
beads[0].addConnection(1, this); // add a connection to the next
// bead on the string
for(int i(1);i<numBeads-1;++i)
{
beads.addConnection(i+1, this);
beads.addConnection(i-1, this);
}
beads[numBeads-1].addConnection(numBeads-2, this);
}
}
// ...
void Web::addBeadString(int numBeads, std::string label)
{
// Make a new bead string.
BeadString bs(numBeads, label);
// Add it to the list.
beads.push_back(bs); // FAIL! We try to construct a copy of bead
string bs,
// but the connections between beads in the
bead
// string break since the pointers involved
point to
// bead string bs, not the copy!
}
// ...
---
Now I know that we could resolve this by using, say, a list of
pointers to bead strings instead of actual bead strings, and so avoid
the need to make copies of bs, or have a custom copy constructor that
roots through the connections in each bead (perhaps using special
connection accessors on the beads). But is it possible this signals a
fault with the design itself? If so, what would be a better one?
Remember: each bead needs to know what it's connected to. It just
"seems" off, somehow. But I could be wrong.
I've got this problem. I'm trying to implement a funny kind of "graph"
composed of "beads" which are strung together to form "strings" that
are in turn attached at beads to form the full "graphs". I've got
something like this:
---
class BeadString;
struct Connection {
int dstBeadIdx;
BeadString *dstString;
};
class Bead {
private:
// ...
std::vector<Connection> connections; // connections from this
bead to
// other beads
public:
// ...
void addConnection(int, BeadString *); // Adds a connection to
other beads
// ...
};
class BeadString {
private:
std::vector<Bead> beads;
std::string label;
public:
// ...
BeadString(int, std::string);
// ...
};
class Web {
private:
std::vector<BeadString> strings;
std::string label;
// ...
public:
// ...
void addBeadString(int, std::string);
};
// ...
void Bead::addConnection(int dstBeadIdx, BeadString *dstString)
{
// ...
}
// ...
BeadString::BeadString(int numBeads, std::string label_)
: beads(numBeads),
label(label_)
{
// Connect the beads together.
if(numBeads > 1)
{
beads[0].addConnection(1, this); // add a connection to the next
// bead on the string
for(int i(1);i<numBeads-1;++i)
{
beads.addConnection(i+1, this);
beads.addConnection(i-1, this);
}
beads[numBeads-1].addConnection(numBeads-2, this);
}
}
// ...
void Web::addBeadString(int numBeads, std::string label)
{
// Make a new bead string.
BeadString bs(numBeads, label);
// Add it to the list.
beads.push_back(bs); // FAIL! We try to construct a copy of bead
string bs,
// but the connections between beads in the
bead
// string break since the pointers involved
point to
// bead string bs, not the copy!
}
// ...
---
Now I know that we could resolve this by using, say, a list of
pointers to bead strings instead of actual bead strings, and so avoid
the need to make copies of bs, or have a custom copy constructor that
roots through the connections in each bead (perhaps using special
connection accessors on the beads). But is it possible this signals a
fault with the design itself? If so, what would be a better one?
Remember: each bead needs to know what it's connected to. It just
"seems" off, somehow. But I could be wrong.