dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Renaming Concept Dough McIlory

  1. #1
    Join Date
    Mar 2009
    Posts
    4

    Smile Renaming Concept Dough McIlory

    Hi All,

    I was going through "The Annotated C++ reference manual" by Margaret A. Ellis and Bjarne Stroustrup (I got this **** cheap - a profit of going for LOW price edition :) )

    Where I saw a concept on renaming by Dough McIlory, chapter Derived Classes Section 10.11c page 236-237
    But I am not able to understand and Implement this.

    Can anyone be a help on this.

    Thanks
    n_khullar

  2. #2
    Join Date
    May 2007
    Posts
    843
    Can you post the section here so we can read it for you ?

  3. #3
    Join Date
    Mar 2009
    Posts
    4
    class Lottery
    {
    public:
    int virtual Draw()
    {
    cout<<"Class Lottery funtion is being drawn";
    return 0;
    }
    };

    class GraphicObject
    {
    public:
    void Draw()
    {
    cout<<"Class GraphicObject funtion is being drawn";

    }
    };

    class LLottery: public Lottery
    {
    public:
    // virtual int l_draw() =0;
    int l_draw()
    {
    cout<<"Class Lottery funtion is being drawn";
    Draw();
    return 0;
    }
    };
    class GGraphicObject : public GraphicObject
    {
    public:
    // virtual void go_draw() =0;
    int go_draw()
    {
    cout<<"Class GraphicObject funtion is being drawn";
    Draw();
    }
    };

    class LoterySimulation: public LLottery,public GGraphicObject
    {

    int l_draw();

    void go_draw();
    };
    renaming of overidden function is being done in LLottery and GGraphicObject but i am not able to understand how we are going to remove the ambiguity.Can we write a sample program to demonstrate this renaming behaviour?
    Last edited by n_khullar; 04-03-2009 at 05:14 AM.

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    You use a qualified name to disambiguate a call to a member function of a base class:

    Code:
    class LoterySimulation: public LLottery,public GGraphicObject
    {
    
    int l_draw() {Lottery::Draw(); return 0;}
    
    void go_draw() {GraphicObject::Draw();}
    };
    Danny Kalev

  5. #5
    Join Date
    Mar 2009
    Posts
    4
    Hi Danny,

    Delay in posting but I am not getting why extra classes (GGraphicObject and LLottery), were introduced here.

    Excerpts from book:

    " Dough said: Renaming could be achieved through the introduction of extra class for each class with virtual function that needs to be overridden by a function with different name plus a forwarding function for each such function."

    For example: "Please refer the code that I have added. in previous post - #3"

    I am not able to understand why do we need to introduce extra classes and what am I missing when I code it as follows (with no intermidiate classes):

    class Lottery
    {
    public:
    int virtual Draw()
    {
    cout<<"Class Lottery funtion is being drawn"<<flush<<endl;
    return 0;
    }
    };

    class GraphicObject
    {
    public:
    void Draw()
    {
    cout<<"Class GraphicObject funtion is being drawn"<<flush<<endl;
    }
    };

    class LoterySimulation: public Lottery,public GraphicObject
    {
    public:
    int l_draw() {
    cout<<"Class Lottery funtion is being drawn"<<flush<<endl;
    Lottery::Draw();
    return 0;
    }

    void go_draw()
    {
    cout<<"Class GraphicObject funtion is being drawn"<<flush<<endl;
    GraphicObject::Draw();
    }
    };

    int main()
    {
    LoterySimulation obj;
    obj.l_draw();
    }

  6. #6
    Join Date
    Dec 2007
    Posts
    401
    let us say, we have
    Code:
    struct lottery
    {
        virtual void draw()
        {
            // make a random draw to determine the winner of the lottery
        }
    };
    
    void publish_result( lottery* l )
    {
        l->draw() ; // determine the winner of the lottery
        // and print out the result of the draw
    }
    and
    Code:
    struct graphic_object
    {
        // draw the graphic object on a viewport (on the screen)
        virtual void draw() = 0 ;
    };
    
    void paint_object_on_screen( graphic_object* object )
    {
        // paint the screen background
    
        // and call draw() on the object to paint it
        object->draw() ;
    }
    when merging the two hierarchies using multiple inheritance, we have the two base classes using the same name draw() for virtual functions having unrelated semantics (performing completely different tasks). this results in a name clash. (note that the clash occurs only when the virtual functions have the same name *and* the same number and types of arguments; or else the usual function name overloading resolution rules would have ensured that the problem of the name clash is not manifested.)
    Code:
    struct impossible_graphic_lottery_object : graphic_object, lottery
    {
        // we have to override draw in this class.
        // but how do we override it so that it works correctly
        // with both publish_result() and paint_object_on_screen()
        // we cannot call both the base class draw() functions; 
        // this would result in general insanity:
        //   a. when-ever publish_result() is called, the object would be 
        //      repainted (which might not be ok if a grapihic system uses 
        //      xor raster ops for painting)
        //   b. and horror: when-ever paint_object_on_screen() is called, 
        //      a fresh draw would be made for the winning lottery ticket  
        virtual void draw()
        {
          // ??
        }
    
    };
    to solve this problem, we need to introduce two extra classes; one each for the lottery hierarchy and the graphic_object hierarchy. the only purpose of these classes is to introduce new names for the draw() function so that we can distinguish between draw_lottery() to draw the winning lottery ticket from draw_graphic_object() to draw the graphic object on screen.
    Code:
    struct lottery_interface_resolver : lottery
    {
        virtual void draw()
        { draw_lottery() ; }
    
        protected : virtual void draw_lottery() { lottery::draw() ; }
    };
    
    struct graphic_object_interface_resolver : graphic_object
    {
        virtual void draw()
        { draw_graphic_object() ; }
    
        protected :
            virtual void draw_graphic_object() { graphic_object::draw() ; }
    };
    
    struct graphic_lottery_object : graphic_object_interface_resolver,
                                                         lottery_interface_resolver
    {
        // we do not override draw at all.
        // instead we override draw_graphic_object and draw_lottery
        // the name clash has been resolved by introducing different names.
    
        protected :
    
            virtual void draw_lottery()
            {
              lottery::draw() ;
              // if required, invalidate graphic_object to demand a repaint
            }
    
            virtual void draw_graphic_object()
            {
              // paint our lottery object on screen
            }
    };
    Last edited by vijayan; 04-04-2009 at 11:06 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center