Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > slicing - copying or assigning derived object to base object

Reply
Thread Tools

slicing - copying or assigning derived object to base object

 
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      04-22-2008
Consider

class Base
{
....
};

class Derived : public Base
{
...
};

Derived d_obj;

As per my understanding, slicing happens in the following scenarios
and they should be avoided. However my question is, will we ever
require one of the following scenarios?

1) Will there be any need to instantiate Base like
Base b_obj(d_obj); // derived object sliced down to base object

2) Will there be any need to assign like
b_obj = d_obj; // again derived object sliced down to base object

3) void fn(Base base_obj_arg)
{
...
}

fn(d_obj); // slicing happens here

4) Base fn(const Derived& arg)
{
return arg;
}

Base temp = fn(d_obj); // again slicing happens here.


What I am trying to say is, if the above four scenarios are avoided,
slicing will not happen. However will there be any need for one of the
above four scenarios to be used as mandatory ?

Kindly explain.

Thanks
V.Subramanian
 
Reply With Quote
 
 
 
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      04-22-2008
"(E-Mail Removed), India" <(E-Mail Removed)> writes:

> Consider
>
> class Base
> {
> ...
> };
>
> class Derived : public Base
> {
> ..
> };
>
> Derived d_obj;
>
> As per my understanding, slicing happens in the following scenarios
> and they should be avoided. However my question is, will we ever
> require one of the following scenarios?
>
> 1) Will there be any need to instantiate Base like
> Base b_obj(d_obj); // derived object sliced down to base object
>
> 2) Will there be any need to assign like
> b_obj = d_obj; // again derived object sliced down to base object
>
> 3) void fn(Base base_obj_arg)
> {
> ...
> }
>
> fn(d_obj); // slicing happens here
>
> 4) Base fn(const Derived& arg)
> {
> return arg;
> }
>
> Base temp = fn(d_obj); // again slicing happens here.
>
>
> What I am trying to say is, if the above four scenarios are avoided,
> slicing will not happen. However will there be any need for one of the
> above four scenarios to be used as mandatory ?



There's not really a NEED to slice. But this could be something
somebody might want to do. I wouldn't advise doing it, but still.

Imagine a 3D editor. You could have a graphic card processor that
would expect data as:

class Point { public: int x; int y; };

but while editing 3D models, the application may want to attach
attributes to the points:

class EditorPoint: public Point {
std::string name;
DateTime dateCreated;
User* lastModifier;
// ...
};


So this 3D editor will work with EditorPoints, with a lot of data, but
just before sending these points to the graphic card to be rendered on
the screen, it would slice the EditorPoints down to Points:

std::vector<EditorPoint> ep;
std::vector<Point> gp(ep.size());

copy(ep.begin(),ep.end(),gp.begin());




Of course, one could argue that it would be saner to do

Point& EditorPoint::convertToGraphicCardPoint(void){
return Point(this->x,this->y);
}

transform(ep.begin(),ep.end(),gp.begin(),
boost::bind(EditorPoint::convertToGraphicCardPoint ,_1));

But the binary code executed should be very close or even identical in
both cases, I'd say.



--
__Pascal Bourguignon__
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) developereo@hotmail.com C++ 4 05-23-2007 09:32 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
problem with auto_ptr<> and assigning derived class to base class object Stephan Hoffmann C++ 2 12-11-2005 06:04 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM



Advertisments