+ 1

How to make it work for both int and double

Hello Please refer below structure of code. spinBox is used as class and its object has been created everywhere in entire code base in actual. How to make it work for double also? I mean I could think of having a constructor spinBox(double) and it should not be a problem. Main worry is for get and set functionality. I can define set for double as both set will be overloading function case. While get will be a problem as int get() and double get() is ambiguous. How to handle this scenario? Any thoughts? Unfortunately, I cannot share entire code base, but would like to definitely answer the query and try to mimic the answer in sample code. In short, How to handle spinbox class for both int and double? https://sololearn.com/compiler-playground/c21dSD9kVFjv/?ref=app

1st Mar 2024, 7:23 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
8 Réponses
+ 3
why not just make your class generic? It is simpler than overloading. what is the advantage of casting to and from double to int? It feels very messy.
2nd Mar 2024, 12:10 AM
Bob_Li
Bob_Li - avatar
+ 2
Making class generic with template changes class spinBox to a template class. This change makes me change everywhere for spinbox to spinbox<int>. This headache is too much. Is this template one the best approach ? If so, I can still choose it to have it with the burden of changing spinbox to spinbox<int>
2nd Mar 2024, 3:38 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
I am not familiar with SWIG. From what I read in the website, it seems like it generates a glue code that allows communication between C/C++ and scripting languages like Python and Ruby. So you are basically writing a C++ Python module. Googling c++ version SWIG gives me C99,C++11,C++14,C++17, and partial C++20.... So maybe try it on simple tests first? There are other ways to use C++ with Python if this doesn't work.
2nd Mar 2024, 4:35 AM
Bob_Li
Bob_Li - avatar
+ 1
i don't know, but I feel that having separation between int and double is better than making internal conversions and convoluted inheritance just to keep things in the same class.
2nd Mar 2024, 3:53 AM
Bob_Li
Bob_Li - avatar
+ 1
have you explored std::variant? maybe you just need a variant<int,double> value?
2nd Mar 2024, 4:00 AM
Bob_Li
Bob_Li - avatar
+ 1
Sounds good
2nd Mar 2024, 6:17 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Does that support for SWIG ? I have basically these code getting used by python
2nd Mar 2024, 4:19 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Is learning programming language difficult?
3rd Mar 2024, 5:24 PM
Ali Gouh
Ali Gouh - avatar