1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Prevent Object Slicing in pass by value mechanism

Discussion in 'C' started by d_arin100, Sep 29, 2009.

  1. d_arin100

    d_arin100 New Member

    Joined:
    Sep 25, 2009
    Messages:
    10
    Likes Received:
    3
    Trophy Points:
    0
    Location:
    Bangalore

    Introduction



    This article is about the slicing of object while sending the argument through a pass by value mechanism when using polymorphism.

    Background



    While we are using passing by address mechanism the address of the base class type as well as the derived class type are same in size. But the object of the derived class is usually bigger than the base class. So while we upcast an object instead of reference or pointer the object will be sliced. Let we have a function which takes an argument as base class type object. Now if we call this function with a derived class object as parameter then the compiler will copy only the base portion of the object the derived portion of the object will be sliced.
    Code:
    Derived object before slice          Derived object after slice    
    -------------------------------      -------------------------------
    Derived vptr                         Base vptr
    Member variables of base class       Member variables of base class
    Member variables of derived class
    
    Since the compiler knows that a particular type of object will be passed as value so the derived object has been forced to become a base object. When passing by value the copy constructor of the base object will be used and which will initialize the VPTR (Derived object) with the base VTABLE and will copy only the base part of the object.

    The code



    Code:
    #include <iostream>
    #include <string>
    using namespace std;
     
    class Base 
    {
                string BaseName;
    public:
                Base(const string& basename) : BaseName(basename) {}
                string Name() 
                { 
                            return BaseName;
                }
     
                virtual string Information()
                {
                            return "We are in " + BaseName;
                }
    };
     
    class Derived : public Base 
    {
                string DerivedName;
     
    public:
                Derived(const string& basename, const string& derivedname)
                : Base(basename), DerivedName(derivedname) {}
                string Information() 
                {
                            return DerivedName + "Derive from " + Base::Name();
                }
    };
     
    void CreateSliceObject(Base base) 
    { 
                cout << base.Information() << endl;
    }
     
    int main() {
                Base base("Animal");
                Derived derived("Dog", "Animal");
                CreateSliceObject(base);
                CreateSliceObject(derived);
    }
    
    Now if we declare a function as pure virtual in the base class then the compiler will prevent the object slicing because it will not allow creating an object of base type (This is generally happen for upcast by value). The compiler will show a compilation error and will prevent the object slicing.

    Code:
    class Base 
    {
                string BaseName; 
    public:
                Base(const string& basename) : BaseName(basename) {}
                string Name() 
                { 
                            return BaseName;
                }
     
                virtual string Information() = 0;
    };
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
  4. rasd123

    rasd123 Banned

    Joined:
    Nov 4, 2009
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for the info, I appreciate it.
     
  5. ewyawa

    ewyawa New Member

    Joined:
    Nov 18, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Good work !
    Very cool, looking great so far. Keep going, I wanna see it finished!
     

Share This Page