-
April 21st, 2009, 01:14 PM
#1
self containing classes?
I'd like to have a class (atoms), where each instance maintains a list of other instances as a data member (a list of bonded atoms). Is this possible? I just tried writing it up as if it were and I got an error in the constructor.
-
April 21st, 2009, 01:19 PM
#2
Re: self containing classes?
Possible, sure, but probably not practical. Any reason you don't want to use a container class?
-
April 21st, 2009, 01:28 PM
#3
Re: self containing classes?
You may need to make a list of pointers to the other bonded atoms. Or indexes.
This is the old "How do I design a graph class" problem. I've seen a few generic attempts at it, but rarely good ones. You pretty much just have to design what you need each time.
-
April 21st, 2009, 01:41 PM
#4
Re: self containing classes?
Code:
class Atom{
vector<Atom *> Bonded;
}
-
April 21st, 2009, 03:18 PM
#5
Re: self containing classes?
Originally Posted by ninja9578
Code:
class Atom{
vector<Atom *> Bonded;
}
I'd actually recommend making your own container class that not only stores the bonded atoms, but signifies type and strength of the bond, etc., but otherwise just like this
Intel Core Duo Macbook w/ Mac OS 10.5.6
gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1
-
April 21st, 2009, 04:14 PM
#6
Re: self containing classes?
Originally Posted by Etherous
I'd actually recommend making your own container class that not only stores the bonded atoms, but signifies type and strength of the bond, etc., but otherwise just like this
Why go to the trouble of writing your own container class? You could just do something like:
Code:
class Atom;
class BondedAtom
{
BondType type;
BondStrength strength;
Atom* atom;
// etc
};
class Atom
{
vector<BondedAtom> bonded;
};
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|