object-slicing

Is legal use initializer_list to initialize an object with derived types?

家住魔仙堡 提交于 2019-12-14 04:08:49
问题 Well, maybe from the title is not clear what I'm actually asking. I have a class with an initializer-list constructor std::initializer_list<B> . Is legal initialize it with an initializer list of objects of class D , where D is derived from B ? #include <initializer_list> struct B { B(int) {} }; struct D: public B { D(int s): B(s) {} }; struct Foo { Foo(std::initializer_list<B> l) {} }; void main() { Foo f{ D{ 1 }, D{ 2 } }; } If is not legal, is that ill-formed? or just undefined behavior? I

Idiomatic way to prevent slicing?

六月ゝ 毕业季﹏ 提交于 2019-12-12 07:31:55
问题 Sometimes it can be an annoyance that c++ defaults to allow slicing. For example struct foo { int a; }; struct bar : foo { int b; }; int main() { bar x{1,2}; foo y = x; // <- I dont want this to compile! } This compiles and runs as expected! Though, what if I dont want to enable slicing? What is the idiomatic way to write foo such that one cannot slice instances of any derived class? 回答1: I'm not sure if there is a named idiom for it but you can add a deleted function to the overload set that

Python sum values in list of tuples up to certain values

≡放荡痞女 提交于 2019-12-11 17:24:41
问题 NOTE: I edited the question! I am having trouble with iteration in Python, especially when I would like to sum up values up to a certain number. Here's more information on the problem I'm facing: I have a list of tuples that looks like this: [(1, 0.5, 'min'), (2, 3, 'NA'), (3, 6, 'NA'), (4, 40, 'NA'), (5, 90, 'NA'), (6, 130.8, 'max'), (7, 129, 'NA'), (8, 111, 'NA'), (9, 8, 'NA'), (10, 9, 'NA'), (11, 0.01, 'min'), (12, 9, 'NA'), (13, 40, 'NA'), (14, 90, 'NA'), (15, 130.1, 'max'), (16, 112, 'NA

Collection of objects of more than one type

蓝咒 提交于 2019-12-11 03:09:48
问题 Is there any non-awful way to have a collection of objects of more than one type? I'm perfectly happy to derive each type from a common base. I need sensible semantics so the collection can be copied, assigned, and so on. Obviously, I can't just use a vector or list of the base class. Objects will be sliced and copying won't work at all. Using vectors or lists of pointers or smart pointers works, but you don't get sane copy semantics. To get sane copy semantics, you need to use something like

c++ call to base class method slices object

二次信任 提交于 2019-12-11 01:59:35
问题 I have something like this: #include <iostream> class X; class A { public: virtual void bar(X &x); }; class B : public A { public: }; class X { public: void foo(A &a) { std::cout << "foo A" << std::endl; } void foo(B &b) { std::cout << "foo B" << std::endl; } }; void A::bar(X &x) { x.foo(*this); } int main(int argc, char **argv) { X x; B b; b.bar(x); return 0; } Compile it and execute it, you'll have: # ./a.out foo A # I believe this is because object is sliced when cast to A. How can I avoid

Is it possible to implement a copyable_unique_ptr that is not affected by slicing?

梦想的初衷 提交于 2019-12-10 22:52:14
问题 Regardless of the fact that copying a unique_ptr makes sense or not*, I tried to implement this kind of class, simply wrapping a std::unique_ptr , and got into difficulty exactly where the copy is taken, in the case of a smart pointer to base and the stored object being a derived class. A naive implementation of the copy constructor can be found all over the internet ( data is the wrapped std::unique_ptr ): copyable_unique_ptr::copyable_unique_ptr(const copyable_unique_ptr& other) : data(std:

C# vs C++ - Types, inheritance and vtable

可紊 提交于 2019-12-10 20:00:06
问题 I'm having trouble understanding what causes this difference between C++ and C#. First we have an example in which the base class contains a virtual function. class Base { protected: int super; public: virtual int f() = 0; }; class Derived : public Base { public: int extraA; int f(){ return 1; } }; int main() { Derived *d = new Derived(); std::vector<Base*> v; v.push_back(d); for(int i=0; i < v.size() ;i++) { // Output "Derived" std::cout << typeid(*v[i]).name() << std::endl; } return 0; }

C++ slicing in Java / C# [duplicate]

本秂侑毒 提交于 2019-12-07 10:55:46
问题 This question already has answers here : Closed 10 years ago . Can C++ slicing apply to other languages too, like Java/C#? 回答1: Slicing means that if you assign a subclass instance to a superclass variable, the extra information contained by subclass is "sliced" off, because the superclass variable doesn't have the extra space to store this extra information of the subclass. This doesn't happen in Java nor with C#, because all object variables are references; when you assign a subclass

C++ slicing in Java / C# [duplicate]

落爺英雄遲暮 提交于 2019-12-05 12:04:21
Can C++ slicing apply to other languages too, like Java/C#? Slicing means that if you assign a subclass instance to a superclass variable, the extra information contained by subclass is "sliced" off, because the superclass variable doesn't have the extra space to store this extra information of the subclass. This doesn't happen in Java nor with C#, because all object variables are references; when you assign a subclass instance to a superclass variable, you actually just copy the reference; the subclass object itself remains intact. 来源: https://stackoverflow.com/questions/536267/c-slicing-in

Learning C++: returning references AND getting around slicing

断了今生、忘了曾经 提交于 2019-12-03 11:47:41
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 = vFunc(); v.makeSound(); } And the results are: "bark bark rawr rawr". In a Java way of thinking,