problem with multimaps and virtual functions

This is a discussion on problem with multimaps and virtual functions within the C++ Programming forums, part of the General Programming Boards category; The problem I have having is slightly odd. Am not sure if it is specific to gcc but anyway. I ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    5

    problem with multimaps and virtual functions

    The problem I have having is slightly odd. Am not sure if it is specific to gcc but anyway.

    I am creating a multimap container <double,scatter> where scatter is a parent class. I am then populating the map with a number and a child object. with in the class there is a call to a virtual function which is different for different child objects, however when I trace the function call in gdb(debugger) it seems to call the parent class function instead of the desired child class.

    if the viftual fuction is called foo then the call is with the code
    Code:
    prob = RandomNumber() * totalprob;
    					
    	sc_mapi = scattering_map.begin();
    	for (double probcount=0; probcount<prob; ++sc_mapi)
    	{
    		probcount +=sc_mapi->first;
    	}
    	sc_mapi->second.foo();
    where sc_mapi is an iterator and totalprob is the sum of all doubles in the map. If you can see anything wrong with the implementation here or have had a similar problem ..any help would be great.

    Thank in advance.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    one solution is to store points of the derived classes cast to their base class in the map.

    std::multimap<double, scatter *>

    Kuphryn

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,670
    In other words, if your multimap contains type "scatter" then that's exactly what it will store - objects of type scatter. So any virtual function calls will resolve to the scatter implementation.

    Polymorphism only works through the use of a pointer type or reference type.

    gg

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    5
    I have tried tp do this .. using a <double,*scatter> multimap and then acclocating the pointers to the child class with
    Code:
    ChldScatter *scatter=new ChildScatter();
    multimap.insert(make_pair(double,scatter));
    but when trying to access the virtual function with
    Code:
    iterator->second->foo();
    it segfaults ... help ?

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,670
    Simple example:
    Code:
    #include <iostream>
    #include <map>
    using namespace std;
    
    class Base
    {
    public:
        virtual void foo() const {cout << "Base::foo" << endl;}
    };//Base
    
    class Derived
    {
    public:
        virtual void foo() const {cout << "Derived::foo" << endl;}
    };//Derived
    
    int main()
    {
        multimap<int, Base*> mm;
    
        Base B1, B2;
        Derived D1, D2;
    
        mm.insert(make_pair(1, &B1));
        mm.insert(make_pair(1, &B2));
        mm.insert(make_pair(2, (Base*)&D1));
        mm.insert(make_pair(2, (Base*)&D2));
    
        multimap<int, Base*>::const_iterator it = mm.begin();
        for (; it != mm.end(); ++it)
            it->second->foo();
        
        return 0;
    }//main
    You'll have to post all the code if you want help with the segfault.

    gg

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21