polymorphism

Make a copy of an unknown concrete type in c++

旧城冷巷雨未停 提交于 2019-12-23 10:40:17
问题 Suppose we have the following class hierarchy: class Base { ... }; class Derived1 : public Base { ... }; class Derived2 : public Base { ... }; Given a Base* which could point to either a Derived1 or Derived2 object how can I make a copy of the actual object given that it's concrete type is unknown. I thought of defining copy constructors but I don't think this is possible without knowing the actual types involved. The only solution I can think of is defining a clone() method on each type in

Make a copy of an unknown concrete type in c++

狂风中的少年 提交于 2019-12-23 10:40:02
问题 Suppose we have the following class hierarchy: class Base { ... }; class Derived1 : public Base { ... }; class Derived2 : public Base { ... }; Given a Base* which could point to either a Derived1 or Derived2 object how can I make a copy of the actual object given that it's concrete type is unknown. I thought of defining copy constructors but I don't think this is possible without knowing the actual types involved. The only solution I can think of is defining a clone() method on each type in

Maintain sub type information while serializing java objects using Jackson, without using wrapper class

a 夏天 提交于 2019-12-23 10:37:26
问题 I am trying to convert between a JSON file and an abstract class with two subclasses in Java using Jackson. Ideally, I would like to use a JSON as the following: Json document without wrapper [ { "type" : "lion", "name" : "Simba", "endangered" : true, "action" : "running" }, { "type" : "elephant", "name" : "Dumbo", "endangered" : false, "table" : [ 1.0, 2.0, 3.0 ] } ] I have annotated the Animal abstract class as shown on http://www.studytrails.com/java/json/java-jackson-Serialization

How to cleanly deal with different behavior based on polymorphism

拜拜、爱过 提交于 2019-12-23 10:02:02
问题 Suppose I have an interface IFoo with implementation classes VideoFoo , AudioFoo , and TextFoo . Suppose further that I cannot modify any of that code. Suppose that I would then like to write a function that acts differently based on the runtime type of IFoo , such as Public Class Bar Public Shared Sub Fix(ByVal Foo as IFoo) If TypeOf Foo Is VideoFoo Then DoBar1() If TypeOf Foo Is AudioFoo Then DoBar2() If TypeOf Foo Is TextFoo Then DoBar3() End Sub End Class I would like to refactor this to

What are the best ways to compare the contents of two list-like objects?

橙三吉。 提交于 2019-12-23 09:38:18
问题 When I have to compare the contents of two array-like objects -- for instance list s, tuple s or collection.deque s -- without regard for the type of the objects, I use list(an_arrayish) == list(another_arrayish) Is there any more idiomatic/faster/better way to achieve this? 回答1: Compare it elementwise: def compare(a,b): if len(a) != len(b): return False return all(i == j for i,j in itertools.izip(a,b)) For Python 3.x, use zip instead 回答2: Tuples appear to be faster: tuple(an_arrayish) ==

Can I write I Julia method that works “whenever possible” like a c++ template function?

ぃ、小莉子 提交于 2019-12-23 09:29:31
问题 rand works with ranges: rand(1:10) I'd like to make rand work with Array , and anything that is indexable and has length : import Base.Random rand(thing) = thing[rand(1:length(thing))] array = {1, 2, 3} myRand(array) range = 1:8 myRand(range) tupple = (1, 2, 3, "a", "b", "c") myRand(tupple) … but if I try this, my implementation stack overflows, presumably because it is completely general and matches everything passed, so it ends up calling itself? Is there a way to fix this? I want to better

C++ alternatives to void* pointers (that isn't templates)

笑着哭i 提交于 2019-12-23 08:36:41
问题 It looks like I had a fundamental misunderstanding about C++ :< I like the polymorphic container solution. Thank you SO, for bringing that to my attention :) So, we have a need to create a relatively generic container type object. It also happens to encapsulate some business related logic. However, we need to store essentially arbitrary data in this container - everything from primitive data types to complex classes. Thus, one would immediately jump to the idea of a template class and be done

c++ virtual keyword vs overriding function

ε祈祈猫儿з 提交于 2019-12-23 07:58:15
问题 I am learning c++ and am learning about the virtual keyword. I have scoured the internet trying to understand it to no avail. I went into my editor and did the following experiment, expecting it to print out the base message twice (because I was under the impression that the virtual keyword is needed to override functions). However, it printed out two different messages. Can someone explain to me why we need the virtual keyword if we can simply override functions and still seemingly get

c++ cast vector<Inherited*> to vector<abstract*>

荒凉一梦 提交于 2019-12-23 07:47:03
问题 class Interface{}; class Foo: public Interface{}; class Bar{ public: vector<Interface*> getStuff(); private: vector<Foo*> stuff; }; How do I implement the function getStuff() ? 回答1: vector<Interface*> result(stuff.begin(), stuff.end()); return result; 回答2: std::vector<Inherited*> and std::vector<abstract*> are different, and pretty much unrelated, types. You cannot cast from one to the other. But you can std::copy or use iterator range constructor as @Grozz says. Edit: Answering your question

Is extending a base class with non-virtual destructor dangerous?

£可爱£侵袭症+ 提交于 2019-12-23 07:04:54
问题 In the following code: class A { }; class B : public A { }; class C : public A { int x; }; int main (int argc, char** argv) { A* b = new B(); A* c = new C(); //in both cases, only ~A() is called, not ~B() or ~C() delete b; //is this ok? delete c; //does this line leak memory? return 0; } When calling delete on a class with a non-virtual destructor with member functions (like class C), can the memory allocator tell what the proper size of the object is? If not, is memory leaked? Secondly, if