Strings in classes

Started by
8 comments, last by Zahlman 18 years, 1 month ago
I was working on a simple text adventure game in c++. I had a class for rooms like this: class place { public: char[150] descrip; (more variables and a few functions here) void showDescrip(room); } How could I get showDescrip to show the caption arrays for different rooms?
------------------------------I just learned to speak CounterStrike! "W00t I own this map!I m lik a l33t h4x0r or sumthing n0 0ne can 0wn up to my r0x0r skillz!"
Advertisement
Quote:Original post by BASICisforsquares
I was working on a simple text adventure game in c++. I had a class for rooms like this:
class place
{
public:
char[150] descrip;
(more variables and a few functions here)
void showDescrip(room);
}

How could I get showDescrip to show the caption arrays for different rooms?
First, the usual observation: since you're programming in C++, you might consider using std::string instead of char[]. It can make your life a lot easier.

As for your question, what do you mean by 'show'? Is this a console program, or do you have some other environment for displaying text? In any case, a little more info would be useful.
If your using c++ I would use std::string instead. If you need to convert it to a c style string for anything you can just do "mystring.c_str()".
I'm not quite sure what you're asking, but is this what you're trying to do?

class place{public:void showDescrip() { std::cout << descrip; }private:std::string descrip;};

I mean just display it on the screen in a console program. I thought about:

place::showDescrip(roomInstanceName)
{
cin << roomInstanceName.descrip << endl;
}

But that would make it look for a class called roomInstanceName. Sorry, I conldn't come up with the words at the time, I wanted to make the function so it could adapt to being used on different instances of the class without having to write a function per room. That would just be ridiculous.

Ps: Will, I can do that without all the std::'s by using using namespace std, right? ANd no, the string was public so i didn't have to make ANOTHER function.
------------------------------I just learned to speak CounterStrike! "W00t I own this map!I m lik a l33t h4x0r or sumthing n0 0ne can 0wn up to my r0x0r skillz!"
Quote:Original post by BASICisforsquares
Ps: Will, I can do that without all the std::'s by using using namespace std, right?


Yes, or you can do "using whatever", for example, "using std::cout".

Edit: And I think you mean "cout", above...
Thanks, I just didn't know if the conventions were different outside of the main() function.
------------------------------I just learned to speak CounterStrike! "W00t I own this map!I m lik a l33t h4x0r or sumthing n0 0ne can 0wn up to my r0x0r skillz!"
But i still need to know the answer to my question! ::sniff:: will no-one come to my rescue???
------------------------------I just learned to speak CounterStrike! "W00t I own this map!I m lik a l33t h4x0r or sumthing n0 0ne can 0wn up to my r0x0r skillz!"
Quote:Original post by BASICisforsquares
I mean just display it on the screen in a console program. I thought about:
place::showDescrip(roomInstanceName){     cin << roomInstanceName.descrip << endl;}


But that would make it look for a class called roomInstanceName. Sorry, I conldn't come up with the words at the time, I wanted to make the function so it could adapt to being used on different instances of the class without having to write a function per room. That would just be ridiculous.
[...]

I think thats already the right approach - but you should rather try something like this here:
class CRoom {public:   std::string descrip;   //  constructor takes one argument, which is the name of the room   //   the argument is being passed to the constructor of descrip   CRoom( std::string strRoomName ) : descrip(strRoomName) { };   ~CRoom( void ) { };   };class CPlace {public:   std::string descrip;   CPlace( std::string strPlaceName ) : descrip( strPlaceName ) { };   ~CPlace( void ) { };   // ... your other methods/properties ...   void showDescrip( CRoom* );}void CPlace::showDescrip(CRoom* roomInstanceName) {     std::cout << roomInstanceName->descrip << std::endl;}int main( void ) {   CPlace MyPlace("At Home");   CRoom FirstRoom("Library");   CRoom SecondRoom("Kitchen");   MyPlace.showDescrip(&FirstRoom);   MyPlace.showDescrip(&SecondRoom);};

... one can argue about whether this approach is effective in any way or not - but I guess it should do.

Have Fun & bye bye,
MHOOO
To try to answer the original question, if I understand it: within a *member* function, you have direct access to the data members of the "current object". Thus you don't have these functions accept an instance of the class, unless they implement some interaction between two instances (the current one and the provided one). And to refer to the names, just use them directly. They are in scope, sitting in between the function locals and namespace globals.

void object::doSomething() {  int foo = 42;  use(bar); // if 'bar' is a member, then that works  use(foo); // the local name takes precedence  use(this->foo); // that's how you get at a member with the same name, if necessary  use(::bar); // that's bar at the global scope, not the data member}// When we *use* a member function, we have to know which object instance to// call it "on" (with):// object::doSomething(); <-- no worky! You can only do this with "static"// functions, which aren't associated with objects (so you don't get to refer// to non-static members when you write them).object o;o.doSomething(); // ok: use()s o.bar, the local foo, o.foo and the global bar in order.

This topic is closed to new replies.

Advertisement