polymorphism

Define the method once to be virtual in the inheritance hierarchy to make polymorphism to work

限于喜欢 提交于 2019-12-24 01:25:45
问题 Is it sufficient to define the method once to be virtual in the inheritance hierarchy to make polymorphism to work. In the following example Der::f is not defined to be virtual but d2->f(); prints der2 I am using VS IDE (may be it is only there...) class Base { public: virtual void f() { std::cout << "base"; } }; class Der : public Base { public: void f() { std::cout << "der"; } //should be virtual? }; class Der2 : public Der { public: void f() { std::cout << "der2"; } }; int main() { Der* d2

Downcasting best-practice (C++)

我怕爱的太早我们不能终老 提交于 2019-12-24 01:16:08
问题 Static code analysis tools tend to ramble a lot about "downcasting a base class to a derived class" and I also found a couple of coding standard guides, which mention not to do this so I was wondering what is the best-practice way. Here's my use case: I have a Base (interface), DerivedA, DerivedB classes and then an array containing Base pointers. Then I iterate through the array and based on a flag, I determine if the object is DerivedA or DerivedB, cast it down and do some random stuff to

Java: Parent Methods accessing Subclasses' static variables?

假如想象 提交于 2019-12-24 00:59:16
问题 I am trying to understand my way around polymorphism in Java. I created a parent class that has too many common methods that all children will use in the same manner. Each of the subclasses' children all share static information, These variables or information will be used in the methods declared only in the parent. The problem wish accessing static variables from Parent methods seems not really possible, Its a solution to declare the common information per instance but since there will be

How to design a nested polymorphism in OOP?

你说的曾经没有我的故事 提交于 2019-12-24 00:12:25
问题 Let's assume we have a simple payment feature on an online shop. We want to manage different transactions with different processors of transactions: A transaction can be a payment or a refund. A processor of transactions can be Paypal or Payplug. So we have the following classes: class PaymentTransaction implements Transaction { } class RefundTransaction implements Transaction { } class PaypalProcessor implements Processor { } class PayplugProcessor implements Processor { } What should be a

Is it possible to break code by adding a new virtual function in the base class?

怎甘沉沦 提交于 2019-12-23 20:32:58
问题 Is it possible to have the observed behavior of a program changed by simply adding a new virtual function to a base class? I mean that no other change must be made to the code. 回答1: #include <stdlib.h> struct A { #if ADD_TO_BASE virtual void foo() { } #endif }; struct B : A { void foo() { } }; struct C : B { void foo() { abort(); } }; int main() { C c; B& b = c; b.foo(); } Without the virtual function the base class b.foo() is a non-virtual call to B::foo() : $ g++ virt.cc $ ./a.out With the

C# Accessing a subclass property

北慕城南 提交于 2019-12-23 20:25:57
问题 I have a few classes and I am having problems accessing properties defined in subclasses from other class methods. I have a base class called Section and a few subclasses, e.g. SectionPlane : Section . In each subclass, a different set of fields and properties are defined (in SectionPlane , private field _t and public property t can be found, whereas in SectionExtruded : Section I have private field _A and public property ´A´). Class Section // General section object public abstract class

Heterogeneous polymorphism in Haskell (correct way)

陌路散爱 提交于 2019-12-23 20:07:33
问题 Let a module to abstract Area operations (bad definition) class Area someShapeType where area :: someShapeType -> Float -- module utilities sumAreas :: Area someShapeType => [someShapeType] sumAreas = sum . map area Let a posteriori explicit shape type modules (good or acceptable definition) data Point = Point Float Float data Circle = Circle Point Float instance Surface Circle where surface (Circle _ r) = 2 * pi * r data Rectangle = Rectangle Point Point instance Surface Rectangle where

Are Laravel's Polymorphic Relations also suitable for true polymorphism?

余生长醉 提交于 2019-12-23 19:16:35
问题 To explain myself better: the examples on Laravel.com showcase a relation of comments belonging to both videos and posts . However I'm talking about specification: not belonging to, but being a specification of the parent table. (Subtyping) Is using Laravel's Polymorphic Relations still the best approach? A very basic (and potentially bad) example. 回答1: Interesting question. It is entirely possible to do what you require, with the polymorphic relation that Laravel provides. You however, are

c++ problem with function overloading

柔情痞子 提交于 2019-12-23 19:13:48
问题 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&, A&); What did i do wrong? I mean polymorphism and overloading are for stuff like that, arent they? edit: Ok now i know it does not work (thanks for your answers). Any solution

I can't assign from pointer to pointer to derived class and pointer to pointer to base class? [duplicate]

[亡魂溺海] 提交于 2019-12-23 18:34:12
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Conversion between Derived** to Base** I'm getting back into C++ after several years of mostly Python, and am hitting up against a strongly-typed wall. I think I have a good handle on basic polymorphism and type-casting between pointers of base and derived classes (e.g. Can a pointer of a derived class be type cast to the pointer of its base class?), but here's a stumper: why can't I assign a pointer to a