polymorphism

C++ virtual function not called in subclass

Hi, Consider this simple situation: A.h class A { public: virtual void a() = 0; }; B.h #include <iostream> class B { public: virtual void b() {std::cout << "b()." << std::endl;}; }; C.h #include "A.h" #include "B.h" class C : public B, public A { public: void a() {std::cout << "a() in C." << std::endl;}; }; in...

When to use enums, and when to replace them with a class with static members?

It recently occured to me that the following (sample) enumeration... enum Color { Red, Green, Yellow, Blue } ... could be replaced with a seemingly more type-safe class: struct Color // (edited: this was previously a class) { private Color() { } public static readonly Color Red = new Color(); p...

Rails data modeling - alternatves to has_many :through, given polymorphism?

I'm working on an application that allows users to associate images with specific events. Events are owned by a user. The easy solution would of course be: class Image < ActiveRecord::Base belongs_to :event end class Event < ActiveRecord::Base has_many :images belongs_to :user end class User < ActiveRecord::Base has_many :eve...

Classes, methods, and polymorphism in Python

I made a module prototype with the aim of building complex timer schedules in python. The class prototypes emulate Timer objects, each with their waiting times, Repeat objects that group Timer and other Repeat objects, and a Schedule class, just for holding a whole construction or Timers and Repeat instances. The construction can be as c...

Understanding Tcl Polymorphism

In Tcl a variable and a procs can have the same name ... for instance I can have set container(alist) {} proc container a {puts " do something"} Um ... what other forms of polymorphism exist in tcl? ... I am looking at some code and I see stuff like this. ...

Using pointer to base class as array parameter

Hey, I have 2 classes: class Base { public: virtual int Foo(int n); virtual void Goo() = 0; virtual ~Base() ; }; class Derived : public Base { public: int Add4Bytes; void Goo(); int Foo(int n); }; int Test(Base* b) { for (int i=0;i<5;++i) { b->Foo(i); ++b; } re...

Operator== in derived class never gets called.

Can someone please put me out of my misery with this? I'm trying to figure out why a derived operator== never gets called in a loop. To simplify the example, here's my Base and Derived class: class Base { // ... snipped bool operator==( const Base& other ) const { return name_ == other.name_; } }; class Derived : public Base { // ......

C++ How to I replace this if...else statement?

I have the following C++ code (simplified version): class Shape { bool isCircle = false; bool isSquare = false; } class Circle : public Shape { // some special members/methods } class Square : public Shape { // some special members/methods } class CAD { virtual DrawCircle(Circle * circle) = 0; } class SWX : publi...

invoking method declaration without reflection

I have a base class (order) with a set of sub classes (productorder, specialorder, partsorder etc). Only Some of these sub classes implement a particular interface (ITrackingCustomer) which has a single method declaration (object getcustdetails()). As part of my solution all of my orders are processed in a central place, i.e. any cru...

Polyvariadic Functions in Haskell

After reading this article on writing polyvariadic functions in Haskell, I tried to write some of my own. At first I thought I'd try to generalize it - so I could have a function that returned variadic functions by collapsing arguments as given. {-# OPTIONS -fglasgow-exts #-} module Collapse where class Collapse a r | r -> a where co...

c++ problem with function overloading

Hello, i have a problem with function overloading. I will show you with some simple example: class A {}; class B : public A{}; void somefunction(A&, A&); void somefunction(B&, B&); void someotherfunction() { ... A& a1 = ... A& a2 = ... ... } Both a1 and a2 are instances of B but somefunction(a1,a2); calls void somefunction(A&, ...

Polymorphism by function parameter

Ok - this may be a very stupid question, but it's been bothering me. Is there a language where class Animal; class Ape : public Animal {...} void doStuff(Animal* animalPtr) { cout << "doing animal stuff" << endl; } void doStuff(Ape* apePtr) { cout << "doing ape stuff" << endl; } Animal *ape = new Ape(); doStuff(ape); would...

Why would you make a whole class sealed/final?

I understand the motivation for making individual methods of a class sealed/final, but what purpose does completely prohibiting inheritance from the class serve? While allowing overriding of certain methods can cause bad things to happen, I can't see how allowing inheritance from your class purely to add behavior to it without overridin...

How to invoke a java method based on a subclass type?

Hi, I'm trying to dispatch objects to separate method according to their subclass. For instance, consider those 2 objects class A extends I {} class B extends I {} and the method void dispatch(I i) {} in dispatch(), I'd like to invoke a method according to the type of i. Hence if i is actually of type A, the handlerA(A a) method ...

Polymorphism - adding to existing methods while overwriting them

I want to be able to subclass a class, and define __init__ but still run the old __init__ as well. To illustrate, say I have the following classes: class A(object): def __init__(self): self.var1 = 1 class B(A): def __init__(self) self.var2 = 2 doInitForA() And I want to be able to do this: instB = B(...

Private virtual method in C++

What is the advantage of making a private method virtual in C++? I have noticed this in an open source C++ project: class HTMLDocument : public Document, public CachedResourceClient { private: virtual bool childAllowed(Node*); virtual PassRefPtr<Element> createElement(const AtomicString& tagName, ExceptionCode&); }; ...

Polymorphic / Plugable PHP Classes

I have a question which is sonewhat more of a design question. I have a class which defines a bunch of functions. However, I want that the behaviour of some functions are changeable during runtime - or at least working like a plugin interface during design time. eg: class MyClass { public function doSomething(); } $obj = new MyClas...

Rails STI using ONE form

I have a form that allows me to add files of different formats to a stream. So, a stream is made up of many files, these files are XML files but basically have different schemas. I have one form that allows the user to add whatever file they want, I am using STI (which works great when data is already in the table), my trouble is adding ...

C# object initializer wanting to use wrong Add method

I have the following class hierarchy: public class Row : ICloneable, IComparable, IEquatable<Row>, IStringIndexable, IDictionary<string, string>, ICollection<KeyValuePair<string, string>>, IEnumerable<KeyValuePair<string, string>>, System.Collections.IEnumerable { } public class SpecificRow : Row, IXmlSerializable, ...

Why can't C# compiler follow all code paths through a switch statement

The below code works fine: ListControl lstMyControl; if (SomeVariable == SomeEnum.Value1) { lstMyControl = new DropDownList(); } else { lstMyControl = new RadioButtonList(); } lstMyControl.CssClass = "SomeClass"; Whereas the below code won't compile: ListControl lstMyControl; switch (SomeVariable) { case SomeE...