polymorphism

At runtime, how does Swift know which implementation to use?

强颜欢笑 提交于 2020-01-13 13:07:08
问题 protocol A { func f() } struct S1 : A { func f() { print("S1") } } struct S2 : A { func f() { print("S2") } } let array: [A] = [S1(), S2()] for s: A in array { s.f() } // "S1\n" "S2\n" If this was an inheritance hierarchy, I would expect Swift to use a v-table to look up the correct implementation. However, the concrete types in array could be anything that implements A , along with any number of other protocols, so how would the Swift runtime know the structure of the object if it was also

At runtime, how does Swift know which implementation to use?

风格不统一 提交于 2020-01-13 13:07:03
问题 protocol A { func f() } struct S1 : A { func f() { print("S1") } } struct S2 : A { func f() { print("S2") } } let array: [A] = [S1(), S2()] for s: A in array { s.f() } // "S1\n" "S2\n" If this was an inheritance hierarchy, I would expect Swift to use a v-table to look up the correct implementation. However, the concrete types in array could be anything that implements A , along with any number of other protocols, so how would the Swift runtime know the structure of the object if it was also

What are the suggested practices for function polymorphism in R?

我怕爱的太早我们不能终老 提交于 2020-01-12 14:08:18
问题 Suppose I want to write a function in R which is a function of a couple of sufficient statistics on some data. For example, suppose the function, call it foo.func depends only on the sample mean of a sample of data. For convenience, I think users might like to pass to foo.func the sample of random variables (in which case foo.func computes the sample mean), or the sample mean itself, which is all that foo.func needs. For reasons of efficiency, the latter is preferred if there are multiple

Validate presence of polymorphic parent

一个人想着一个人 提交于 2020-01-12 13:55:14
问题 I am developing a Rails 3.2 application with the following models: class User < ActiveRecord::Base # Associations belongs_to :authenticatable, polymorphic: true # Validations validates :authenticatable, presence: true # this is the critical line end class Physician < ActiveRecord::Base attr_accessible :user_attributes # Associations has_one :user, as: :authenticatable accepts_nested_attributes_for :user end What I am trying to do is validate whether a user always has an authenticatable parent

Using subclass type parameters in virtual functions

馋奶兔 提交于 2020-01-12 04:38:26
问题 I have this piece of code (contrived from my real-life trouble) It cannot compile, complaining ExtendsB does not implement B::Run(A* a) . However, it has no problems understanding the extension to A* Run(); class A { }; class ExtendsA : public A { }; class B { public: virtual ~B(){} virtual void Run(A* a) = 0; virtual A* Run() = 0; }; class ExtendsB : public B { public: virtual ~ExtendsB(){} // Not OK! It does not see it as an implementation of // virtual void Run(A* a) = 0; virtual void Run

Hibernate polymorphism

喜欢而已 提交于 2020-01-12 01:47:28
问题 This is a Hibnerate polymorphism question and a data model design question; they are intertwingled. I've used Hibernate in the past, and have enjoyed it, but sometimes I find it difficult to think about anything but trivial designs. Not a knock on Hibernate; just an observation that ORM in general can be challenging. I think this is a Hibernate 101 question, but I am not sure. What I am trying to achieve may not even be possible. I have an abstract class Fruit that will be subclassed into

What's wrong with this deserialization from a JSON file to objects using Gson?

那年仲夏 提交于 2020-01-11 14:38:07
问题 So I have the following super class: public class Location { private String name; private double[] coordinates; private String description; } And the following subclasses: public class Monument extends Location { private String architect; private short inauguration; } public class Restaurant extends Location { private String[] characteristics; } public class Hotel extends Location { private short stars; } The JSON file I'm trying to deserialize is an array of Locations where each location can

Designing hiearchical classes with template function

≯℡__Kan透↙ 提交于 2020-01-11 13:40:11
问题 I am writing a class Base which has a member function taking a template parameter: class Base { template<class T> void func(const T& t) { ... } }; There's a class Derived which conceptually inherits natures of Base and has the same function func with different implementation. At first I thought of deriving Derived from Base and make func virtual, but I can't because it's template. I also thought of CRTP, but it's an option because instances must be able to put into a container and be

Virtual functions and polymorphism

房东的猫 提交于 2020-01-11 12:13:50
问题 Suppose I have this: class A { public: virtual int hello(A a); }; class B : public A { public: int hello(B b){ bla bla }; }; So, A it's an abstract class. 1)In the class B, I'm defining a method that its suppose overrides the A class. But the parameter it's slightly different. I'm not sure about this, is this correct? Maybe because of polymorphism, this is ok but its rather confusing. 2) If I do: A a = new B;, and then a.hello(lol); if "lol" it's not of type B, then it would give compile

Attempting to learn polymorphism, etc. in VB6, but my code doesn't do what I want it to

两盒软妹~` 提交于 2020-01-11 11:09:29
问题 Here's what I've got on a command button; it's just creating variables and attempting to output their ID (which should be an instance variable inherited from the base class.) Private Sub Command1_Click() Dim ball1 As Ball, ball2 As Ball Dim cube1 As Cube, cube2 As Cube Set ball1 = New Ball Set cube1 = New Cube Set cube2 = New Cube Set ball2 = New Ball MsgBoxTheID (ball1) 'errors; should be 0 MsgBoxTheID (ball2) 'errors; should be 3 MsgBoxTheID (cube1) 'errors; should be 1 MsgBoxTheID (cube2)