473,395 Members | 1,762 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. class room
  2. {
  3.     public:
  4.     string description;
  5.     room(string s,??) //problem here
  6.     {
  7.         s=description;
  8.     }
  9.     vector<room*> next;
  10. }
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:
Expand|Select|Wrap|Line Numbers
  1. room something(d,s1,s2); //the vector stores the addresses of rooms s1,s2
  2.  
This doesnt seem too difficult but since any room can link up with any number of other rooms, something like this can happen:
Expand|Select|Wrap|Line Numbers
  1. room something(d,s1,s2) //something is "linked" to 2 other rooms
  2. room something2(d,t1) //something2 is "linked" to 1 other room
  3. room something3(d,q1,q2,q3) //something3 is "linked" to 3 other rooms
  4.  
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
Sep 22 '08 #1
6 1940
boxfish
469 Expert 256MB
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.
Sep 22 '08 #2
Ganon11
3,652 Expert 2GB
Alternatively, check out this header file and the methods contained within it to create a variadic function.
Sep 22 '08 #3
Banfa
9,065 Expert Mod 8TB
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.
Sep 22 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
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.
Sep 22 '08 #5
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
Expand|Select|Wrap|Line Numbers
  1. vector<wall> v
  2. if(v.door)
  3. //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.
Sep 22 '08 #6
weaknessforcats
9,208 Expert Mod 8TB
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.
Sep 22 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: lawrence | last post by:
The following class method is being rejected by the PHP parser. If I change the method paramaters and allow the objects to be passed as copies, then the parser has no problem. Or, if I pass by...
38
by: Radde | last post by:
HI all, Whats the difference b/w pass by ref and pass by pointer in C++ when ur passing objects as args.. Cheers..
5
by: deko | last post by:
I'd like to use a bit of code in the OnOpen event of a report: =rptOpen(Me.ReportName), (Me.Tag) --this doesn't work This does work: Private Sub Report_Open(Cancel As Integer)...
5
by: CViniciusM | last post by:
Hello, The output of the code below is: number = 10 number = 0 (or other number except 15) Why the output of the second 'number' is not 15? Why the 'number' address is not changed? Thanks...
10
by: Sean Dockery | last post by:
I have the following HTML file that I've been using for testing... <html> <head> <script type="text/javascript"> <!-- function handleWindowLoad() { var items = ; for (var i = 0; i < 11; i++)...
23
by: Sanjay Kumar | last post by:
Folks, I am getting back into C++ after a long time and I have this simple question: How do pyou ass a STL container like say a vector or a map (to and from a function) ? function: ...
9
by: Greg Strong | last post by:
Hello All, What is the maximum length of an ODBC pass through query? Things work fine with the code except when I try to create a view which is pretty complex in Oracle. I'm using a DSN...
9
by: grbgooglefan | last post by:
I am trying to pass a C++ object to Python function. This Python function then calls another C++ function which then uses this C++ object to call methods of that object's class. I tried...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.