Sign in to follow this  
BASICisforsquares

Strings in classes

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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()".

Share this post


Link to post
Share on other sites
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;

};

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this