about c++ classes

is it possible that the parameters of the one function is globally used without using any global variable?

1
2
3
4
5
6
7
8
9
10
11
void myclass::myclass() { /** code here */ }

void myclass::thefunction(int x, char y)
{
  /** code here using the x & y */
}

void myclass::thefunction2()
{
  /** code here also using the x & y */
}


how can i initialize the x & y globally?
Last edited on
I am not sure that I really understand what you are saying. But if you wish to share variables between member functions then I suppose that is what member variables are for:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class myclass
{
private:
	int x;
	int y;
	
public:

	myclass(): x(0), y(0) {}

	void thefunction()
	{
		// code using x, y
	}

	void thefunction2()
	{
		// code using x, y
	}
}
thanks for the reply but i was thinking that the x & y values comes from the parameters.

file.cpp
1
2
3
4
5
#include myclass.h

void main() {
  myclass::thefunction(2, 't');
}


myclass.cpp
1
2
3
4
5
6
7
8
9
10
11
void myclass::myclass() { /** code here */ }

void myclass::thefunction(int x, char y)
{
  /** code here using the x & y */
}

void myclass::thefunction2()
{
  printf("%d - %", x , y); /** which displays number 2 and letter t */
}
Well you will either have to pass the variables as many times as necessary for the function you want to have access on them, or you can make them global, or you can make them data members of the class which this function belongs to.
Not possible. Arguments are local to the function and can never be available to the other function.
closed account (z05DSL3A)
Static variables are class member that are shared across all instances of the class, about as 'global' as you can get within a class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class myclass
{
public:
    myclass(){}
    
    static void Func1(int a)
    {
        y = a;
    }
    void Func2()
    {
        cout << y << endl;
    }
private:
    static int y;
};

int myclass::y = 0;


int main() 
{
    myclass test1;
    myclass test2;


    test1.Func1(3);
    test2.Func2();
    
    return 0;
}
Last edited on
You could the assign the value of the parameters to member data. That would make them accessible to all functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class myclass
{
public:
         myclass (){} 
         ~myclass(){}
         funct1(int a,b){}
         funct2(){}
private:
       int x;
       int y;
}

myclass::funct1(int a,b)
{
a=x;
b=y;
}

myclass:funct2()
{
//code that deals with x and y (which are equal to a,b parameters)
}
Last edited on
The x and y are defined only within the function scope. Once the function is over, they don't exist anymore.

I'm not clear on what you want to do, but if you want to pass x and y once to thefunction so that you won't have to pass it to thefunction2 again, your only way is to define them as members of myclass.

you can add a part to thefunction which saves the x and y value in the relevant members. Or you can initialize them in the construction of myclass, and you won't have to pass the values to thefunction even.
closed account (z05DSL3A)
TheCreator wrote:
1
2
3
4
5
myclass::funct1(int a,b)
{
a=x;
b=y;
}
should that not be x=a and y=b?
@Grey Wolf.
Yeah, sorry about that- stupid mistake.
thanks to all of your replies.

im doing it on all your samples before this thread. i just want to know if it was possible to directly use the parameters to the other functions.
Topic archived. No new replies allowed.