Template type from return | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Template type from return

Hi Refer code below: I am ok with set function. It has parameter T which help me decide whether function is called for int or not. How to do same for get which don't have input paramter. It has only return type. How to call this get method from main ? Calling It currently results into error. How to solve this? In short : how to avoid error even if last two lines of main functions are not commented.? https://sololearn.com/compiler-playground/cBNFA2qnYPyJ/?ref=app

3rd Mar 2024, 7:26 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
10 Answers
+ 4
Debug: #include <iostream> using namespace std; class Test { private: int intArg; double dblArg; char chrArg; public: template<class T> T get(){ cout << "Getter called for "; if(is_same<T,int>::value) return intArg; else if(is_same<T,double>::value) return dblArg; else return chrArg; } template<class T> void set(T t){ cout << "Setter called for "; if(is_same<T,int>::value) cout << "int\n", intArg = t; else if(is_same<T,double>::value) cout << "double\n", dblArg = t; else cout << "char\n", chrArg = t; } }; int main(){ Test obj; obj.set(1); obj.set(2.3); obj.set('a'); cout<<endl<<obj.get<int>() <<endl<<obj.get<double>() <<endl<<obj.get<char>(); return 0; }
3rd Mar 2024, 9:54 AM
Solo
Solo - avatar
+ 2
but your setter did not really set anything that the getter can access... you only inferred the type and displayed a message. Notice that the get() is not really returning any value. That is the problem. What should the return type be? The hard part is the type of the variable the setter and getter are passing around. C++ types are rigid and variant types are hard to work with.
3rd Mar 2024, 7:39 AM
Bob_Li
Bob_Li - avatar
+ 2
His solution is best for traditional getter and setter. I don't need to save any data or state to class
3rd Mar 2024, 10:43 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
Solo I saved a copy of your code for personal reference in the future . It is a great alternative to variant, any, etc. Why try to put multiple data types into one variable when it is possible to work with them separately. Your code is a simpler solution. Ketan Lalcheta, good to know you got the real code sorted out. But I agree with Solo, calling it a setter if it does not set anything is confusing. Setters and getters usually work together to access private variables. My feeling is that you actually just need to set a flag to indicate whether the value is an integer or a double. But yeah, all's well that ends well..
3rd Mar 2024, 1:45 PM
Bob_Li
Bob_Li - avatar
+ 1
Ketan Lalcheta I hope you understand that your second version of the code is just a demonstration of the output of a real value that is stored in the getter, while the setter does not carry any useful function at all, so it can simply be delete.
3rd Mar 2024, 12:37 PM
Solo
Solo - avatar
0
Thanks a lot Bob_Li I did a basic mistake of not returning anything from a function having non void return type. I don't want my set function to store anything. Basically this set will be delegating call to QT spin box based on int or double. I solved as below : https://sololearn.com/compiler-playground/cpVz9D6CioHN/?ref=app
3rd Mar 2024, 8:08 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Ketan Lalcheta Solo 's solution seems to be the more useful implementation. The setter and getter are actually setting and getting values. perhaps the best answer checkmark should be moved there?
3rd Mar 2024, 10:25 AM
Bob_Li
Bob_Li - avatar
0
Bob_Li thank you so much for evaluating my version of this code. I don't pretend to be the best answer, I just wanted to supplement your answer with code that has practical application in my opinion...😎 The code turned out to be too big to fit in the answer, so I squeezed it slightly, but still not enough to write a comment on it, and there's not much to add to your answer, in my opinion everything is already visible from the code.
3rd Mar 2024, 12:31 PM
Solo
Solo - avatar
0
Solo , let me reiterate. I never wanted to set or get any value stored in class object. This is sample code and not the exact production code. Current class is simple class wrapper which will pass details to QT implementation. My whole concern was to make this class wrapper work for int and double and I should be able to differentiate int or double in both getter and setter. I was observing crash in initial code as I was not returning anything from getter earlier
3rd Mar 2024, 1:31 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Ketan Lalcheta 🙌...😎🏳
3rd Mar 2024, 1:40 PM
Solo
Solo - avatar