# Perost

Member

296

332 Neutral

• Rank
Member
1. ## Tricky related class code!!

The answer to your first question is that Button uses a virtual constructor to make a copy of the action. For you second question, it simply checks that action_ has been set to something with setAction. If it has, then it calls the operator() on the Action object (which has been overloaded by the class that inherits from Action, since operator() is declared pure virtual).
2. ## "Automatic" fill

Regarding sequence points, a sequence point is a point in the code where the side-effects of the previous code has been evaluated. C++ does not specify in which order function arguments should be evaluated, so a compiler is free to evaluate a functions arguments in whichever order it wants. When you try to do fill(vec.begin() + (--f), vec.end(), f); you therefore have no way of telling what the value of the last f is, since it may or may not have been decremented. And you still seem to be confused about what algorithms such as fill and generate does. These algorithms work on a range defined by a begin and end iterator. If you were to execute the follow code: vector<int> vec(5); for(int f = 1; f <= 5; ++f) { fill(vec.begin() + (f - 1), vec.end(), f); } this is what will happen: fill(vec.begin() + 0, vec.end(), 1) => vec = {1, 1, 1, 1, 1} fill(vec.begin() + 1, vec.end(), 2) => vec = {1, 2, 2, 2, 2} fill(vec.begin() + 3, vec.end(), 3) => vec = {1, 2, 3, 3, 3} fill(vec.begin() + 4, vec.end(), 4) => vec = {1, 2, 3, 4, 4} fill(vec.begin() + 5, vec.end(), 5) => vec = {1, 2, 3, 4, 5} So if you fix the problems in your code that leads to undefined behaviour it will indeed work. But, you will have used (n^2 + n)/2 = 15 (with n = 5) assignments, instead of just assigning each elements once (you're using a quadratic algorithm instead of a linear one). If you use a vector with 100 elements you will instead have to do 5050 assignments instead of 100. I'm sure that you realize that that's not a good way of solving the problem. So if you wish to assign each element in a vector some value, use either a for-loop or an algorithm such as generate, but not both.
3. ## Interesting Class Declaration

Quote:Original post by AngleWyrm "According to the C++ standard" should probably be a link to a reputable source instead of an assertion, when arguing against the content of cpp.com's reference material. You mean like cpp.com ;)
4. ## C++ HELP PLEEEAAAASSSEEEE

Think about what sumGrades really does, and how that corresponds to what the input file contains. Does it really sum all entries in the input file? Also, have you written the function prototypes, or where they supplied by your professor? It seems to me that sumGrades doesn't really need the m, gender and GPA parameters, since they are not used outside of the function. They should probably be local variables instead. You should also learn to use the debugger, which will be able to tell you where you program crashes.
5. ## [c++] Create a Child Object from within Parents Constructor

The constructor for a base class is always executed first when you use inheritance in C++, so you are getting an infinite loop. Because InputEngine inherits from Engine2d, what will happen when you create an instance of Engine2d or InputEngine is that the constructor for Engine2d is executed first. In the constructor for Engine2d you create a new instance of InputEngine, which should result in the constructor for Engine2d executing for the new InputEngine instance, which will execute the constructor for Engine2d, etc. So you cannot create a new instance of InputEngine in the constructor for Engine2d as long as InputEngine inherits from Engine2d.
6. ## Having problems in text based game

Is your hasRoom method working correctly? If it's not, then the second room will not be added to the first. So use your debugger, or print a trace message when adding the room to make sure that it really is added.
7. ## c++: passing class pointers into constructors

Quote:Original post by wild_pointer Quote:Original post by Perost In GCC 4.4 I get the slightly more helpful error messages: *** Source Snippet Removed *** So it seems like it ignores the parenthesis on "Wrapper(cfg);", and parses it as "Wrapper cfg;" instead. I'm actually a bit surprised at this, since this only seems to be the case if the constructor takes one argument that is a variable. But I guess no one is surprised that C++ isn't always consistent... I'm sure that there's some weird syntax one can use to call the constructor in the way you tried. Anyway, if you would call the constructor with for example "Wrapper(new Cfg());" instead, then it works, but doesn't really do what you want it to as it just creates a temporary instance of your class as Hodgman explained. I think it's actually interpreting that as a c-style cast. It's kind of fortunate too because otherwise there would have been no error at all and jamesd128 would have been left with the more confusing problem of why his default constructor wasn't doing anything. Actually, I think GCCs error message is quite clear that it interprets the line as a declaration.
8. ## c++: passing class pointers into constructors

In GCC 4.4 I get the slightly more helpful error messages: main.cpp: In constructor 'Wrapper::Wrapper()': main.cpp:10: error: conflicting declaration 'Wrapper cfg' main.cpp:9: error: 'cfg' has a previous declaration as 'Cfg* cfg' So it seems like it ignores the parenthesis on "Wrapper(cfg);", and parses it as "Wrapper cfg;" instead. I'm actually a bit surprised at this, since this only seems to be the case if the constructor takes one argument that is a variable. But I guess no one is surprised that C++ isn't always consistent... I'm sure that there's some weird syntax one can use to call the constructor in the way you tried. Anyway, if you would call the constructor with for example "Wrapper(new Cfg());" instead, then it works, but doesn't really do what you want it to as it just creates a temporary instance of your class as Hodgman explained.
9. ## How do I clear correctly std::map, vector etc...

You can also store smart pointers that automatically delete the memory when you call clear. You can use Boost's smart pointers that will be included in the next C++ standard, or their smart pointer containers.
10. ## Some articles mising

Internet Achive to the rescue! Basics of Game AI Introduction to Learning in Games
11. ## I'm relatively new to C++, having trouble with remembering stuff.

Another good book is "Thinking in C++" by Bruce Eckel, which has been made available as a free download here. But as Washu said, perhaps you should look at other alternatives than C++ if you only want to create games.
12. ## Float of double for timing

Quote:Original post by Nanoha I'm no expert ont his but it sounds reasonably to me. Usually float = 32 bits, double = 64 bits. If your on a 32 bit system then yeah it should be faster to work with floats than with doubles. On 64 bit I guess they would be the same but I'm not really sure. Actually, I believe that all mainstream processors since the first Pentiums use 80-bit FPUs internally. So both float and double are casted to 80-bit before any operations are done, unless you're using SSE or something. Doubles are still twice as large though, so they have larger memory overhead.
13. ## How do you implement PAUSE?

Quote:Original post by ricardo_ruiz_lopez BTW, how I calculate delta on my first frame? You could just set it to 0. I don't think anyone will notice if nothing moves in the first few milliseconds it takes for the first frame to be drawn [wink]
14. ## Utterly confusing problem

Both Shape and Ship has several member variables that you don't set in the constructor for these classes, and therefore they are undefined until you call the appropriate setter. So the ships position is completely undefined, it could be anywhere. When you call GetPosition the position might get set to (0, 0, 0) for some reason, but that is just luck (and programmers don't rely on luck). So either you need to make sure to pass the relevant data to the constructor for each class, or provide appropriate default values.
15. ## C++ Pointer Theory

There's also my favourite, Pointer Fun with Binky, on youtube.