polymorphism

What should be done with inherited factory methods?

落花浮王杯 提交于 2019-12-22 20:04:08
问题 Suppose I have a class BasicDate , and a subclass of BasicDate called EuroDate . The difference between the classes is month-day-year versus day-month-year. I know it'd probably be better to just have methods on the same class to output them differently... but that's not the point of this question. BasicDate has the following init method : -(id)initWithMonth:(int)m andDay:(int)d andYear:(int)y { if(self = [super init]) { /*initialize*/ } return self; } And the matching factory method then

What is the best way to differentiate between derived classes of a base class?

半世苍凉 提交于 2019-12-22 18:41:29
问题 I have base class BaseClass and derived classes DerivedA , DerivedB , and DerivedC that all inherit BaseClass . I have another class, ExternalClass with a method that accepts a parameter of type BaseClass , but is actually passed a derived class. What is the best way to differentiate between these classes in ExternalClass if I wanted to perform a different action based on which derived class it received? I was thinking of doing a Select but I'm not exactly sure how. 回答1: Your design is very

What is the difference between 'a and ''a in SML?

风流意气都作罢 提交于 2019-12-22 10:58:24
问题 For example: fun example (a:'a list) : list = a will have a signatures of: 'a list -> 'a list What if I define it differently but with same content like fun example (a : ''a list) : list = a its signature will be: ''a list -> ''a list What's the difference? 回答1: A plain type variable like 'a can be substituted with an arbitrary type. The form ''a is a so-called equality type variable, which means that it can only be substituted by types that admit the use of the equality operator = (or <> )

RankNpolymorphism and kleisli arrows of outrageous fortune

醉酒当歌 提交于 2019-12-22 10:57:53
问题 I dont understand why the definition of demonbind1 yields some compiler errors. it looks like a stupid flip but somehow.. {-# LANGUAGE GADTs #-} {-# LANGUAGE RankNTypes, ScopedTypeVariables, TypeOperators, TypeFamilies,ImpredicativeTypes #-} type a :-> b = forall i . a i -> b i class IFunctor f where imap :: (a :-> b) -> (f a :-> f b) class (IFunctor m) => IMonad m where skip :: a :-> m a bind :: (a :-> m b) -> (m a :-> m b) -- Conor McBride's "demonic bind" (?>=) :: forall m a b i. (IFunctor

Passing std::vector of derived type where a std::vector of base type is expected

萝らか妹 提交于 2019-12-22 10:24:40
问题 I have a base class: class Member { .... } and a derived class, like this: class Mine_sweeper : private Member { .... } Then I have a container: class Population { public: std::vector<Member>& m_members; Population(std::vector<Member>& members); } When I use Population's constructor with a vector of Members it compiles OK. But if I try to link Population's m_members with a vector of Mine_sweepers, complier throws an error. std::vector<Member> members; Population pop(members); // this compiles

Select instance behavior at runtime

拟墨画扇 提交于 2019-12-22 09:44:51
问题 I'm stuck trying to select one instance from many at runtime. Really is a kind of Backend . I'm able to do it if I select one instance or other at compile time. UPDATED probably I want some similar to Database.Persist (it define a fully behavior but many instances: mongodb, sqlite, postgresql, ...). But is too complex to me. UPDATED using GADTs works but I think exist a better way (full code at the bottom). In some OOP language my problem is more or less interface IBehavior { void foo(); }

Polymorphism with new data members

我的未来我决定 提交于 2019-12-22 09:37:40
问题 I would like to write a function that can initialize and return objects of different classes using polymorphism. I also would like these classes to have different data members which may be called through the virtual function. What I wrote below might work. Could you check if I have some undefined behavior in there? Thank you! One thing I am worried about is that when I call "delete polypoint" at the end it will not free the data member "scale" that is unique to "CRectangle". If my code doesn

Is there any valid reason for not using public virtual methods? [closed]

寵の児 提交于 2019-12-22 09:28:08
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . Is there any valid reason for not using public virtual methods? I have read somewhere that we should avoid using public virtual methods, but I want to confirm from experts if this is valid statement? 回答1: For good and stable API design, the Non-Virtual-Interface is a good

Patterns to compensate for lack of runtime method lookup based on polymorphic args in Java?

寵の児 提交于 2019-12-22 08:36:13
问题 It seems that Java can't choose the most appropriate method implementation based on the runtime type of an argument, as documented here. Recapitulating the example: class Superclass {} class Subclass extends Superclass {} class Test { public void aMethod(Superclass s) {...} public void aMethod(Subclass s) {...} } Which method of the Test class is executed is determined based on the type of the reference, not the type of the instance. Again, based on the linked examples: Test aTest = new Test(

“Polymorphism applies to overriding, not to overloading”?

烂漫一生 提交于 2019-12-22 06:56:34
问题 I just read this line in book "Sun Certified Java Programmer" (page number-158) by Kathy Sierra and Bert Bates who worked as Master trainer at "Sun microsystems": Polymorphism applies to overriding, not to overloading. But some other books and articles say overloading is also a form of polymorphism.. i'm confused.. Please help.. 回答1: Don't believe the other books. Late binding (wiki) (with overriden/inherited methods) is polymorphism. Static binding is what applies to overloading. The two