EvilZone
Programming and Scripting => C - C++ => : PsychoRebellious February 21, 2015, 06:43:28 PM
-
Following is a dummy php code
class user{
private $fname;
private $lname;
public function get($var){
return $this->$var;}
};
I've been using the above approach in PHP. a call to get function does this
user $user1=new user('myname');//implying a constructor is defined
$user1.get('fname'); //returns fname variable
Now let's say try the same in C++
class user{
private:
string name;
public:
user(string x){
this->name=x;}
string get(string vaar){
return this->vaar;
}
};
the call to get function returns an error that the variable vaar is undefined where as I'm using vaar as merely a place holder. It is a better way to access variables than writing functions for each variable like
getvar1(), getvar2()
Yes, yes I know that we can just code a get function and pass it a string and then test the string and return the variable according to what was in the string but I want it to be simple like the PHP one. Why doesn't the PHP approach work here?
Staff note: next time use the code tags pl0x.
-
You get an error, because you use this-> to reference a local (function) variable, where this-> is used for class members, so the program cannot find the variable vaar (which is a local, not class, variable) in a class and throws an error.
-
What Kulver said really, I had to switch web browsers to respond.
Basically, you can't do that in C++. Any variable you need to access from outside sources needs to have a getter. Well... at least for the way you want it. you ~could~ make a single getter that you pass an int to denote which variable you want and it passes back a union with the response in it...
but my advice is to just make the single line function for each, hell I believe a few IDEs will automatically populate a class with setters and getters for you.
TL;DR: It doesnt work because C++ Isn't PHP.
-
You get an error, because you use this-> to reference a local (function) variable, where this-> is used for class members, so the program cannot find the variable vaar (which is a local, not class, variable) in a class and throws an error.
I know. The this is a pointer to the calling object so it should look for the vaar variable in the class and not locally. Had I called the statement
return vaar;
then it should give an error but I find nothing wrong with
this->vaar; // vaar contains a string so this->vaar should translate to this->name when //vaar==name
and name is a class member.
TL;DR: It doesnt work because C++ Isn't PHP.
^This kind of stuff gets me wet. It is obvious that c++ != PHP, but since many things work the same way in most languages the loops/class structures/functions and a lot more then why does this not? I mean technically the call this->vaar SHOULD translate to this->name which is legit. I am only wondering that when vaar is not 'vaar' but vaar shouldn't it be replaced with the value of the variable making it a legit call?
Staff note: y u double post?
-
Glad I got you wet.
Heres the (more) technical answer, C++ doesn't look inside variables and act the way you want it too.
this->vaar always means this->vaar, no matter the contents of vaar. It never changes and means this->name, not in C++ at least.
It will not work because C++ and PHP handle this differently, because they may share some syntax but they are different languages.
If you want more of an answer join ##C++ on Freenode and ask but I doubt they will be gentle.
-
Heres the (more) technical answer, C++ doesn't look inside variables and act the way you want it too.
this->vaar always means this->vaar, no matter the contents of vaar. It never changes and means this->name, not in C++ at least.
It will not work because C++ and PHP handle this differently, because they may share some syntax but they are different languages.
If you want more of an answer join ##C++ on Freenode and ask but I doubt they will be gentle.
Fair enough. I wonder if anybody else gets confused too by working with multiple languages and their own rules.
Those staff notes cracked me up, LOL. I 'll be careful with editing posts from here on. Plus IT WAS THE ONLY COOKIE I HAD.