polymorphism

Is unique_ptr<Derived> to unique_ptr<Base> up-casting automatic?

流过昼夜 提交于 2019-12-30 08:15:41
问题 I know it is possible that a derived class unique_ptr can take place where base class unique_ptr is required for polymorphic types. For example, while returning from function unique_ptr<Base> someFunction() { return make_unique<Derived>(new Derived()); } or passing to function as argument. // Function taking unique pointer void someOtherFunction(unique_ptr<Base>&& ptr) // Code calling this function someOtherFunction(std::move(ptrToDerived)); My question is: Is this upcasting always automatic?

How does Java inheritance work when inner classes are involved

流过昼夜 提交于 2019-12-30 05:58:10
问题 I am having trouble understanding how inheritance works in Java when inner classes are present. I'm currently working on something where a child class needs to slightly change the functionality of the inner class of it's parent. I've come up with an simpler, analagous example below. I expected this code to print "I am a ChildClass.InnerClass" but instead it prints "I am a ParentClass.InnerClass". Why is this? Also, if I change the obj object in main to be of type ChildClass then the output

How do you do polymorphism in Ruby?

狂风中的少年 提交于 2019-12-30 03:58:18
问题 In C#, I can do this: class Program { static void Main(string[] args) { List<Animal> animals = new List<Animal>(); animals.Add(new Dog()); animals.Add(new Cat()); foreach (Animal a in animals) { Console.WriteLine(a.MakeNoise()); a.Sleep(); } } } public class Animal { public virtual string MakeNoise() { return String.Empty; } public void Sleep() { Console.Writeline(this.GetType().ToString() + " is sleeping."); } } public class Dog : Animal { public override string MakeNoise() { return "Woof!";

Why super keyword in generics is not allowed at class level

穿精又带淫゛_ 提交于 2019-12-30 03:09:45
问题 In Generics class A<T extends Number> is allowed But class A<T super Integer> is not allowed I'm not getting this point. This may sound like novice question but I'm stuck in it 回答1: Quoting Java Generics: extends, super and wildcards explained: The super bound is not allowed in class definition. //this code does not compile ! class Forbidden<X super Vehicle> { } Why? Because such construction doesn't make sense. For example, you can't erase the type parameter with Vehicle because the class

How do I create an array of abstract class objects in MATLAB?

强颜欢笑 提交于 2019-12-30 00:59:09
问题 As an example, suppose I have created an abstract class called Shape and two subclasses called Circle and Rectangle that both implement an (abstract) method called Draw . I would like to be able to create a number of Circle and Rectangle objects, store them in an array and call Draw on each array object by iterating through the array. I have tried something like the following: Shape.m: classdef (Abstract) Shape < handle methods (Abstract) Draw(obj); end end Circle.m: classdef Circle < Shape

Can someone explain Polymorphism to me? [duplicate]

£可爱£侵袭症+ 提交于 2019-12-30 00:34:06
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Try to describe polymorphism as easy as you can I've never been able to fully comprehend what polymorphism is. Can someone explain, perhaps with use of an example, what it is and how it works? Just the basics. 回答1: Perhaps it's easiest to start with a non-computer analogy. Consider if you told somebody to "Go to the store and buy some of your favorite food for supper." If you said this to a 14 year-old son, he'd

What is Haskell's style of polymorphism?

依然范特西╮ 提交于 2019-12-29 18:37:03
问题 With Haskell's type classes it almost seems that it enables ad hoc polymorphism, but its functions declarations seem parametric polymorphism. Am I mixing my understanding of different things? 回答1: Indeed, Haskell supports both (higher rank) parametric polymorphism, and ad hoc (or bounded ) polymorphism. Parametric polymorphism in Haskell is supported via its Hindley-Milner/System F type system. Ad hoc polymorphism is supported via type classes. For the origin of type classes and ad hoc

What are the benefits of declaring an object as interface? [duplicate]

不打扰是莪最后的温柔 提交于 2019-12-29 07:39:07
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: What does it mean to “program to an interface”? I noticed that some people like to declare an object as one of the interfaces it implements even though, within the scope of the variable, it is not necessary to look at it as the interface , e.g. there is no external API that expect an interface. For example: Map<String, Object> someMap = new HashMap<String, Object>(); Or you can just do HashMap<String, Object>

dynamic_cast across a shared_ptr?

亡梦爱人 提交于 2019-12-29 05:20:44
问题 I have two classes A and B, B inherits from A. If I have a shared_ptr<A> object which I know is really a B subtype, how can I perform a dynamic cast to access the API of B (bearing in mind my object is shared_ptr, not just A? 回答1: If you just want to call a function from B you can use one of these: std::shared_ptr<A> ap = ...; dynamic_cast<B&>(*ap).b_function(); if (B* bp = dynamic_cast<B*>(ap.get()) { ... } In case you actually want to get a std::shared_ptr<B> from the std::shared_ptr<A> ,

What is “n” in RankNTypes

守給你的承諾、 提交于 2019-12-29 05:19:24
问题 I understand how forall enables us to write polymorphic function. According to this chapter, the normal function which we generally write are Rank 1 types. And this function is of Rank 2 type: foo :: (forall a. a -> a) -> (Char,Bool) foo f = (f 'c', f True) It explains like this: In general, a rank-n type is a function that has at least one rank-(n-1) argument but no arguments of even higher rank. What does it actually mean by rank argument ? Can somebody give an example of Rank 3 type which