CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2008
    Posts
    12

    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.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: self containing classes?

    Possible, sure, but probably not practical. Any reason you don't want to use a container class?

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  4. #4
    Join Date
    Jan 2009
    Posts
    1,689

    Re: self containing classes?

    Code:
    class Atom{
        vector<Atom *> Bonded;
    }

  5. #5
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: self containing classes?

    Quote Originally Posted by ninja9578 View Post
    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

  6. #6
    Join Date
    Aug 2007
    Posts
    858

    Re: self containing classes?

    Quote Originally Posted by Etherous View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured