Linking Objects between Vectors - Best method

This is a discussion on Linking Objects between Vectors - Best method within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I have a question about how to best proceed on a program I'm trying to make as an ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    Linking Objects between Vectors - Best method

    Hi everyone, I have a question about how to best proceed on a program I'm trying to make as an exercise for myself. I'm not asking for code, but rather for the best method to get me started. I'm pretty new to all this so my explanation might be very confusing.

    So basicly I want to make a program which allows the user to insert a number of nodes in an X-Y plane. So every node would be an object, with 2 int values assigned. So I would first have to define the class nodes and then make a vector container out of it to allow the users to add/delete as many nodes as they want.

    As a second part I'd like to allow the user to make a number of lines, which are objects, consisting out of 2 nodes as defined above. The class lines would also allow a function to calculate length depending on the 2 nodes that are used. Again I'd need to make a vector container out of it as to allow for as many lines as the user wants.

    So how I see it:
    1. define class nodes
    2. create vector consisting of nodes
    3. define class lines, with 2 nodes and one function which calculates the length
    4. create a vector consisting of lines

    However, step 3 is unclear to me. How can you link all this together, so that when you change a node co÷rdinates, the lines will know this happened and the length function will use the correct node co÷rdinates.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    A little psuedo
    Code:
    struct Node
    {
      Node(int px, int py) : x(px), y(py)
      {}
      int x;
      int y;
    };
    
    struct LineSegment
    { 
      LineSegment(Node* pFirst, Node* pSecond) : Point1(pFirst), Point2(pSecond)
      {}
      Node* Point1;
      Node* point2;
    };
    
    function
    {
     Node FirstPoint = Node(2,2);
     Node SecondPoint = Node(4,4);
    
     LineSegment MyLine = LineSegment(&FirstPoint, &SecondPoint);
    }
    Then just modify it a little to incorporate the vectors. Which should be relatively easy to accomplish.

    This response was rushed so may not be perfect.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    I would use this approach:
    Code:
    struct Node
    {
      Node(int px, int py) : x(px), y(py)
      {}
      int x;
      int y;
    };
    
    struct LineSegment
    { 
      LineSegment(Node& First, Node& Second) : Point1(First), Point2(Second)
      {}
      Node& Point1;
      Node& point2;
    
    private:
      LineSegment operator = (const LineSegment&);
    };
    
    function
    {
     Node FirstPoint = Node(2,2);
     Node SecondPoint = Node(4,4);
    
     LineSegment MyLine = LineSegment(FirstPoint, SecondPoint);
    }
    The drawback is that you can't change the points of your Node at a later point, but it is safer. It depends on what you want to do, though.
    Typically, if you can, go with references, otherwise go with pointers. Just remember that pointers opens a can of worms by itself.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Thank you for correcting my response Elysia. I just threw that together without much thought. At least I was not to far off.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    2
    Thank you very much. I understand how using pointers can create the link between lines and nodes and allow to change the nodes afterwards.

    Elysias example with references however is confusing. Would changing Myline.Point1 also change FirstPoint because of the reference? Or am I completely on the wrong track? I'll just have to play with it and see what happens.

    Thanks again!

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    The reference acts similarily to the pointer, the difference is that you cannot change it (and cannot set it to NULL, since references always point to something).
    I would use yet a different approach:

    Code:
    class LineSegment {
    private:
        Node* First_;
        Node* Second_;
        LineSegment(Node& First, Node& Second)
            :
            First_(&First),
            Second_(&Second)
        {
        }
    public:
        Node& First() const
        {
            return *First_;
        }
        Node& Second() const
        {
            return *First_;
        }
    };
    The difference is that this class is still copyable (you can have two line segments pointing to the same nodes). It uses raw pointers, but it is safe since they act as weak pointers (google shared_ptr if you want to know more).
    I never put signature, but I decided to make an exception.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking with two objects
    By emanresu in forum C Programming
    Replies: 2
    Last Post: 11-07-2006, 07:42 AM
  2. Problems with vectors of objects!
    By bobthebullet990 in forum C++ Programming
    Replies: 3
    Last Post: 08-11-2006, 02:16 PM
  3. Creating vectors of objects!
    By bobthebullet990 in forum C++ Programming
    Replies: 17
    Last Post: 08-08-2006, 05:51 PM
  4. vectors and objects
    By barneygumble742 in forum C++ Programming
    Replies: 3
    Last Post: 07-04-2005, 09:53 AM
  5. Vectors of pointers to objects
    By Myownworstenemy in forum C++ Programming
    Replies: 3
    Last Post: 09-02-2003, 12:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21