Click here to Skip to main content
15,919,749 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Object slicing
correct me if i am wrong what is think "the best way to avoid object slicing is to use reference variable instead of object its self (i mean in up-casting of classes).
But i have also read that by making base class abstract we also avoid object slicing. I agree that compiler will generate an error in that case. But that will also restrict the creation of object of base class.So please tell me except reference or pointer object is there any other way to avoid object slicing. And please take in consideration no class should be abstract and copy constructor(base class) should be public.
Is there any other way?
Thanks in advance
Posted

The answer is yes.

You can add a virtual method that produces a copy of the object, and returns a pointer type, of the base class. You'll have to write this method for every derived class.

C++
class CBase
{
public:
    virtual CBase * MakeCopy(void) const { return new CBase(*this); }

public:
    CBase() {}
    CBase(const CBase & rhs) {}
    virtual ~CBase()
};

class CDerived : CBase
{
public:
    virtual CBase * MakeCopy(void) const { return (CBase *) new CDerived(*this); }

public:
    CDerived() {}
    CDerived(const CDerived & rhs) : CBase(*(CBase *) this) {}
    virtual ~CDerived()

};


But you're right, you can't pass the object by value, to a type it's derived from without slicing.
 
Share this answer
 
v4
Comments
kaushik4study 29-Aug-12 15:31pm    
WEll first of all thank you for answer and writing. As per your answer what i guess.
1. Every function should be virtual.
2. no member variable should be private.

AND you have not mentioned how will you call them in main() i guess you are using pointer variable or reference variable. If yes then its okh if not then please tell me other way too.
JackDingler 29-Aug-12 15:42pm    
You didn't give me much to work with. You weren't really clear. I had to guess to a large degree, exactly what you are asking for. I provided 'a' solution to the object slicing problem.

Your question reads like a class assignment, and if that's the case then you're likely to have been presented information in the lecture that helps in understanding the request.

Further you made no mention of them being called in main() or what the context of those calls are. I made a guess as to your intent. It's very likely that I may not have provided the answer that you need. How would you call methods in an object?

And as to your questions, 1 and 2, those answers are false in the case I presented. They may well be true in other cases, such as one where the base class contains all of the member variables any derived class might need AND no derived classes instantiate any additional member variables. Perhaps that's the solution you're looking for?

I did leave out one 'public:' keyword. I'll repair my example.
JackDingler 29-Aug-12 16:12pm    
After rereading your question, I can add a bit.

There are times when you do want to make a copy of an object, so that a function can use it as a temporary variable and safely modify the contents. If the function you're calling is only aware of the base class implementation, then how can it create a temporary, when it's actually being passed a reference to a base and not a derived class?

The short answer is that it can't, because it doesn't have enough information to 'know' this.

But if it knows that the base class has virtualized copy method, it can call that method, and perform those operations on the pointer that is returned. In this way it can instantiate derived objects by proxy and use them as temporary objects.
Object Slicing is occurred when a derived class object is passed by value as a BaseCls class object, the base class copy constructor is called. We know that using virtual functions concept.
 
Share this answer
 
v2
Comments
Richard MacCutchan 1-Dec-14 3:58am    
Please do not post in questions that are more than two years old.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900