polymorphism

Namespace Clashing in C++

I cannot understand why this piece of code does not compile: namespace A { class F {}; // line 2 class H : public F {}; } namespace B { void F(A::H x); // line 7 void G(A::H x) { F(x); // line 9 } } I am using gcc 4.3.3, and the error is: s3.cpp: I...

nhibernate Polymorphic association

I have a problem width Polymorphic association in nhibernate. DocumentLocation Mapping : <class name="Document.DocumentLocation" table="DocumentLocation"> <id name="DocumentLocationId" type="Int32" unsaved-value="null"> <column name="DocumentLocationId" length="4" sql-type="int" not-null="true" unique="true" index="PK_Documen...

c++ standard practice: virtual interface classes vs. templates

I have to make a decision regarding generalization vs polymorphism. Well the scenario is standard: I want to make my monolithic interdependent code to be more modular, clean and extensible. It is still in a stage where the change of design principle is doable, and, as I look at it, highly desirable. Will I introduce purely virtual base...

C++ pure virtual class question

I'm attempting to write a simple B+tree implementation (very early stages). I've got a virtual class with a few functions. Needless to say, I'm very new to these strategies and am running into all sorts of problems. I'm attempting to create a root node within the BTree class. The root node will be a BBranch, which should inherit from...

Boost: How to deserialize inside a method?

I have a problem deserializing data serialized using Boost.Serialization. I want a method like this DataObject* Transmitter::read(). DataObject is the parent class of multiple classes that can be send using Transmitter::write(DataObject& data). What I have at the moment looks like this, but it doesn't work. DataObject* Transmitter::rea...

method hiding in c# with a valid example .why is it implemented in the framework.? what is the Real world advantage.?

Can anyone explain the actual use of method hiding in C# with a valid example ? If the method is defined using the new keyword in the derived class, then it cannot be overridden. Then it is the same as creating a fresh method (other than the one mentioned in the base class) with a different name. Is there any specific reason to use th...

method overloading and polymorphism

I'm writing a .NET web application in which administrators can customize the various data entry forms presented to their users. There are about half a dozen different field types that admins can create and customize (i.e. text, numeric, dropdown, file upload). All fields share a set of base attributes/behaviors (is the field required? ...

C# : Why doesn't 'ref' and 'out' support polymorphism?

Take the following : class A {} class B : A {} class C { C() { var b = new B(); Foo(b); Foo2(ref b); // <= Compile time error : // 'The 'ref' argument doesn't match the parameter type } void Foo(A a) {} void Foo2(ref A a) {} } Why does the above compile time error occ...

C# basic Polymorphism question

I'm not sure it's really a "polymorphism" question but anyway... So I have an abstract class and 2 other classes that inherit from it. Some code from the base class have a variable that would be different between classes that extends the base class. What's the best way to do that? Override a "get" method? Change the architecture a litt...

Creating Core Foundation classes

Since I can't seem to find any documentation on this subject, is it possible to create your own Core Foundation "class"? (classes as in ones that can be used with CFRetain() and CFRelease) I want to take advantage of the polymorphic capabilities and object inspection built into Core Foundation without the overhead of Objective-C or creat...

vtables for derived, concrete, classes

If I have one base class and I derive 10 different concrete derived classes from it then will each and every concrete derived class have a different vtable? ...

Field belongs to a class, but how to use polymorphism

I have a method in a base class class Base { private static string Colour = "blue"; string DoStuff() { return ColourProp; } protected virtual string ColourProp { get{ return Base.Colour; } } } that is called on an instance of this subclass class Sub { private static string Colour = "orange"; protected override string...

What's really happening with new and override under the covers?

I've found loads of practical examples of this, and understand the practical output when overriding or hiding methods, but I'm looking for some under the covers info on why this is and why C# allows it when according to the rules of polymorphism, this shouldn't be allowed - at least, insofar as my understanding of polymorphism goes (whic...

Avoiding instanceof when checking a message type

I have the following situation where a client class executes different behavior based on the type of message it receives. I'm wondering if there is a better way of doing this since I don't like the instanceof and the if statements. One thing I thought of doing was pulling the methods out of the client class and putting them into the mes...

Polymorphically convert Java enum values into a list of strings

I have a handful of helper methods that convert enum values into a list of strings suitable for display by an HTML <select> element. I was wondering if it's possible to refactor these into a single polymorphic method. This is an example of one of my existing methods: /** * Gets the list of available colours. * * @return the list o...

Rails Polymorphism 'backwards'

Say I'm writing a blog app with models for posts, pages and photos. I've got a category model, that may be linked to any of these models. So, a category may contain various kinds of items. Every item only has ONE category. I could implement this using a generic tagging pattern with a join table, but I want to make sure every subject can...

Template Polymorphism not Working?

I'm building a small template hierarchy and try to make use of class polymorphism. Here's some example code (which does not compile) to demonstrate it: template<typename T> struct A {}; template<typename T> struct B { B (A<B> *) {} }; struct C : public B<int> { C(A<C> *p) : B<int>(p) {} // error }; int main() { A<C> ac; ...

Benefit of Generic Constructors

What is the benefit of having generic constructor for a non-generic class? Java spec permits the following: class NonGeneric { <T> NonGeneric() { } ... NonGeneric ref = new <String> NonGeneric(); } Can one come up with a realistic example of when it enhances the typesafety of the class? How would it be better than using Generi...

Applying the decorator pattern polymorphically and coupling problems in C++

I am trying to make a C++ implementation of the board game Carcassonne. I am trying to make a tile object which has four sides and one of three basic terrains(field, road, city). The best interface for tile creation I could think of was of the form: City city; city_city_city_city = new Tile(city, city, city, city); Where a Tile clas...

Haskell: Algebric data types whose type variables need to be an instance of a typeclass

I am trying to define an algebric type: data MyType t = MyType t And make it an instance of Show: instance Show (MyType t) where show (MyType x) = "MyType: " ++ (show x) GHC complains becasue it cannot deduce that type 't' in 'Show (MyType t)' is actually an instance of Show, which is needed for (show x). I have no idea where an...