polymorphism

saving and retrieving from EAV table structure using polymorphism

I have an attribute table like the following Attributes attributeid backendtype code displayname 1 int size Product Size 2 text description Product Description i then have the product attribute tables ProductAttributes_ints id attributeid productid ...

Polymorphic STL compare function (class cmp class, class cmp int) for sorting

I'm implementing a game. I have a state tree and a set<> based priority queue that sorts the states on their costs. For this I have the < operator implemented as: struct DereferenceCompareState : public std::binary_function<State*, State*, bool> { bool operator()(const State* lhs, const State* rhs) const { verbose6("comparing ...

are reference and pointer equal with regards to polymorphism

I always think of having to use pointers for polymorphism. Using the canonical example: DrawEngine:render(Shape *shape) { shape->draw(); shape->visible(true); } And passing in pointer to various Shape derived classes. Does it work the same with references DrawEngine:render(Shape &shape) { shape.draw(); shape.visible...

Is this true about late binding ?

I read in "Thinking in java" in chapter "Polymorphism" about the concept of "Late binding" , i just wanna know if my understanding of this concept is true Procedural languages know where is the function to execute before run-time for instance if(condition){func1();}else{func2();} So the address of each possible function is known ex...

Polymorphism in objective C

I guess there is no operator overloading in objective C. Exact function overloading as in C++ is not present. In what way polymorphism is implemented in objective C? ...

C# Polymorphism

Hi, What's the difference between run-time polymorphism and compile-time polymorphism? Also, what's the difference between early binding and late binding? Examples would be highly appreciated. ...

C++ fill array with objects derived from array type

In C++ I have an array of pointers to Player objects and want to fill it with Fickle objects where Fickle is a class that is derived from Player. This is because I want a general Player array that I can fill with different objects from different classes that all are derived from the Player class. How can I do this? I create an array o...

In Java, can I consolidate two similar functions where uses JspWriter and the other PrintWriter?

I have the following class, which as you will see has rather a rather redundant formatNameAndAddress method: package hu.flux.helper; import java.io.PrintWriter; import javax.servlet.jsp.JspWriter; // A holder for formatting data public class NameAndAddress { public String firstName; public String middleName; public String lastNam...

Map polymorphic type strings to different class names

I'm thinking about creating a Rails 3 app that will integrate with an existing PHP app, using the same database. The trouble is that the PHP app uses polymorphic associations of sorts, placing references to the PHP class names like MyPHPAppPrefix_User in the owner_type column. (Odd prefix stuff, yes, but what's done is done.) To reprodu...

Casting to base class validity

Say I have a class named Base and a class that derives from it called SuperBase. Given that add takes in a Base*, would either of these be valid: SuperBase *super = new SuperBase; bases.add(super); Or SuperBase *super = new SuperBase; bases.add((Base*)super); ...

Is there any reason that Java uses late/static binding for overloaded methods in the same class?

Is there any specific reason why Java uses early binding for overloaded methods? Wouldn't it be possible to use late binding for this? Example: public class SomeClass { public void doSomething(Integer i) { System.out.println("INTEGER"); } public void doSomething(Object o) { System.out.println("OBJECT"); ...

C++ simple polymorphism issue

Ok I admit it, I'm a total C++ noob. I was checking the book Data Structures and algorithms in C++ by Adam Drozdek, in the section 1.5 : "Polymorphism" he proposes the next example: class Class1 { public: virtual void f() { cout << "Function f() in Class1" << endl; } void g() { cout << "Function g()...

Transform a list of pointers to base class

I have a design where I have a std::list of base pointers that I'd like to transform into a parallel list that adds behavior. The problem I'm having is that the object that I'm trying to use to do the transform doesnt know what the actual types are when it is invoked. It's quite possible that I'm missing something subtle and that there ...

Interface implementation overrides, etc.

Here's the simplest form of my question: IApple requires, among other things, property Flavor IToffeeApple also requires property Flavor The problem is, I want IToffeeApple to implement IApple (public interface IToffeeApple : IApple), but they both have the same property requirement. This becomes a problem when, for 1 purpose I need a...

Why is std::type_info polymorphic?

Is there a reason why std::type_info is specified to be polymorphic? The destructor is specified to be virtual (and there's a comment to the effect of "so that it's polymorphic" in The Design and Evolution of C++). I can't really see a compelling reason why. I don't have any specific use case, I was just wondering if there ever was a rat...

c++ cast vector<Inherited*> to vector<abstract*>

Hi! class Interface{}; class Foo: public Interface{}; class Bar{ public: vector<Interface*> getStuff(); private: vector<Foo*> stuff; }; how Do I implement the funtion getStuff() ? hope the question is clear.. Thanks! ...

How can I create and use a partial stub (in MoQ) without being tied to the concrete implimentation?

I have code that uses MoQ to create a partial stub. I'd prefer to interact with the interface instead of the concrete implementation so that I won't have to modify the unit test if I have a different implementation of the interface. So for example, I have a factory method such as: private Mock<ISomeInterface> ISomeInterfaceStubFactory...

.net: Please help me understand why this is completely legal in .net?

Suppose we have declared these two classes: public class Animal { //..... } public class Dog : Animal { //..... } Well, my question is: why below line of code is valid? Animal animal = new Dog(); EDIT: In e-book, "Professional C# 2008", there is a paragraph that says: It's always safe to store a derived type within a b...

upcast at runtime. (Morph from Base Class to derive Class)

class B{ private: int a; } class D: public B{ private: int b; } B* b = new B; Now for some reason I want turn b into a D* Type of Object. e.g. retain the information of B and become D with extra Informations required. What I am currently thinking of is. static_cast to do the upcasting. the additional attributes will be se...

Sharing model for polymorphic associations

Assume a polymorphic association, say 'business' and 'staff', both of which are 'hourable' (meaning they have hours assigned to them). What's the recommended approach to have the 'hour' model performs the same methods on the hours of either a business object or a staff object? For a simple example, the 'hour' model might contain: def...