473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

slicing - copying or assigning derived object to base object

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
Jun 27 '08 #1
1 2763
"su**************@yahoo.com, India" <su**************@yahoo.comwrites:
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<EditorPointep;
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__
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
17
by: Jon Slaughter | last post by:
I'm having a little trouble understanding what the slicing problem is. In B.S.'s C++ PL3rdEd he says "Becayse the Employee copy functions do not know anything about Managers, only the Employee...
6
by: SureshKumar.PP | last post by:
Hi. Can somebody help me out in how to solve this problem...?? class base { public: int b; }; class derived : public base
2
by: Stephan Hoffmann | last post by:
Hi, I'm new to std::auto_ptr<> and wanted to use it with a base class and several derived classes. I'm using gcc 3.3.5 and get a compile error I don't know how to resolve when compiling the...
17
by: baibaichen | last post by:
i have written some code to verify how to disable slicing copy according C++ Gotchas item 30 the follow is my class hierarchy, and note that B is abstract class!! class B { public: explicit...
19
by: AlesD | last post by:
Hello, I have problem that when I use std::list<MyClassand then store various subclasses of MyClass in that list (or any other STL container) the instances get sliced. I have read FAQ: ' What...
1
by: Bart Simpson | last post by:
Can anyone explain the concept of "slicing" with respect to the "virtual constructor" idiom as explain at parashift ? From parashift: class Shape { public: virtual ~Shape() { } ...
2
by: Rahul | last post by:
Hi Everyone, I was working around object slicing (pass by value) and was wondering how it is specified in the standard, class A { }; class B: public A
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.