polymorphism

How virtual is this?

Hi, can you explain me why: int main (int argc, char * const argv[]) { Parent* p = new Child(); p->Method(); return 0; } prints "Child::Method()", and this: int main (int argc, char * const argv[]) { Parent p = *(new Child()); p.Method(); return 0; } prints "Parent::Method()"? Classes: class Parent { publ...

How to TDD functionality in a base mixin class from one of many leaf classes?

Hi guys, following on from this question (Developing to an interface with TDD), I'm still having some issues. I test-drove two classes into existence, both of which ended up shared some identical functionality. I refactored a common base class into existence, and all the tests still passed. So far, so tdd-tastic. I needed a third cla...

How can I cast an Interface as it's type in c#?

Hello Everyone, I have a property that returns an interface. During debugging I can break on what was returned and while it is the interface, Visual Studio is smart enough to know the derived type that it actually is. I assume it's using reflection or something. I'm not sure. My question is, can I have that same info avaialble to me at r...

Which .Equals() method to use

In this question. we discovered that in .NET 1.1, Array.IndexOf(array, value) searched for an element with value.Equals(arrayElement) == true while .NET 2.0 changed it to search for an element with arrayElement.Equals(value) == true Obviously the potential difference between the two results arises from polymorphism, but is there an...

Correct Implementation of Virtual Functions in PHP?

Hi guys, at my working place (php only) we have a base class for database abstraction. When you want to add a new database table to the base layer, you have to create a subclass of this base class and override some methods to define individual behaviour for using this table. The normal behaviour should stay the same. Now I have seen ma...

What is the best way to differentiate between derived classes of a base class?

I have base class BaseClass and derived classes DerivedA, DerivedB, and DerivedC that all inherit BaseClass. I have another class, ExternalClass with a method that accepts a parameter of type BaseClass, but is actually passed a derived class. What is the best way to differentiate between these classes in ExternalClass if I wanted to per...

C# Inheritance, accessing child members with base functions?

I have a class which inherits from another and I want to make use of one of the base class functions...but I am running into a problem where calling the base function does not return the member variable from the inherited class as I had hoped. To illustrate my problem, I have created a simplified example. I expected this example to outp...

Polymorphism in VB.NET via Late Binding disallows With Events, workaround?

Hello, I'm working on developing an application that talks to a family of USB sensors. I've created a basic implementation that utilizes a class called Sensor. The class contains events and methods that allow for interaction with the sensor (there is also a threaded task processor involved but I'll go with a simple example). My issue ...

C# WPF: Overriding functions in ResourceDictionary elsewhere

Basically, I have a Grid with content that I want to reuse, the only difference being the functions called by clicking the buttons. I've added it to the ResourceDictionary, the plan being that I could simply set the content of a Page to that Grid, then override the function in the codebehind to do what I want. Unfortunately, I've run int...

Should we need to differentiate between Interface class and abstract class, in terms of naming convention?

Hi, Is that important to differentiate between Abstract class and interface class? Abstract class is merely an interface class, with some concrete methods. If the abstract class shares the same prefix "I" with Interface class, we can easily upgrade our interface class to abstract class, by introducing new logic etc. UPDATE The reaso...

C# numeric base class

I want to write a C# method that can accept any number. Something like: public static T Sum(T a, T b) where T : number { // (not real code) return a + b; } But I don't see a "number" base class in C#, as exists in most other languages I've used. The numeric value types are IComparable, IFormattable, IConvertible, IComparable, a...

Constructor Polymorphism Help

I have a UserControl that has a BaseClass object as a public member. Right now I'm doing the following to discern between which type of object I need to instantiate: Public WithEvents theForm As OrderForm Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Select Case Form Case...

Java Webservice and Polymorphism

Let's say i have the following strucutre public class Mammal { @WebMethod public ArrayList<Mammal> getAll(){ return null; } @WebMethod public String speak(){ return "Unable to speak"; } @WebMethod public Mammal me(){ return this; } } @WebService public class Human extends Mammal { @WebMethod public Arra...

Error calling function and passing a reference-to-pointer with a derived type

Can somebody explain why the following code is not valid? Is it because the offset for the variable named d is different than the variable named b? class Base { public: int foo; }; class Derived : public Base { public: int bar; }; int DoSomething( Base*& b ) { return b->foo; } Base* b = new Derived; Derived* d = new Derived; int mai...

trouble making polymorphism defeat those switch/case statements

Continuing on previous questions (here, and here), I implemented a basic command pattern, created my command classes and coded to an interface, so when any command is used, to call the execute() method. However, I am still finding myself unable to shake those case statements: I am reading each character from a master/decision String, w...

refactoring and removing case statements when circling over an enum structure

Hello, An enum structure declared in its own class is a member variable to the business logic class. That enum basically represents the state of that other class. Although I have revisited the issue several times, replacing, or getting rid of those case statements proves quite frustrating to me. Several business logic methods simple i...

how can I replace instanceof in this case?

I'm trying to compare compareCriteria. Simple ones like 'between' and 'inArray' or 'greaterThan'. I use polymorphism for these classes. One method they share from the compareCriteria interface is 'matchCompareCriteria'. What I'm trying to avoid is having each class check for the type of compareCriteria they should match against. Eg the ...

Determining size of an overloaded C++ class

Using the sizeof operator, I can determine the size of any type – but how can I dynamically determine the size of a polymorphic class at runtime? For example, I have a pointer to an Animal, and I want to get the size of the actual object it points to, which will be different if it is a Cat or a Dog. Is there a simple way to do this, sh...

Polymorphism & Pointers to arrays

Hi all, I have a class A: class A { public: virtual double getValue() = 0; } And a class B: class B : public A { public: virtual double getValue() { return 0.0; } } And then in main() I do: A * var; var = new B[100]; std::cout << var[0].getValue(); //This works fine std::cout << var[1].getValue(); //This, ...

Subclassing and Casting in Objective C

I came across a strange problem today. I created a subclass of UIView and added only 1 method to the template code provided by xcode. @interface FloatView : UIView { } - (void)floatTest:(CGFloat)x; @end - (void)floatTest:(CGFloat)x { NSLog(@"float was %f", x); } Then in my appDelegate I had code like this: UIView *floatView = [[F...