This forum is in archive mode. You will not be able to post new content.

Author Topic: Why does the following method work in PHP but not C++?  (Read 1359 times)

0 Members and 3 Guests are viewing this topic.

Offline PsychoRebellious

  • Peasant
  • *
  • Posts: 130
  • Cookies: -6
    • View Profile
    • My Rantings
Why does the following method work in PHP but not C++?
« on: February 21, 2015, 06:43:28 PM »
Following is a dummy php code

Code: (php) [Select]
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++
Code: (cpp) [Select]
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.
« Last Edit: February 21, 2015, 07:21:18 PM by Kulverstukas »

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Why does the following method work in PHP but not C++?
« Reply #1 on: February 21, 2015, 07:23:31 PM »
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.
« Last Edit: February 21, 2015, 07:25:04 PM by Kulverstukas »

Offline HTH

  • Official EZ Slut
  • Administrator
  • Knight
  • *
  • Posts: 395
  • Cookies: 158
  • EZ Titan
    • View Profile
Re: Why does the following method work in PHP but not C++?
« Reply #2 on: February 21, 2015, 07:50:02 PM »
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.

« Last Edit: February 21, 2015, 07:50:57 PM by HTH »
<ande> HTH is love, HTH is life
<TurboBorland> hth is the only person on this server I can say would successfully spitefuck peoples women

Offline PsychoRebellious

  • Peasant
  • *
  • Posts: 130
  • Cookies: -6
    • View Profile
    • My Rantings
Re: Why does the following method work in PHP but not C++?
« Reply #3 on: February 21, 2015, 07:51:51 PM »
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
Code: [Select]
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?
« Last Edit: February 21, 2015, 09:09:05 PM by Kulverstukas »

Offline HTH

  • Official EZ Slut
  • Administrator
  • Knight
  • *
  • Posts: 395
  • Cookies: 158
  • EZ Titan
    • View Profile
Re: Why does the following method work in PHP but not C++?
« Reply #4 on: February 21, 2015, 08:06:13 PM »
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.
<ande> HTH is love, HTH is life
<TurboBorland> hth is the only person on this server I can say would successfully spitefuck peoples women

Offline PsychoRebellious

  • Peasant
  • *
  • Posts: 130
  • Cookies: -6
    • View Profile
    • My Rantings
Re: Why does the following method work in PHP but not C++?
« Reply #5 on: February 21, 2015, 09:46:41 PM »



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.

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.