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 7 years ago.
Improve this question
Is there any way of writing If as a function in c++?
I am not sure if I write my question correct.
int sum(int a, int b)
int total;
total = a + b;
return total;
So this is sum function, how can I write If in that way?
Something like that?
void if_else(bool cond, std::function<void()> if_, std::function<void()> else_) {
cond ? if_() : else_();
}
Even to there is still an if/else statement in the form of the ternary operator...
This one may be another solution:
void if_else(bool cond, std::function<void()> if_, std::function<void()> else_) {
(cond && ([&]() { if_(); return true; })()) || else_();
}
I'm not sure about the second one, mainly because of the definition of a function immediately executed, but I guess the idea is pretty clear.
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 2 years ago.
Improve this question
If I have the following:
bool type;
int main();
{
type = false;
}
void update()
{
if( type == false )
{
//do something
}
else
{
//do something else
}
}
The code will set the value of type to either true or false, using an if statement will cause the compiler to check what the value is every update, I want to avoid this and only do one check since the type will be set and then not changed. Is there some way to make it only check once?
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 2 years ago.
Improve this question
I have about 40 questions. (all the answers to the questions are of either int or float data type)
I will display the question and ask the user to input his/her answer.
Then I will check if the answer is correct or not.
I need to implement this program in object oriented programming.
I thought about doing it this way,questions by questions..but is there another way of doing this? Please help me..
The code below is sort of a pseudocode . I use c++ .
class workout{
private:
float variables...;
public:
workout();
workout(float variables...);
void answers();
int Display();
~workout();
};
...
void workout::answers(){
declare variables;
display question1;
input answer1
check if correct
display question2;
input answer2
check if correct
display question3;
..and so on..
}
workout::~workout(){
}
int main(){
...
return 0;
}
Well you could have a Question class, and then go from there. Here's an example.
class Question
{
public:
std::string question
float answer;
Question(std::string _question, float _answer)
: question(_question), answer(_answer) {}
void Display() {}
bool Check(float input) {}
};
And then maybe create an array of questions?
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'm quite new to C++, and since I want my code to be good, I have a small question, let's say I have a function
UI::GetColor(CClass Class)
{
if (Class.m_Something)
return 0;
return 1;
}
Is it better to pass the CClass as a pointer, so it's not being copied or not? I saw a lot of code using different styles of that and I'm kind of confused which one is better and why. Thanks for answers.
The function should be written as follows:
class UI {
...
int GetColor(const CClass &c) const {
if (c.m_Something) {
return 0;
}
return 1;
}
}
The reference avoids an unnecessary copy; the const-parameter states that the parameter will not be changed; the const-function declarator states that the function will not change the this-object (i.e. the UI-instance on which it is called).
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 6 years ago.
Improve this question
I can't see to find the problem in my code. Nor the solution on the internet. I can see that i can create the code in a different way but i need to know how to work with it written like this:
class Triunghi{
int l1=0;//latura1
int l2=0;//latura2
int ba=0;//baza
int p=0;//perimetru
public:
Triunghi(){}
Triunghi(int a):l1(a){}
Triunghi(int a,int b):l1(a),l2(b){}
Triunghi(int a,int b,int c):l1(a),l2(b),ba(c){}
Triunghi(int a,int b,int c,char *msg):l1(a),l2(b),ba(c){
cout<<msg<<""<<l1<<l2<<ba<<endl;
}
Triunghi(Triunghi &A){
l1=A.l1;
l2=A.l2;
ba=A.ba;
}
~Triunghi(){
}
int Perimetru()
{
p=l1+l2+ba;
return (p);
}
};
It works fine there are no errors but i cant seem to give value to l1, l2 and ba, to use them in function 'Perimetru'.
This is how my main looks.
int main()
{
Triunghi tri;
Triunghi(1,2,3);
tri.Perimetru();
return 0;
}
How to make it work?
Triunghi tri;
Triunghi(1,2,3);
This doesn't do what you think it does. tri is created with default constructor, so all of your members are set with their in-class default values, meaning 0. Then, you create temporary object Triunghi, and you don't even use it. What you might wanted to do instead is:
Triunghi tri(1,2,3);
Then Perimetru() returns 6 as expected.
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
I have an private property in my class and I want to add some code on its setter, how can I accomplish that?
I might do that using method but I don't want to just to avoid using property.
private:
float damage;
You can replace your plain old data type with a class type to do what you want:
class Damage
{
public:
Damage& operator=(float x) { value = x; /* add code here*/ }
operator float() const { return value; }
private:
float value;
};
Then just replace your float with Damage and you can make it behave how you want.