473,386 Members | 1,766 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,386 software developers and data experts.

beginner's question on slicing

In his book, Stroustrup has mentioned that slicing can be a source of
surprises and errors. I am unable to understand it. Kindly give an
example where slicing can lead to surprise and error.

Thanks
V.Subramanian

Nov 5 '07 #1
2 1100
su**************@yahoo.com, India wrote:
In his book, Stroustrup has mentioned that slicing can be a source of
surprises and errors. I am unable to understand it. Kindly give an
example where slicing can lead to surprise and error.
ex.

try{
throw std::runtime_error("example error");
}
catch(std::exception e){
std::cout << "there has been an error: " << e.what() << "\n";
}

Regards,

Zepp
Nov 5 '07 #2
"su**************@yahoo.com, India" <su**************@yahoo.com>
wrote:
In his book, Stroustrup has mentioned that slicing can be a source of
surprises and errors. I am unable to understand it. Kindly give an
example where slicing can lead to surprise and error.
struct foo {
virtual int fnc() { return 5; }
};

struct bar : foo {
virtual int fnc() { return 7; }
};

void test( foo y ) {
cout << y.fnc() << '\n';
}

int main() {
bar x;
test( x );
}

What should be output on the screen? One would expect that since we
passed in a 'bar' object to test, it should output '7', but because of
slicing, it outputs '5'.

If 'test()' took a foo& or foo*, or if 'test()' were a template, then
the output would have been '7'.
Nov 5 '07 #3

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

Similar topics

9
by: Jerry Sievers | last post by:
Fellow Pythonists; I am totally puzzled on the use of slicing on mapping types and especially unsure on use of the Ellipsis... and slicing syntax that has two or more groups seperated by comma....
3
by: Bryan Olson | last post by:
I recently wrote a module supporting value-shared slicing. I don't know if this functionality already existed somewhere, but I think it's useful enough that other Pythoners might want it, so here...
11
by: jbperez808 | last post by:
>>> rs='AUGCUAGACGUGGAGUAG' >>> rs='GAG' Traceback (most recent call last): File "<pyshell#119>", line 1, in ? rs='GAG' TypeError: object doesn't support slice assignment You can't assign 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...
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...
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
3
by: jmDesktop | last post by:
This program: s = 'abcde' i = -1 for i in range (-1, -len(s), -1): print s, i gives abcd -1
1
by: subramanian100in | last post by:
Consider class Base { .... }; class Derived : public Base { ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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,...

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.