C++ Reference Variable [closed] - c++

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)) {
}
}

Related

Would this be considered bad programming practice? [closed]

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.

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.

How to maintain initialization of struct as members are added? [closed]

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
I have a question that is sort of a follow up to this:
Initializing default values in a struct
I've got a struct that's already got 17 bools, and a clear() method that sets them all to false. It's a long term project; the code could still be in use years from now and get added to. Is there a way to initialize all members that will extend automatically, so that someone adding new members to the struct doesn't need to remember to add them to the clear() method (other than a comment saying "please don't forget")?
This code base is not C++11 at this time, so I don't think I can initialize in the declaration.
The code is like this:
typedef struct {
bool doThingA;
bool doThingB;
bool doThingC;
bool doThingD;
// etc for several more bools
void clear() {
doThingA = false;
doThingB = false;
doThingC = false;
doThingD = false;
// etc...
}
} EnableFlags;
struct EnableFlags {
bool doThingA;
bool doThingB;
bool doThingC;
bool doThingD;
// etc for several more bools
void clear() {
*this = EnableFlags();
}
};
This will create a temporary with all members set to zero and then make *this a copy of it. So it sets all the members to zero, no matter how many there are.
This assumes that you haven't defined a default constructor that does something other than set all the flags to false. If you have no user-defined constructors then that assumption holds.
Since C++11 it's even simpler:
void clear() {
*this = {};
}
One option is to use a static assertion about the size of the structure inside the clear function.
First determine the current size of the struct. Let's say it's 68. Then, add this:
void clear()
{
BOOST_STATIC_ASSERT(sizeof(EnableFlags) == 68 && "You seem to have added a data member. Clear it before changing the number 68 to match current struct size!!");
// the rest as before ...
}
I've used Boost's static assertion macro, but you can use any other one you want, or roll out your own.
With such an assertion, the code will fail to compile when the size of the structure changes. It's not a catch-all solution, but does provide a safeguard.
Just use objetcs.
So you can use a 'for' loop to check in a std::vector if their values are false or true.
So you don't have your futurs co-workers put the "false" value each time they create a new boolean variable.
Structures are inapropriates here.