How do I pass a number of objects to a function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Poke386
    New Member
    • Sep 2008
    • 2

    How do I pass a number of objects to a function?

    I'm in the process of making an text based-rpg in C++. Its just a little project so I can learn some object-oriented programming, nothing serious. My problem is that I've created a class like so:
    Code:
    class room
    {
        public:
        string description;
        room(string s,??) //problem here
        {
            s=description;
        }
        vector<room*> next;
    }
    This is the room class. Yes, its missing a lot, but thats not the point. The problem is that I've got to make this room "link-up" with other rooms. What I mean is that its somehow connected to other rooms, allowing me to traverse from room to room. So I got the idea of passing the objects to the constructor and storing their addresses in a vector of pointers. The problem is how exactly I can pass these rooms the the constructor.

    In case I'm not clear enough, here's what I want to do:
    Code:
    room something(d,s1,s2); //the vector stores the addresses of rooms s1,s2
    This doesnt seem too difficult but since any room can link up with any number of other rooms, something like this can happen:
    Code:
    room something(d,s1,s2) //something is "linked" to 2 other rooms
    room something2(d,t1) //something2 is "linked" to 1 other room
    room something3(d,q1,q2,q3) //something3 is "linked" to 3 other rooms
    how exactly can I make the above code work? Or how can I pass any number of objects to a function?

    Any help/suggestions are greatly appreciated.

    PS: Anyone know of some text based RPG-making C++ tutorials? All I find are either said to be defective in some way or way to complex for me.

    Regards,
    -Poke386
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    Originally posted by Poke386
    Or how can I pass any number of objects to a function?
    How about passing an array or a vector of objects? That will work as long as the objects all have the same type.

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Alternatively, check out this header file and the methods contained within it to create a variadic function.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Personally I would say that you have 2 objects in play, rooms and links between rooms especially if you want a variable number of links between your rooms (as opposed to a grid with all rooms linked north, south, east and west).

        If you have the rooms containing the links then for 2 linked rooms A and B A has to link to B and B has to link to A but if you have the links separately you just need 1 object showing a link between A and B.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          The essential problem is that your class model has not been developed.

          A Room could be a class. Since a Room has walls, its data member could be a vector<Wall> where Wall is yet another class. The Wall could have Window and Door objects. So the Wall class could have data members of vector<Door> and vector<Window>

          A House is a class that contains a number of Rooms. Therefore, the House data member could be a vector<Room>.

          The technique here is called composition (HASA).

          You build you House by first creating Wall objects and then adding Door an Window objects as necessarty. Once you have four walls, you can create a Room object and then you can create a House object.

          There's no linking involved. Everything is based on containment.

          Comment

          • Poke386
            New Member
            • Sep 2008
            • 2

            #6
            Thanks for your suggestions. I think I'll go with Ganon11's idea.
            and weaknessofcats, I dont think I understand you very well. How exactly would the player get out of the room?
            I think something along the lines of
            Code:
            vector<wall> v
            if(v.door)
            //go to next room
            Although I'm not exactly sure how I'd go about implementing it. I'll look into it once I finish this project though, so thanks for that.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              You would need an association index object for your House that shows how the Room objects are connected.

              Let's say you are in Room Z which has Wall A, Wall B, Wall C and Wall D.

              You leave through Wall C. So lookup Wall C in the association index:

              Wall A Room Y
              Wall A Room Z
              Wall B Room H
              Wall B Room Z
              Wall C Room T
              Wall C Room Z
              Wall D Room K
              Wall D Room Z

              There you find two entries for Wall C. One for Room T and one for Room Z. Since you are in Room Z then you must be entering Room T.

              If you need to know the Walls of a Room, just look inside the vector of the Room object.

              Comment

              Working...