How can I make class return value like function without brackets | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

How can I make class return value like function without brackets

There is one thing I'm interested in, how to make something like get/set from c# in c++, there is way to do that, we can see it on class string, anyone know how? Example: class A{ int x; public: A(int x){ this->x=x; } char* a(){ return "I'm A"; } } int main(){ A a = 5; // call constructor A(int x) cout<<a.a; // get char* from a() }

4th Oct 2020, 10:50 AM
nicolas turek
nicolas turek - avatar
4 Antworten
+ 1
C++ doesn't have getters and setters like JavaScript or C#. You just have to call the methods as functions
4th Oct 2020, 11:02 AM
Anthony Maina
Anthony Maina - avatar
0
And how does string work? This code works and it's almost what I'm asking about #include <string> int main(){ string str = "text"; }
4th Oct 2020, 12:31 PM
nicolas turek
nicolas turek - avatar
0
Ok, part of solution is using: void operator=(int x){...} however, this doesn't work on: A a = 1; only: A a; a = 1
4th Oct 2020, 12:55 PM
nicolas turek
nicolas turek - avatar
0
I've got it, string is defined like this: typedef char* string;
4th Oct 2020, 5:59 PM
nicolas turek
nicolas turek - avatar