Skip to main content
gamedev.net
🔒 Locked

class variables

Started by TiitHns 4 replies 750+ views
Original Post
TiitHns
TiitHns
Simple question about setting class variables;

class MyClass{
public:
void MyFunc(int *i);
int *i;
};
void MyClass::MyFunc(int *i){
 
}
MyClass->MyFunc(&Smth);
Is there a way so when calling function int *i from function sets automatically MyFunc::i same value; Without passing it manually (MyFunc::i = i;)
http://www.daily-it.com
Zakwayda
Zakwayda
Quote:
Original post by TiitHns
Simple question about setting class variables;
class MyClass{public:void MyFunc(int *i);int *i;};void MyClass::MyFunc(int *i){ }MyClass->MyFunc(&Smth);


Is there a way so when calling function int *i from function sets automatically
MyFunc::i same value; Without passing it manually (MyFunc::i = i;)
Your question isn't clear to me (although it may be to others).

What do you mean by MyFunc::i? Can you maybe restate your question? Maybe putting it in different words will help clarify things...
smitty1276
smitty1276
Quote:
Original post by TiitHns
Is there a way so when calling function int *i from function sets automatically
MyFunc::i same value; Without passing it manually (MyFunc::i = i;)


Are you saying that you don't want to have to type this line:
void MyClass::MyFunc(int *i){  this->i = i;  // <-- You're trying not to type this??}


If I'm understanding correctly.... why?

And the answer is "no", by the way, unless you use a default argument or something... it depends on what you're asking I guess.
TiitHns
TiitHns
yes i dont want to use this pointer. These has to be some way to do it.
http://www.daily-it.com
Antheus
Antheus
Quote:
Original post by TiitHnsThese has to be some way to do it.


No, there isn't.
Zakwayda
Zakwayda
Quote:
Original post by TiitHns
yes i dont want to use this pointer. These has to be some way to do it.
It looks like you're trying to write a 'setter' function, and/or a function to initialize the object.

If it's a 'setter' function, you might reconsider whether such a function is necessary or even advisable (of course in your example the data is public anyway, but I assume that's just because it's an example). For the why's and wherefore's, read up on object-oriented design (or search the archives for 'getters and setters').

If it's an initialization function, move the initialization of the variable to the constructor. If you use an initializer list (as you should), you can then use the same variable name for both the member variable and the argument (which seems to be what you're wanting to do).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.