Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

DrewSmug

accessing an ARRAY with CLASSES

This topic is 5208 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

so here''s my starter code
class Screen
{
public:
	void setBuffer(char buffer);
	char getBuffer();
private:
		char itsBuffer;
};

void Screen::setBuffer(char buffer)
{
	itsBuffer = buffer;
}

char Screen::getBuffer()
{
	return itsBuffer;
}
what i''ve tried to do is make itsBuffer an array of lets say 3x3... so itsBuffer[3][3]. my problem is i don''t know how i would write a setBuffer or getBuffer method that would allow me to select just one part of the array to write to or read. for instance if i wanted to make itsBuffer[1][2] = ''#''; what is the syntax that would allow me to do that. thanks for any help in advance

Share this post


Link to post
Share on other sites
Advertisement
Well.. you're not declaring itsBuffer as an array in your class. It's only a char. You'll need to declare it as char itsBuffer[MAX][MAX], if you want it as a 2-dimensional array of chars.

To set a specefic value with setBuffer(), you'll need to add 2 more parameters, which will be the array entry that you want to write to. So, if you want to change the value of itsBuffer[3][4] to "f", you can call setBuffer("f", 3, 4) or something like that. Now the value of a specific entry in itsBuffer will be changed.

Same with getBuffer(). If you want to read a certain entry of the array, add 2 parameters as well.


class Screen
{
public:
// code

private:
char itsBuffer[MAX][MAX];
};

void Screen::setBuffer(char buffer, int row, int col)
{
itsBuffer[row][col] = buffer;
}
char Screen::getBuffer(int row, int col)
{
return itsBuffer[row][col];
}


I think that's what you want

[edited by - shyam on April 13, 2004 9:07:07 PM]

[edited by - shyam on April 13, 2004 9:07:46 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!