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).
Related
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 create a unique pointer in one class, class A, then pass on the ownership to another class, class B. Am I ok doing such a thing?
The code below gives me error in getC:
error: call to deleted constructor of 'std::unique_ptr<C>
What am I doing wrong?
class A {
...
void func(shared_ptr<B> Bptr) {
A_pass = make_unique<C>();
Bptr->setPass(move(A_pass));
}
unique_ptr<C> getC()
{
return A_pass;
}
unique_ptr<C> A_pass;
};
class B {
...
void setPass(unique_ptr<C> pass_ptr){
B_pass = move(pass_ptr);
}
unique_ptr<C> B_pass;
}
edit: update the question
Your question does not state where the compiler error occurs, but I would guess it’s the getC() member function:
class A {
...
unique_ptr<C> getC()
{
return A_pass;
}
unique_ptr<C> A_pass;
};
The function as written is attempting to copy A_pass, which of course is not possible for the std::unique_ptr<T> type.
You can rewrite it to explicitly move from the source (I’m not able to test this):
unique_ptr<C> A::getC() {
return A_pass.release();
// alternative: return unique_ptr<C>(std::move(A_pass));
}
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 4 years ago.
Improve this question
I had a situation where I wanted to use a default paramenter for a reference in a VERY large legacy codebase for a fix.
static bool _defaultValue = false;
bool SomeFunction(const SomeComplexObject& iObj, bool& isSomeVal = _defaultValue )
{
// ... code
}
My issue is with using a static variable inside a namespace just dangling there by itself.
This code is going to be reviewed before being shipped but I'm unsure if it would be considered bad practice to have a dangling static variable like that.
Without the variable you can't have a default value for a reference. My options are very limited to make other changes to get the desired effect.
Would this be considered "hacky unprofessional coding"?
My suggestion would be:
Remove the global variable.
Don't use a default value for the reference argument.
Create a function overload that has only one argument.
Call the first function from the second function.
bool SomeFunction(const SomeComplexObject& iObj, bool& isSomeVal)
{
// ... code
}
bool SomeFunction(const SomeComplexObject& iObj)
{
bool dummy;
return SomeFunction(iObj, dummy);
}
Client code can call whichever function is appropriate in their context.
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 5 years ago.
Improve this question
I am reading a sample code that uses C++ and classes, I am new on C++ classes I can work with basics similar to this http://www.cplusplus.com/doc/tutorial/classes/, but I cant understand what the code below does mean or the color it is using visual studio c++
thanks
I am sorry if it is a fool question
It creates an object named some by instantiating the class some.
Then it calls the member function ToVector() on the object some and pass the result of the call to the function named function.
class is blue because it is a keyword of the C++ language.
The first some is green because it is the name of a class.
The second some is black because it is a variable.
And function and ToVector are red because the are functions.
Now this is ugly code because you "hide" the class some by reusing the same name for your variable. Also you do not need to put the word class here.
Here is a more complete and nicer version:
#include <vector>
class Some
{
public:
std::vector<int> ToVector()
{
return std::vector<int>(); //return an empty vector
}
};
int f(std::vector<int> v)
{
return 0;
}
int main(int, char**)
{
Some some; // Was "class some some"
return f(some.ToVector());
}
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
It is possible somehow to a call a non-member function from a member function?
Something like this:
class A {
int a;
public:
bool (int b) {
smt;
lala = define_smt(smt);
if (lala < 5) {
return true;
}
else {return false}
}
}
int define_smt(smt){ ...}
Thnaks in advance
The function must be known at call time. That means you must declare it before you use it. You have basically two choices here: Either put the whole function before the class, or use a function prototype before the class. So either:
int define_smt(smt) { ... }
class A {
// ...
}
or:
int define_smt(smt);
class A {
// ...
}
int define_smt(smt) { ... }
Of course It is Possible. As long as your function is available in the current context. i.e. either a prototype or a defination should be available, before the point of calling.