polymorphism

Polymorphic-like SQL database table structure

会有一股神秘感。 提交于 2019-12-12 21:31:27
问题 I have some tables that have different structures, but contains the same data (in the example it is name and surname). Edit: Demo structure fiddle - http://sqlfiddle.com/#!2/cce568 However now I need to create some collective table which will store this information + some more details (eg datetime, company name, position etc). This collective table can contain some entity more than once according to the context of the remaining fields. Is there some pattern how to store this collective table,

Inherited class with methods taking Child type as a parameter: wrong method being called

坚强是说给别人听的谎言 提交于 2019-12-12 19:28:54
问题 I am making a card game, and I have several scripts that control card behavior. public class BasicCardModel : Draggable { public int hitPoints; public GameObject cardObject; public static string cardName = "Basic Card"; public CardStatus cardStatus = CardStatus.None; public void copyAttributes(BasicCardModel bcm) { Debug.Log("Calling basic copy attributes"); hitPoints = bcm.hitPoints; } ... } I have several specialized cards similar to below: public class AttackCardModel : BasicCardModel {

How to return derived type?

ぃ、小莉子 提交于 2019-12-12 18:27:18
问题 I have a Validator class and derived classes from it; When i'm trying to return pointer to derived class then method return base class(Validator) instead of Derived. class Validator { public: std::string m_name = "BaseValidator"; static const std::map<std::string, Validator *> validators(); static Validator *getByName(std::string &name); }; const std::map<std::string, Validator*> Validator::validators() { std::map<std::string, Validator*> result; //RequiredValidator is derived result[

C#/.NET 3.5: Casting objects that extend generic lists

≯℡__Kan透↙ 提交于 2019-12-12 16:36:31
问题 I'm exposing an API through a facade with methods returning interface types, and I have run into an issue with generic lists and inheritance in C#/.NET 3.5. I have two interfaces: IMyList (implements IList<IMyItem>) IMyItem And three classes: MyList (implements IMyList, extends List<MyItem>) MyOtherList (implements IMyList, extends ObservableCollection<MyItem>) MyItem (implements IMyItem) But it doesn't seem possible. How should I go about exposing only what is necessary, but still call the

C multiple types function

与世无争的帅哥 提交于 2019-12-12 16:14:21
问题 I'd like to write some functions in C but they have to be available for all numeric types (int, float, double). What is good practise? Use pointer on void (and pointer to function of course)? Or write a different function for every type? For example: float func(float a, float b) { return a+b; } 回答1: If you can use C11, _Generic can help: #include <stdio.h> int ifunc(int a, int b) { return a+b; } float ffunc(float a, float b) { return a+b; } double dfunc(double a, double b) { return a+b; }

How can I make a class that type-erases objects until a function is called on them without specifying the list of possible functions up front?

半世苍凉 提交于 2019-12-12 16:03:32
问题 Background The title probably sounds confusing, so let me explain. First of all, here is a minimal version of my implementation, so you can follow along with the concepts more easily. If you've seen some of Sean Parent's talks, you'll know he came up with a way to abstract polymorphism, allowing code such as this: std::vector<Drawable> figures{Circle{}, Square{}}; for (auto &&figure : figures) {draw(figure);} Notice that there are no pointers or anything. Calling draw on a Drawable will call

Best practice for enforcing type safety in polymorphic inheritance hierarchies [closed]

大憨熊 提交于 2019-12-12 15:49:32
问题 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 last year . I seem to run into this situation quite a lot and have yet to find a solution that I find acceptable. Quite often I will have parallel inheritance hierarchies where a method in one hierarchy gets passed the matching class from the other hierarchy as a parameter. Here is an

Polymorphism on extension functions in Kotlin

 ̄綄美尐妖づ 提交于 2019-12-12 15:42:10
问题 I have a several classes I don't control , upon which I've already created several identically-named extension methods across several common "attributes". The identically-named extension functions always return the same value type, though calculated in different ways for each type of receiver. Here is a simplified example based on built-in types for just one attribute: // **DOES NOT COMPILE** // three sample classes I don't control extended for .len inline val String.len get() = length inline

How can I cast an Interface as its type in c#?

对着背影说爱祢 提交于 2019-12-12 15:18:05
问题 I have a property that returns an interface. During debugging I can break on what was returned and while it is the interface, Visual Studio is smart enough to know the derived type that it actually is. I assume it's using reflection or something. I'm not sure. My question is, can I have that same info available to me at runtime so I can create a variable of the appropriate type and cast the interface as that? Here is what I am saying: IPreDisplay preDisplay = cb.PreDisplay; If preDisplay is a

Accessing derived class-only methods using base class object

♀尐吖头ヾ 提交于 2019-12-12 13:49:20
问题 I am new to OO programming and I was wondering how to solve the following question - I have the following: class A { public: A() {}; ~A() {}; virtual functionA() = 0; } class B: public A { public: B() {}; ~B() {}; functionA(); functionB(); } Is there a way to access functionB using an object of the base class A? If no, why is it not possible? If yes, how does one do it? I tried to find this answer on this site but I didn't find anything concrete. 回答1: I would modify your example slightly to