Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 09 Dec 1999
Offline Last Active Yesterday, 11:27 PM

#5147379 c++ oo syntax behind the scenes behavior

Posted by SiCrane on 16 April 2014 - 10:28 AM


where is the VMT stored, and when ?

With the memory of the class, when the class is created - usually alongside the member variables in memory.

I think you're confusing a pointer to the vtable and the vtable itself. An object will store a pointer to the vtable not the full table. The vtables themselves are generally stored in a read only data segment. 

#5146762 Error from/with unique_ptr trying to acces private member, error C2248

Posted by SiCrane on 13 April 2014 - 02:28 PM

A unique_ptr is movable but not copyable, which means a std::vector of unique_ptrs is also not copyable. And a class that contains a std::vector of unique_ptrs would also not be copyable with the default copy constructor or assignment operator. One option in your case is to give your Bag a copy constructor that clones all the pointed to Items. Another option is to use shared_ptrs instead of unique_ptrs.

#5146175 What does the standard say about this?

Posted by SiCrane on 11 April 2014 - 01:07 AM

According to the standard, "The full expressions in an initializer-clause are evaluated in the order which they appear." Section 8.5.1 paragraph 17 of C++11. Also, the elements of std::array<> constructed without an initializer are default initialized. In the case of a std::array of primitives this means no initialization is performed.

#5143724 Deleting pointers to inherited objects

Posted by SiCrane on 01 April 2014 - 10:22 AM

Deleting a derived object through a pointer to the base when there is no virtual destructor in the base class is undefined behavior. This means anything can happen including memory being freed properly. In practice what often happens is that the base class destructor is called and then a pointer allocated memory is passed to a deallocation function. Since most deallocation functions only accept a pointer without needing you to know how much memory was originally allocated, the memory of the class itself will be freed properly. However if the derived class contains objects that dynamically allocate memory (ex: a std::string object if small string opt didn't kick in) if the destructors for those objects are not called there will be a memory leak. Of course, this is only a common case. There are other possibilities, many of which will involve corrupting the heap.

#5141491 Management of game objects

Posted by SiCrane on 23 March 2014 - 12:38 PM

If you're using pointers, there isn't anything that stops you from having both a master Entity * container and separate containers for specific types. You can iterate over the master list when you want to batch process everything and over the type containers when you just want projectiles or enemies.

#5140343 What is the best 3D game to make first?

Posted by SiCrane on 19 March 2014 - 10:22 AM

A good beginning game is a Rubik's cube simulator. It's you, 36 polygons and no way out without actually understanding rotations. (Which sounds like it would be the worst locked room puzzle in an RPG ever.)

#5140205 Editing a Python Script during Runtime?

Posted by SiCrane on 18 March 2014 - 08:45 PM

How did you load it in the first place? If you imported it as a module then you can reload it with PyImport_ReloadModule().

#5139483 Is Python any good for...?

Posted by SiCrane on 16 March 2014 - 11:23 AM

Note that Python doesn't have to be as painful to embed as Vortez's code indicates. There are wrapper libraries for C++ like boost::python that make things significantly easier to work with.


One problem with Python as an embedded scripting language is that the CPython interpreter makes it very difficult to run Python scripts in a restricted execution environment (sandboxing).This isn't a big deal for some applications, but it can be a problem for games.

#5139265 Need help: a vector of different extended classes

Posted by SiCrane on 15 March 2014 - 11:28 AM

// removing entities
delete *it; // make sure to delete the entity being removed from the list

You're trying to delete an element you've just erased. You may want to reconsider your order of operations here.

#5135897 Memory overwrite after allocating 4k ints

Posted by SiCrane on 02 March 2014 - 01:50 PM

Im basically forcing the class to work inside vectors. (that Im assuming to require a copy ctor)

In C++ parlance, std::vector requires that the object it stores are copyable. std::vector expects to be able to copy objects as it reallocates or if existing objects are added to it.

This changes with C++11, which allows non-copyable objects if they are moveable. Of course you would need to properly implement your move constructor and move assignment operators so the amount of work is about the same.

#5133597 Smart Pointers Confusion

Posted by SiCrane on 22 February 2014 - 10:46 AM

Calling a member function on a deleted object is undefined behavior, which often means a crash, but can really do anything at all including succeeding. In this case I'm guessing that your member function doesn't actually use any member variables so doesn't actually access any deleted memory.

#5131938 Reusable function for adding values from a container

Posted by SiCrane on 17 February 2014 - 12:11 AM

This looks like a job for pointer to members:

template <typename T, typename U, typename Container>
T SumValuesFrom(Container c, T U::* member_ptr) {
  return std::accumulate(c.begin(), 
                         [=](T a, const U & b) {
                           return a + (b.*member_ptr);

int main(int, char **) {
  std::vector<MyStruct> myContainer = /* */;
  int total = SumValuesFrom(myContainer, &MyStruct::memberVariable);

#5130961 Environment for Python?

Posted by SiCrane on 12 February 2014 - 10:50 PM

At work most of us use PyCharm.

#5128285 std c89 not working with gcc?

Posted by SiCrane on 02 February 2014 - 06:55 PM

Did you try using -pedantic or -pedantic-error?

#5125371 C++ trivia

Posted by SiCrane on 21 January 2014 - 09:45 AM

y in getY() isn't a temporary. Returning a const reference to it won't extend it's lifetime.