CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: vtable problem

  1. #1
    Join Date
    Dec 2010
    Posts
    121

    vtable problem

    Code:
    boost::shared_ptr<SimObject> pObj = m_simObject;
    	if (pObj)
    		dynamic_pointer_cast<SimObject>(pObj)->ProcessMovement();
    In the code snippet above,
    SimObject defines a ProcessMovement method which is virtual....
    and CPF_SimObject defines the overrided version of it.

    Vehicle is a derived class from CPF_SimObject which doesn't have the overrided method from CPF_SimObject
    But m_simObject is simply a SimObject which created off Vehicle
    In the above case, I want to call the SimObject::ProcessMovement, which doesn't succeed.

    It is still calling the CPF_SimObject version... any ideas why?
    Thanks
    Jack

  2. #2
    Join Date
    Feb 2017
    Posts
    677

    Re: vtable problem

    The reference to "vtable" suggests you want to surpass the override mechanism of virtual functions?

    The best way would be to change the design. Why not just make the base class implementation of ProcessMovement available (under some other name) in the derived classes? You can do this by inheritance or even better, as a free function.

    But there is another way I don't recommend. It's called object slicing and is normally considered a trap you fall into by accident. Slicing is when you copy a derived object onto a base object because then the derived part is "sliced away". With your code it would look something like this,

    Code:
    if (pObj) {
        SimObject slice = *pObj; // ugly slicing on purpose, derived part of *pObj object is lost
        slice.ProcessMovement(); // calls SimObject version of function
    }
    Last edited by wolle; April 6th, 2017 at 07:27 AM.

  3. #3
    Join Date
    Dec 2010
    Posts
    121

    Re: vtable problem

    In other words, the preferred way is to give another specific implementation of ProcessMovement for this particular use case....
    The first use case is called AStar which find paths in specific way then process the movement in one specific way also
    The second use case is called CPF which find paths in another way then process the movement in another specific way

    How can I abstract them out?
    I have 3 kinds of pathfinders and 3 kinds of ways to process the movements
    CPF is a special one, AStar is a normal one
    but only one type of SimObject... Maybe I don't bother using a CPF_SimObject which is a specific way
    to process movement.... I haven't considered there would be some extensions to this (a new pathfinder and a new way to process it )...
    Let's say I have added one more pathfinder to the mess....
    Last edited by luckiejacky; April 6th, 2017 at 02:45 AM.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: vtable problem

    In cases like this it is helpful if you create the simplest possible complete test program that demonstrates the issue. We can then see what is being tried and make suggestions accordingly when can then be tried in the test program until the required outcome is achieved. Once the test program behaves as required then the soultion can then be incorporated into the main program. It's usually easier to try things out in a small test program than in the actual program.

    Cheers.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Feb 2017
    Posts
    677

    Re: vtable problem

    Quote Originally Posted by luckiejacky View Post
    How can I abstract them out?
    I have 3 kinds of pathfinders and 3 kinds of ways to process the movements
    You don't have to use inheritance to create variation. In fact the second most important rule in object orientation (OO) is: Favour object composition over class inheritance.

    So rather than deriving from SimObject you can pass in a PathFinder object and a MoveProcessing object to its constructor when it is created. In this way SimObject will vary depending on how it is composed. You can even change the composition by passing in other PathFinder and MoveProcessing objects. This actually is the State design pattern: Allow an object to alter its behavior when its internal state changes; The object will appear to change class.
    Last edited by wolle; April 8th, 2017 at 12:07 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured