polymorphism

RoR: has_one “or the other”? (Or, polymorphism without the inheritance.)

半世苍凉 提交于 2019-12-20 21:26:08
问题 Hey all, I have something of an interesting requirement for my project. I need a has_one relationship where it is either one class or the other, but without inheritance. I could get away with inheritance if it is the only way, but the two associate records have completely different data and aren't related at all. What I need to figure out is something like the following. # 1. Foo never belongs to anything. # 2. Foo MUST have one assigned sub-record for validity. # 3. Foo can only have either

RoR: has_one “or the other”? (Or, polymorphism without the inheritance.)

拟墨画扇 提交于 2019-12-20 21:24:02
问题 Hey all, I have something of an interesting requirement for my project. I need a has_one relationship where it is either one class or the other, but without inheritance. I could get away with inheritance if it is the only way, but the two associate records have completely different data and aren't related at all. What I need to figure out is something like the following. # 1. Foo never belongs to anything. # 2. Foo MUST have one assigned sub-record for validity. # 3. Foo can only have either

RoR: has_one “or the other”? (Or, polymorphism without the inheritance.)

折月煮酒 提交于 2019-12-20 21:23:26
问题 Hey all, I have something of an interesting requirement for my project. I need a has_one relationship where it is either one class or the other, but without inheritance. I could get away with inheritance if it is the only way, but the two associate records have completely different data and aren't related at all. What I need to figure out is something like the following. # 1. Foo never belongs to anything. # 2. Foo MUST have one assigned sub-record for validity. # 3. Foo can only have either

How to work with type hierarchies in Neo4j?

不羁岁月 提交于 2019-12-20 20:31:07
问题 Is there some way to model type hierarchies in Neo4j? If for example I want to build a class hierarchy of cars, I might have a base type of "Car" and then have sub classes that extend that, like "SportCar", etc. I'd like to be able to create instances of "SportCar", but run a query to get all "Car"s. Is this possible? If so what is the technique? I think what I'm trying to do is create a "label hierarchy" - but I just don't think that's supported in neo4j. 回答1: Paul, First, I'd recommend that

A pointer to abstract template base class?

这一生的挚爱 提交于 2019-12-20 12:35:32
问题 I cannot figure this out. I need to have an abstract template base class, which is the following: template <class T> class Dendrite { public: Dendrite() { } virtual ~Dendrite() { } virtual void Get(std::vector<T> &o) = 0; protected: std::vector<T> _data; }; Now, I derive from this which specifies exact usage of Dendrite. Now the problem. How do I create a vector of pointers to the base-class with no specific type, which I want to specify by pushing elements to it later? Something like: class

Static Polymorphism with CRTP: Using the Base Class to Call Derived Methods

Deadly 提交于 2019-12-20 10:54:22
问题 One of the main benefits of virtual in C++ is being able to use the base class (pointer or reference) to call derived methods. I'm reading up on using CRTP to implement static polymorphism, but I can't understand how to achieve what I've mentioned above using this technique, because I can't declare a function as taking type Base when this requires a template. It seems to me that what is described in the article could be achieved by simply using function overloading, so I'm sure that there

Laravel 4, Multiple Polymorphic Relations from one Model

折月煮酒 提交于 2019-12-20 10:49:07
问题 I'm trying to set up polymorphic relationships in Laravel 4 so that I can have one Image class which handles everything related to uploads, unlinks and so on, then have it used by multiple different Models. That's fine, until I get to trying to create multiple links from the same Model. For example, I currently have something like this: Models/Image.php class Image extends Eloquent { public function of() { return $this->morphTo(); } } Models/Person.php class Person extends Eloquent { public

Inheritance , method signature , method overriding and throws clause

試著忘記壹切 提交于 2019-12-20 09:56:05
问题 My Parent class is : import java.io.IOException; public class Parent { int x = 0; public int getX() throws IOException{ if(x<=0){ throw new IOException(); } return x; } } I extend this class to write a subclass Child : public class Child1 extends Parent{ public int getX(){ return x+10; } } Notice while overriding the getX method in the Child class , I have removed the throws clause from the method definition .Now it results in an anomalous behavior by the compiler which is expected : new

How to achieve dynamic polymorphism (run-time call dispatch) on unrelated types?

♀尐吖头ヾ 提交于 2019-12-20 09:37:56
问题 GOAL: I would like to achieve type-safe dynamic polymorphism (i.e. run-time dispatch of a function call) on unrelated types - i.e. on types which do not have a common base class . It seems to me that this is achievable, or at least theoretically sound. I will try to define my problem more formally. PROBLEM DEFINITION: Given the following: two or more unrelated types A1, ..., An , each of which has a method called f , possibly with different signatures, but with the same return type R ; and a

Polymorphic objects on the stack?

╄→гoц情女王★ 提交于 2019-12-20 09:14:23
问题 In a previous question I quoted Stroustrup on why a common Object class for all classes is problematic in c++. In that quote there is the statement: Using a universal base class implies cost: Objects must be heap-allocated to be polymorphic; I really didn't look twice at it, and since its on Bjarnes home page I would suppose a lot of eyes have scanned that sentence and reported any misstatements. A commenter however pointed out that this is probably not the case, and in retrospect I can't