6
Is there a class initialization function for C++?
In Lua, using the library classlib
there is the function __init
, and in Python as well.
EX:
require "classlib"
Human = class();
function Human:__init(name, age)
self.name = name
self.age = age
end
gabriel = Human("Gabriel", 16)
print(gabriel.name, gabriel.age)
In C++ I had to do so:
#include <iostream>
#include <string.h>
using namespace std;
class human {
public:
string name;
int age;
void SET(string, int);
};
void human::SET(string name, int age){
this->name = name;
this->age = age;
}
int main (){
human gabriel;
gabriel.SET("Gabriel Sales", 16);
cout << gabriel.name;
return 0;
}
Exactly that.
– Gabriel Sales
@Bigown Only in C++ o
this
is a pointer and has to be treated like this -> ?– axell-brendow
@axell13 no, if you have any specific question, you want comparison with some language, ask a question that I try to answer.
– Maniero