Jump to content
  • Advertisement
Sign in to follow this  
Toadhead

scope

This topic is 4841 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

I have a program. Lets say I have a class testclass:
class TestClass
{
public:
void TestFunction1();
void TestFunction2();
}

void TestClass::TestFunction1() {

int number = 0;

cout << "number: " << number << endl;

TestFunction2();

}

void TestClass::TestFunction2() {
number++;
cout << "New number: " << number << endl;
}

In this example I call TestFunction2 inside TestFunction1, and TestFunction2 uses the number variable from TestFunction1. This will result in an error right? But how can I fix this? Do I need to use TestFunction1::number++ or something?

Share this post


Link to post
Share on other sites
Advertisement
In your example, TestFunction2 has no access at all to number.
It will in fact generate a compiler error.

If you want to increment number in TestFunction2, you will need something like this:


class TestClass
{
public:
void TestFunction1();
void TestFunction2(int&);
}

void TestClass::TestFunction1() {

int number = 0;

cout << "number: " << number << endl;

TestFunction2(number);

}

void TestClass::TestFunction2(int& numParameter) {
numParameter++;
cout << "New number: " << numParameter << endl;
}


Share this post


Link to post
Share on other sites
So how can I fix it?
(I dont mean just copy the stuff from Function2 in 1 and delete the whole function)

In my tetris clone I have a loop method that will be runned from another method. The loop method will use images etc. from the normal one and starts a small gameloop were it checks for events etc.

edited:
wow how many times have you edited the above post :)

In my tetris clone I cannot just send ALL images and stuff as reference, that would coat too many time since it also includes recangeles (sdl structs) etc. which would cost too many time.

Share this post


Link to post
Share on other sites
See my modified post.

Possibly a better way would be (depending on the circumstance):

class TestClass
{
private:
int number;

public:
TestClass();
void TestFunction1();
void TestFunction2();
}


TestClass::TestClass(){
number = 0;
}


void TestClass::TestFunction1() {

number = 0;

cout << "number: " << number << endl;

TestFunction2();

}

void TestClass::TestFunction2() {
number++;
cout << "New number: " << number << endl;
}

Share this post


Link to post
Share on other sites
Yeah I tought about that too.
I'll just make all images etc. (surfaces were the image will be loaded in) class variables so I can use them in both methods.

Thanks for your help.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!