object-slicing

Exception slicing - is this due to generated copy constructor?

吃可爱长大的小学妹 提交于 2019-11-28 23:43:45
I've just fixed a very subtle bug in our code, caused by slicing of an exception, and I now want to make sure I understand exactly what was happening. Here's our base exception class, a derived class, and relevant functions: class Exception { public: // construction Exception(int code, const char* format="", ...); virtual ~Exception(void); <snip - get/set routines and print function> protected: private: int mCode; // thrower sets this char mMessage[Exception::MessageLen]; // thrower says this FIXME: use String }; class Derived : public Exception { public: Derived (const char* throwerSays) :

Preventing slicing in copy constructor

送分小仙女□ 提交于 2019-11-28 08:22:45
问题 I want to copy a vector of type Foo objects but the objects can be several different derived types of Foo. I can't figure out how to copy without slicing. Here's my toy code #include "stdafx.h" #include <memory> #include <vector> #include <string> #include <iostream> class Foo { public: Foo() { m_x = "abc"; } Foo( const Foo &other ) { m_x = other.m_x; } virtual std::string ToString() { return m_x; } std::string m_x; }; class FooDerivedA : public Foo { public: FooDerivedA() : Foo() { m_y = 123

C++ slicing causing leak / undefined behavior / crash

大城市里の小女人 提交于 2019-11-28 06:35:19
问题 Is there any example of the C++ object slicing effect which can cause undefined behavior, memory leak or crash in an otherwise correct set of code? For example when class A and B (inherited from A ) are correct and sound, but calling a void f(A a) demonstrably causes nasty things. It is needed for forming a test question. The goal is to know if the participant is aware of the slicing phenomenon or not, using an example code snippet whose correctness must not be a matter of opinion. 回答1: If A

overriding virtual function return type differs and is not covariant

99封情书 提交于 2019-11-28 00:18:21
问题 Ah, SO came back just in time. I am getting a strange error: 'B::blah': overriding virtual function return type differs and is not covariant from 'A::blah' Here is the code causing the problem: class A { public: class Inner { }; virtual Inner blah() = 0; }; class B : public A { public: class Inner2 : public Inner { }; Inner2 blah() { return Inner2(); } }; I looked up the error, and according to a page I found on the Microsoft website, one of the ways types can be covariant is if: the class in

Selecting last n columns and excluding last n columns in dataframe

你离开我真会死。 提交于 2019-11-27 22:21:54
How do I: Select last 3 columns in a dataframe and create a new dataframe? I tried: y = dataframe.iloc[:,-3:] Exclude last 3 columns and create a new dataframe? I tried: X = dataframe.iloc[:,:-3] Is this correct? I am getting array dimensional errors further in my code and want to make sure this step is correct. Thank you just do: y = dataframe[dataframe.columns[-3:]] This slices the columns so you can sub-select from the df Example: In [221]: df = pd.DataFrame(columns=np.arange(10)) df[df.columns[-3:]] Out[221]: Empty DataFrame Columns: [7, 8, 9] Index: [] I think the issue here is that

Exception slicing - is this due to generated copy constructor?

女生的网名这么多〃 提交于 2019-11-27 15:13:45
问题 I've just fixed a very subtle bug in our code, caused by slicing of an exception, and I now want to make sure I understand exactly what was happening. Here's our base exception class, a derived class, and relevant functions: class Exception { public: // construction Exception(int code, const char* format="", ...); virtual ~Exception(void); <snip - get/set routines and print function> protected: private: int mCode; // thrower sets this char mMessage[Exception::MessageLen]; // thrower says this

c++: can vector<Base> contain objects of type Derived?

浪尽此生 提交于 2019-11-27 02:10:33
The title pretty much says it all. Basically, is it legal to do this: class Base { //stuff } class Derived: public Base { //more stuff } vector<Base> foo; Derived bar; foo.push_back(bar); Based on other posts I've seen, the following is okay, but I don't want to use pointers in this case because it's harder to make it thread safe. vector<Base*> foo; Derived* bar = new Derived; foo.push_back(bar); ecatmur No, the Derived objects will be sliced : all additional members will be discarded. Instead of raw pointers, use std::vector<std::unique_ptr<Base> > . It's legal but suffers from object slicing

difference between a pointer and reference parameter?

大兔子大兔子 提交于 2019-11-26 14:13:13
Are these the same: int foo(bar* p) { return p->someInt(); } and int foo(bar& r) { return r.someInt(); } Ignore the null pointer potential. Are these two functions functionally identical no matter if someInt() is virtual or if they are passed a bar or a subclass of bar ? Does this slice anything: bar& ref = *ptr_to_bar; shoosh C++ references are intentionally not specified in the standard to be implemented using pointers. A reference is more like a "synonym" to a variable than a pointer to it. This semantics opens some possible optimizations for the compiler when it's possible to realize that

c++: can vector<Base> contain objects of type Derived?

拈花ヽ惹草 提交于 2019-11-26 09:57:17
问题 The title pretty much says it all. Basically, is it legal to do this: class Base { //stuff } class Derived: public Base { //more stuff } vector<Base> foo; Derived bar; foo.push_back(bar); Based on other posts I\'ve seen, the following is okay, but I don\'t want to use pointers in this case because it\'s harder to make it thread safe. vector<Base*> foo; Derived* bar = new Derived; foo.push_back(bar); 回答1: No, the Derived objects will be sliced: all additional members will be discarded. Instead

difference between a pointer and reference parameter?

孤街浪徒 提交于 2019-11-26 03:07:28
问题 Are these the same: int foo(bar* p) { return p->someInt(); } and int foo(bar& r) { return r.someInt(); } Ignore the null pointer potential. Are these two functions functionally identical no matter if someInt() is virtual or if they are passed a bar or a subclass of bar ? Does this slice anything: bar& ref = *ptr_to_bar; 回答1: C++ references are intentionally not specified in the standard to be implemented using pointers. A reference is more like a "synonym" to a variable than a pointer to it.