Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to pass values from qml to set function written in Qt cpp, the values that will be passed is of different data types, example int or string, I will be writing only one set function in cpp which will take this values and return Qstring or int or double.How can I write a code for this.
C++ does know of two types:
QVariant
QJSValue
which might be the value, that you passed... That depends on you.
Both of them have various methods to test for their content, and to convert to this.
See the linked documentations for this.
You may even store the passed value as the corresponding type, without the need of conversion (up to the time, you need it for calculations in C++) It depends on, what you will do with it, to find the right choice.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I'm learning SDL right now and I'm trying to use timer callback function.
The function SDL_AddTimer() offer me only one argument to pass through but I want to pass different types of variables into it.
(https://wiki.libsdl.org/SDL_AddTimer)
I think of one solution is that I can declare a structure containing all of my variables but I'm not sure if it is the best way to do so.
Thank you for your help ~~
I think of one solution is that I can declare a structure containing all of my variables but I'm not sure if it is the best way to do so.
Yes this is the correct and best way to pass parameters whenever you are presented with a callback that accepts void*. Then cast back to the struct type from inside the callback.
Same thing goes when using pthreads/Windows threads or other such common APIs.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to make a function that has one parameter (a string that contains the name of a variable in my program) and returns the value that the variable stores.
Unfortunately, C++ doesn't provide a simple mechanism for reflection (the idea that the program can, at runtime, look up variable names by value or the like). You may need to consider using an alternate approach.
For example, you could make a std::unordered_map whose keys represent certain values that you'd like to look up and whose values are populated with the items you'd like to look up.
Hope this helps!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'd like to know if there is a way to transform a string into a function em c++. For example if I have the string: "x+y" it'd create the function and by replacing x and y, get the value of it.
In Java there is this API https://www.objecthunter.net/exp4j/index.html, so I was wondering if there is something similar.
There are many possible methods one could use to transform a string into a "function". Many of those involve parsing the string and building a function-like object out of it.
A lightweight and portable solution would be to use ExprTk, a mathematical expression library developed by Arash Partow.
The main page contains various usage examples.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
So pretend I'm developing a car class and I want one of the car class's functions to return a list of passengers, except I'd like to put a list reference as an argument and just set that list instead of returning a list.
void GetPassengerList(PassengerList &passengerList); //sets the list
I don't know if I should call it GetPassengerList or SetPassengerList, or something else. I feel like using the words get / set make it seem like there is a private variable that is being manipulated like the typical getter / setter methods. What's a good naming convention to use here?
In our team for input/output arguments we either use
void AdjustPassengerList(PassengerList&);
or
void AddPassengersTo(PassengerList&);
Depending on the use-case. For example the first one could be used if you want a list created from more than one car. The second usually reads well in code, something like:
car.AddPassengersTo(list);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it possible for me to take an existing array and make certain values (not all) constant? I'm trying to build a sudoku solver and my idea is to have the user enter values, so I would like to have those values remain constant as I change the empty spaces. Any tips or advice with the solver would also be appreciated. This is my first quarter working with c++. Thanks!
No. Const is essentially a compile-time idea. It's not something that can be toggled as a program is running.
If you need certain values to remain untouched while others are changed, then you need to put that into your data and logic. For instance, each value might have an associated boolean that indicates whether or not it can be changed. Then write your logic to respect that boolean.