Would this be considered bad programming practice? [closed] - c++

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.

Related

C++ When comparing, pass class as pointer or normally? [closed]

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).

confused about class structure in code [closed]

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());
}

C++ Calling a function with a vector as a parameter in main [closed]

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 have a myVector class:
class myVector {
public:
void populateVector();
void showMenu(vector <myVector> const &vec_first);
private:
vector <myVector> &vec_first;
}
I haven't filled my vector yet but I want to essentially use the vector in the showMenu() function, however, a problem arises in my main when I attempt to call the showMenu() function.
int main() {
myVector obj;
obj.showMenu(vector <myVector> const &vec_first);
}
Codeblocks keeps saying:
main.cpp|33|error: expected primary-expression before 'const'
Your are confusing the function declaration with calling it. You need
int main() {
myVector obj;
vector<myVector> vec;
obj.showMenu(vec);
}
or something like that
I haven't filled my vector yet but I want to essentially use the vector in the showMenu() function, however, a problem arises in my main when I attempt to call the showMenu() function.
Don't pass the vector (or anything) in through showMenu; it already has access to the vector, which is a member of the same class.
If you did want to pass a function argument, repeating the argument's original declaration would not be the way to do it. Only its name should be specified. Here that would be:
obj.showMenu(obj.vec_first);
… if vec_first weren't private.
It looks like you need to go back to basics and read the initial chapters of your C++ book.

C++ Reference Variable [closed]

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
How to use 'reference variables' in C++ classes?
I have the following code that I want to put into a class: (note KinectCV&)
KinectCV& kinect = freenect.createDevice(0);
kinect.some_init_functions();
while(condition) {
// getting frames from kinect and processing
kinect.some_processing_functions();
}
kinect.some_stopping_functions();
I'm trying to make a class and separate init, process and stop functions:
class MyKinect {
public:
KinectCV kinect;
void init(){
/* I cannot use the '& kinect = freenect.createDevice(0);' syntax, help me in this */
}
void process(){
kinect.some_processing_functions();
}
void stop(){
kinect.some_stopping_functions();
}
}
I cannot use the '& kinect = freenect.createDevice(0)
That is right, you cannot assign references; once initialized, they refer to the same object forever. What looks like an assignment in your code that works
KinectCV& kinect = freenect.createDevice(0);
is not an assignment, it's initialization. It can be rewritten using the initialization syntax instead of the assignment syntax, like this:
KinectCV& kinect(freenect.createDevice(0));
The reason behind it is that in C++ all initialization must happen in the constructor, not in a "designated initialization function". C++ has no idea that init is your initialization function; all it knows is that once the constructor is over, the object must be in a consistent state, included with all the references that it might hold.
Moving the initialization code into MyKinect's constructor will fix the problem:
class MyKinect {
public:
KinectCV kinect;
MyKinect() : kinect(freenect.createDevice(0)) {
}
}

What is wrong with my function? (method to copy from private class member into passed in float) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
These functions are both public members of a class. Private members of the class include *theCharArray and *theFloat.
This one works fine:
void theClass::getCharArray(char charArrayParam[]) const
{
strcpy(charArrayParam, this->theCharArray);
}
This one underlines "this" and VS express says "Error: Expression must be modifiable value"
void theClass::getFloat(float theFloatParam) const
{
theFloatParam = this->theFloat;
}
Please tell me what I'm doing wrong.
In theClass::getCharArray(char charArrayParam[]), charArrayParam is passed basically as a pointer to character array without any idea of the buffer size. This is kind of risky with the risk of overflowing the buffer. Netter interface would be:
theClass::getCharArray(char *charArrayParam, int charArraySize) const {
strncpy(charArrayParam, this->theCharArray, charArraySize - 1);
charArrayParam[charArraySize - 1] = 0;
}
And for the second one:
void theClass::getFloat(float *theFloatParam) const
{
*theFloatParam = this->theFloat;
}
otherwise, since theFloatParam being passed by value, changing that within the function has no effect on the caller.