polymorphism

Virtual destructor in polymorphic classes

家住魔仙堡 提交于 2020-01-03 18:04:59
问题 I understand that whenever you have a polymorphic base class, the base class should define a virtual destructor. So that when a base-class pointer to a derived-class object is deleted, it will call the destructor of the derived class first. Correct me if i am wrong here. also, if the base-class destructor were to be non-virtual, it would be undefined behavior to delete a baseclass pointer to a derived object. Correct me if i am wrong aswell. so my question is: Why is it exactly, that when the

Virtual destructor in polymorphic classes

為{幸葍}努か 提交于 2020-01-03 18:04:13
问题 I understand that whenever you have a polymorphic base class, the base class should define a virtual destructor. So that when a base-class pointer to a derived-class object is deleted, it will call the destructor of the derived class first. Correct me if i am wrong here. also, if the base-class destructor were to be non-virtual, it would be undefined behavior to delete a baseclass pointer to a derived object. Correct me if i am wrong aswell. so my question is: Why is it exactly, that when the

memory leak when deleting derived class with base-class pointer

杀马特。学长 韩版系。学妹 提交于 2020-01-03 17:21:32
问题 I have an issue with a memory leak. I have a base-class pointer. From it, I use new to allocate different derived classes. Then, when I try to delete those classes with the reference (not typecasted), I get a memory leak. I researched the problem and found that I should add a virtual destructor to the base-class, but I tried this and I still have a memory leak; that is, according to my task-manager, the memory usage continues to rise with each allocation and deletion of the derived class

Polymorphic copy in Java

大憨熊 提交于 2020-01-03 13:20:09
问题 I've suddenly encountered a problem of making a deep polymorphic copy in Java. Implementing Clonable solves the problem in my case, but it is often referred as a "bad" technique. So, here are my attempts to find a "no-Clonable" solution: public class Parent { int x; public Parent() {} public Parent(int x0) { x = x0; } public Parent copy() { Parent b = new Parent(); b.assign(this); return b; } protected void assign(Parent c) { x = c.x; } @Override public String toString() { return getClass()

C# numeric base class [duplicate]

岁酱吖の 提交于 2020-01-03 09:01:47
问题 This question already has answers here : Is there a constraint that restricts my generic method to numeric types? (19 answers) Closed 5 years ago . I want to write a C# method that can accept any number. Something like: public static T Sum(T a, T b) where T : number { // (not real code) return a + b; } But I don't see a "number" base class in C#, as exists in most other languages I've used. The numeric value types are IComparable, IFormattable, IConvertible, IComparable, and IEquatable, but

Can a factory somehow determine all possible classes, by looking in a particular file?

余生长醉 提交于 2020-01-03 05:43:04
问题 Is it possible to create a Factory that generates all derived classes (and class names) at runtime, based on all the classes that are specified in a particular file? For example, given a specific file where users are constantly adding derived classes: class Laptop: public Computer { public: virtual void Run(){mHibernating = false;} virtual void Stop(){mHibernating = true;} private: bool mHibernating; // Whether or not the machine is hibernating }; class Desktop: public Computer { public:

Is List<Dog> a subclass of List<Animal>? Why are Java generics not implicitly polymorphic?

荒凉一梦 提交于 2020-01-03 05:19:13
问题 I'm a bit confused about how Java generics handle inheritance / polymorphism. Assume the following hierarchy - Animal (Parent) Dog - Cat (Children) So suppose I have a method doSomething(List<Animal> animals) . By all the rules of inheritance and polymorphism, I would assume that a List<Dog> is a List<Animal> and a List<Cat> is a List<Animal> - and so either one could be passed to this method. Not so. If I want to achieve this behavior, I have to explicitly tell the method to accept a list of

Dynamically define returning row types based on a passed given table in plpgsql?

冷暖自知 提交于 2020-01-03 03:32:06
问题 I'm dynamically building a query in a plpgsql function that accepts a source table as an incoming variable. I want to return the results of the built SELECT statement, which performs aggregations on the given table, and returns results of that table. However, at the moment I'm getting the following error: ********** Error ********** ERROR: a column definition list is required for functions returning "record" SQL state: 42601 So it looks like I need to define column types of the record rows I

c++:error C2440: '<function-style-cast>' : cannot convert from 'A<TYPE>' to 'B<TYPE>'

给你一囗甜甜゛ 提交于 2020-01-03 02:41:28
问题 I have a base class A and it has a subclass B. A overrides the + operator and B overrides it as well,calls the parent's +operator, and casts the result to B. Then I am getting the error message: error C2440: '' : cannot convert from 'A' to 'B' I thought polymorphism worked in a way that should allow this to work? 回答1: In polymorphism you cannot convert A to B , you can convert B to A . B is a kind of A, but A is NOT a kind of B. For example, in the classic Shape classes. If you have a class

Use property of parent object to determine subclass when deserializing?

女生的网名这么多〃 提交于 2020-01-02 14:40:09
问题 children: [ { o kind: "t3" data: { // ExampleNodeT3 class should be used for kind == t3 + t3var1: "val1" + t3var2: true } } { o kind: "t4" data: { // ExampleNodeT4 class should be used for kind == t4 + t4var1: false + t4var2: 2346 } } ] ... etc. @JsonTypeInfo(use=Id.NAME, property="kind") @JsonSubTypes({ @Type(value=ExampleNodeT3.class, name="t3"), @Type(value=ExampleNodeT4.class, name="t4")}) public abstract class ExampleNode { ... public void setData(ExampleNode data) { this.data = data; }