473,804 Members | 3,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
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 1955
boxfish
469 Recognized Expert Contributor
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 Recognized Expert Specialist
Alternatively, check out this header file and the methods contained within it to create a variadic function.
Sep 22 '08 #3
Banfa
9,065 Recognized Expert Moderator Expert
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 Recognized Expert Moderator Expert
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
Poke386
2 New Member
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 Recognized Expert Moderator Expert
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
2852
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 reference but set no default value, then the parser has no problems. I've resovled this for now by taking out the "false" default values. But how shall I handle those situations (there are many) where I wish to pass an object by reference but I'm not...
38
4637
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
11075
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) modHandler.rptOpen (Me.Report.Name), (Me.Tag) End Sub
5
1492
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 in advance, Vinicius.
10
2518
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++) { items = "item" + (i + 1);
23
6553
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: vector<string> tokenize(string s){
9
2582
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 provided with the Oracle Express Edition and using Access 2k2 as the front-end. When I debug print the SQL and paste it into SQLplus it works fine. The length of the debug print that works is 1988 characters. Since the exact same code works with...
9
5019
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 something like this, but it did not work, gave core dump. class myclass { public: myclass(){}; ~myclass(){};
12
11118
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. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
0
10562
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10319
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10303
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5508
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4282
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2978
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.