object-slicing

How to implement ICloneable without inviting future object-slicing

孤街浪徒 提交于 2020-08-08 05:31:06
问题 My question is about how to implement the classic ICloneable interface in such a way that it won't lead to inadvertent object-slicing when a future programmer isn't paying close attention. Here's an example of the kind of programming error I'd like to detect (preferably at compile-time): #include <stdio.h> class ICloneable { public: virtual ICloneable * clone() const = 0; }; class A : public ICloneable { public: A() {} A(const A & rhs) {} virtual ICloneable * clone() const {return new A(*this

Avoiding object slicing

雨燕双飞 提交于 2020-06-11 14:25:35
问题 So I am refreshing on C++, and honestly it's been awhile. I made a console pong game as a sort of refresher task and got some input on using polymorphism for my classes to derive from a base "GameObject" (that has some base methods for drawing objects to the screen). One of the pieces of input was (and I had subsequently asked about) was how memory worked when deriving from base classes. Since I hadn't really done much advanced C++. For instance lets say we have a base class, for now it just

Does polymorphism work in C++ without pointers / references? [duplicate]

白昼怎懂夜的黑 提交于 2020-05-11 05:59:05
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Virtual Functions Object Slicing let's consider: #include <vector> #include <iostream> using namespace std; struct A { virtual void do_it() { cout << "A" << endl; } }; struct B : public A { virtual void do_it() { cout << "B" << endl; } }; int main() { vector<A> v; v.push_back(B()); v[0].do_it(); // output is A } which function will be called? Basically is it possible to use polymorphism without pointers if no

C++ Confused by this code with polymorphism, pointers and object slicing

假如想象 提交于 2019-12-25 12:41:45
问题 I'm trying to understand how polymorphism, object slicing and pointers work in this block of code. I'm working in Visual Studio. #include <iostream> class Man { public: virtual void speak() { std::cout << "I'm a man." << "\n"; } private: }; class Soldier : public Man { public: virtual void speak() { std::cout << "I'm a soldier." << "\n"; } private: }; int main() { Man man1; Soldier soldier1; man1 = soldier1; std::cout << "Man1: "; man1.speak(); Man *man2 = new Man; Man *soldier2 = new Soldier

numpy get 2d array where last dimension is indexed according to a 2d array

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 01:47:06
问题 I did read on numpy indexing but I didn't find what I was looking for. I have a 288*384 image, where each pixel can have a labelling in [0,15]. It is stored in a 3d (288,384,16)-shaped numpy array im . With im[:,:,1] , I can for example get the image where all pixels have the label 1. I have another 2d array labelling , (288*384)-shaped, containing a label for each pixel. How do I get the image where each pixel has the corresponding pixel using some clever slicing? Using loops, that would be:

python slicing does not give key error even when the column is missing

空扰寡人 提交于 2019-12-24 11:56:11
问题 I have a pandas dataframe with 10 keys. If I try to access a column that is not present, even then it returns a NaN for this. I was expecting a KeyError. How is pandas not able to identify the missing column ? In the example below, vendor_id is a valid column in dataframe. The other column is absent from the dataset. final_feature.ix[:,['vendor_id','this column is absent']] Out[1017]: vendor_id this column is absent 0 434236 NaN type(final_feature) Out[1016]: pandas.core.frame.DataFrame EDIT

Learning C++: returning references AND getting around slicing

醉酒当歌 提交于 2019-12-21 04:04:08
问题 I'm having a devil of a time understanding references. Consider the following code: class Animal { public: virtual void makeSound() {cout << "rawr" << endl;} }; class Dog : public Animal { public: virtual void makeSound() {cout << "bark" << endl;} }; Animal* pFunc() { return new Dog(); } Animal& rFunc() { return *(new Dog()); } Animal vFunc() { return Dog(); } int main() { Animal* p = pFunc(); p->makeSound(); Animal& r1 = rFunc(); r1.makeSound(); Animal r2 = rFunc(); r2.makeSound(); Animal v

How can I make the method of child be called: virtual keyword not working?

谁都会走 提交于 2019-12-20 02:14:01
问题 The following is my code, #include<iostream> #include<string> using namespace std; class TestClass { public: virtual void test(string st1, string st2); }; class ExtendedTest: public TestClass { public: virtual void test(string st1, string st2); }; void TestClass::test(string st1, string st2="st2") { cout << st1 << endl; cout << st2 << endl; } void ExtendedTest::test(string st1, string st2="st2") { cout << "Extended: " << st1 << endl; cout << "Extended: " << st2 << endl; } void pass(TestClass

Why do virtual functions need to be passed with a pointer and not by value(of the object)?

試著忘記壹切 提交于 2019-12-18 16:53:20
问题 I think I understand the concept of virtual methods and vtables, but I don't understand why there is a difference between passing the object as a pointer(or reference) and passing it by value (which kind of scraps the vtable or something?) Why would something like this work: Material* m = new Texture; poly->setMaterial(m); // methods from Texture are called if I keep carrying the pointer around And not this?: Material m = Texture(); poly->setMaterial(m); // methods from Material are called if

Selecting last n columns and excluding last n columns in dataframe

北慕城南 提交于 2019-12-17 16:06:52
问题 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 回答1: 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