Bill Door

Member
  • Content count

    9
  • Joined

  • Last visited

Community Reputation

360 Neutral

About Bill Door

  • Rank
    Newbie
  1. Compile time "polymorphism"

    A similar (but more in-depth) technique I have used is called "Runtime Polymorphism" or originally "Concept-based Polymorphism". This polymorphism is different than inheritance. The technique is described in "Better Code: Runtime Polymorphism" (PDF) and in the video "Value Semantics and Concepts-based Polymorphism" from C++ Now! 2012.
  2. Regarding casting -- #gonrogon advice is sound. To add a bit, whenever you use a cast, any of the cast's but especially C-style casts, you are saying that you are smarter than the compiler. That's a risk that I don't like to take unless I am very confident.    If I use a cast, it's because I tried without the cast and the compiler told me what I needed to do to get the cast that I wanted. The compiler is your friend.
  3. Your example works because the deleted object is the type with the virtual destructor.  e.g.        B *p = new AB;     delete p;   However,       A *p = new AB;     delete p;   I would expect that this would not call the destructor for AB or B.   In the first example, the object is of type B, which has a virtual destructor, which causes the virtual table to be used which calls the destructor for AB, which in turn destroys, A and B.   As an object of type A, the compiler has no idea that it should do anything except call the destructor for A. There is no virtual table to access, so only the destructor for A is called.
  4. Your ROOMCLASS is not properly copyable. A vector copies and owns each object as it is inserted into the vector. A new ROOMCLASS instance is created using the copy constructor and the old one is deleted. vector requires copyable classes. I expect that you wrote ROOMCLASS so that it properly destroys the texture when it is deleted. You need a system for copying the texture reference or creating a new texture reference in the new instance of ROOMCLASS. shared_ptr is often referred to for this purpose. I consider shared_ptr a crutch. Each class should learn to manage it's own resources. In this case, perhaps a management class for textures that uses shared_ptr would be useful. I would avoid using shared_ptr as a requirement for using HOUSECLASS.
  5. This is not the canonical method of erasing elements from a vector. Take a look at remove_if in algorithms. The typical problem is that the iterator is invalid after the erase. iterator it = remove_if(enemyVector.begin(), enemyVector.end(), YourPredicate); Then use this new iterator to visit the removed elements to delete them. I would then revisit to erase all in one block. for_each(it, enemyVector.end(), DeleteFunction); enemyVector.erase((it, enemyVector.end()); Three functions calls, two simple (inline) functions.
  6. The question would be why do you think STL has "overhead"? Each container has it's expected expectations and limitations. Understand the expectations and limitations to make your decisions. The c++2011 defines an unordered map (aka hashtable). If you're not using a C++-2011 installation, then you should be able to find something compatible (e.g. boost).
  7. C++ As First Language

    I find it interesting how people seem to overstate how difficult it is to learn programming from C++. I teach high school students programming using Stroustrup's [url="http://www.stroustrup.com/Programming/"]Programming: Principles and Practice Using C++[/url]. This is a text book for an introductory course on computer programming. The students have found the book to be readable, engaging, and useful. There are practical exercise in learning to solve real problems in programming. The language choice is naturally C++. I find C++ to be fine as an introductory programming language.
  8. Pointer to class type?

    [quote name='fotogames' timestamp='1341563419' post='4956256'] Thanks for the responses! I'm trying to make some kind of state machine, and I wanted to be able to tell the machine which class the state machine had to make (sounds weird but my english vocab is not broad enough to explain it clearer than this [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img]) However I've been thinking and I don't think I need an infinite state machine, so I'm going to go with a finite state machine. Thanks for the help guys [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] [/quote] Take a look at [url="http://en.wikipedia.org/wiki/Design_Patterns"]Design Patterns[/url]. In particular the [url="http://en.wikipedia.org/wiki/State_pattern"]State[/url] and [url="http://en.wikipedia.org/wiki/Strategy_pattern"]Strategy[/url] pattern. This might help you to work out a solution. I often find the [url="http://en.wikipedia.org/wiki/Flyweight_pattern"]flyweight[/url] pattern useful for state machines.
  9. Need C++ Help

    C++ is a fine language to learn. I teach C++ programming to high school students as a first language. You do not need to learn C first. In fact, learning C first can be a handicap. I introduce programming to the students using [url="http://www.stroustrup.com/Programming/"]Programming -- Principles and Practice Using C++[/url]. This book teaches programming, while using C++ as the example language. Learning the techniques of problem solving with programming is key to learning to program. C++ is the language of choice for this book. Good luck and Enjoy.
  10. Question about inheration

    [quote name='jlouts' timestamp='1315658349' post='4859961'] I believe you have to make the Render function in CMenueButton virtual as well to override CMenueElement's Render function. [/quote] In C++, once virtual always virtual. The declaration of virtual in the base class is sufficient. Overrides in other subclasses will also be virtual.