Visual C++ member variables changing unexpectedly and unexplainably [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm initializing an instance of PhysWorld class as shown here:
At this point the member variables are as follows:
This seems correct to me.
Then this line executes:
We step into:
And at this point, the member variables look like:
Can someone please help me understand what is going on here? This is one of my first attempts in c++ so I'm guessing it's something stupid on my part.
Thanks!

You probably loose variable value on assignment:
pw = PhysWorld(...);
This statement constructs a temporary object, and then makes a call: pw.operator=(const PhysWorld&);. Check how you implement it (if you do).
Also your function setRectDef contains a serious bug: you are storing a pointer to a stack variable, which would be invalid after leaving the function scope, and accessing it later most likely ruin your stack.
Edit: how to handle tmpS.
You need to allocate your structure on heap:
b2PolygoinShapre *tmpS = new b2PolygoinShape;
tmpS->SetAsTextBox(...);
this->rect = tmpS;

Related

Implementation of vector of integers with overloaded operators, fails to run properly [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have huge problems with making this code work properly.
http://pastebin.com/Mi6gj188
There's an output from example program on the bottom. It simply crashes and doesn't deliver proper results too. It seems that none of the overloaded operators work as it should
You didn't write a copy constructor, or use RAII. As a result, every time your vector object is copied (and it is, a lot, because you make no use of references!!) your internal data pointer is copied, sharing it amongst multiple objects (each of which will attempt to delete it on destruction) causing a horrible bug.
Your book tells you about the rule of three, which you should now go ahead and work on following.

Is it possible to create variables during runtime in c++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Is it possible to create a new variable for a function everytime the function is called? I was considering making a huge array and just using a different block for each time a function is called but I would prefer to create variables on demand instead, is this possible in c++?
Example:
A user types asdf and clicks save, setting off the savetext function
void savetext(textvariable)
{
static int //(this variable name should somehow become asdf) = somedata;
return;
}
If you have something that, for example, adds things to an array, then vector is the right solution. The vector class will automatically grow as you need it, using the push_back function to add things to itself.
The vector class acts largely as an array that grows as you need it to, so it's very easy to use.
Every time you enter a function, all variables in it are new. There's no need to explicitly create variables unless you need objects that live longer than your function, or you don't know how many you need.

I wish to know how a function signature is validated [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I currently believe it is the compiler or the return type that does this but I'm not sure I just need a clarification
A function signature is matched first by it's name, then by it's arguments. Never by it's return type. For example if you have two methods called Add one taking two int values and returning a int and one taking two string values and returning a string, and you called it passing ints it wouldn't matter what you were setting it to the int version would be called.
Where this can get you in a bit of trouble is when you try method overloading with different numeric values. the compiler will try and help you (if you call a method expecting ints but pass a short it will try and figure out what you are trying to do).

Making a STL Queue of pointer types in C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I can't quite seem to figure out why this doesn't work. I tried to make a queue of pointer types and it failed. I have a class Room, and I want to make a queue of pointers to Room.. so I did:
queue<*Room> bfsRooms;
this gave me the error:
`*' cannot appear in a constant-expression
Does this mean it is impossible to make a STL queue of pointers?
No it should be fine, you should really link the exact code and exact error so we can help you better.
That said a queue of pointers would look something like this:
Room r;
std::queue<Room*> rooms;
rooms.push(&r);
EDIT: it is worth noting that if these pointer own their objects they point to you should really encapsulate the pointer in a smart pointer, something like:
std::queue<std::unique_ptr<Room>> rooms;
This abide by the RAII principle and will automatically clean up resources.

Using const on local variables [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Similar to this question, what are the pro/cons to using const local variables like in this answer?
Personally, I like to use const for any declared object (I'm not sure the word "variable" applies) unless I'm actually going to modify it. It documents to the reader that the value of the object is always going to be whatever it was initialized to, which can make the code easier to follow and analyze.
(It can also help the compiler in some cases, but most compilers, when invoked in optimizing mode, are smart enough to notice that the object is never modified. And if you invoke the compiler in non-optimizing mode, you're telling it that you don't care much about performance.)
In fact, if I were going to design my own language, all declared objects would be const (read-only) by default unless you explicitly mark them as modifiable.