polymorphism

c++ - converting a base class pointer to a derived class pointer

浪子不回头ぞ 提交于 2019-12-20 08:59:28
问题 #include <iostream> using namespace std; class Base { public: Base() {}; ~Base() {}; }; template<class T> class Derived: public Base { T _val; public: Derived() {} Derived(T val): _val(val) {} T raw() {return _val;} }; int main() { Base * b = new Derived<int>(1); Derived<int> * d = b; cout << d->raw() << endl; return 0; } I have some polymorphism problem right now and the code above summarizes everything. I created a Base class pointer and I put the pointer of a new derived template class in

Subtype polymorphism in Haskell

谁说胖子不能爱 提交于 2019-12-20 08:56:54
问题 Building a hierarchy of GUI widget classes is pretty much a standard exercise in object-oriented programming. You have some sort of abstract Widget class, with an abstract subclass for widgets that can contain other widgets, and then you have a profusion of further abstract classes for widgets that support textual display, widgets that support being the input focus, widgets that have a boolean state, right down to actual concrete classes such as buttons, sliders, scrollbars, check boxes, etc.

Java collections — polymorphic access to elements

痞子三分冷 提交于 2019-12-20 07:37:35
问题 I have a LinkedHashSet of values of ThisType . ThisType is implementing the interface ThatType . I need the polymorphic use of this collection-- be able to point to it as LinkedHashSet< ThatType >. How is this done? i know this is a naive question, but several things i tried didn't work, and i wanna do it the right way. Thanks in advance. //============================== UPDATE: more details: In the following code, ThisType implements ThatType -- ThatType is an interface. //LinkedHashMap

Why the Overrided function getting called first?

杀马特。学长 韩版系。学妹 提交于 2019-12-20 07:36:02
问题 I executed the following program and I am curious about the output i got in which the function output is getting printed first even if it was the variable i tried to print first. class Baap{ public int h = 4; public int getH(){ System.out.println("Baap "+h); return h; } } public class Beta extends Baap{ public int h = 44; public int getH(){ System.out.println("Beta "+h); return h; } public static void main(String args[]){ Baap b = new Beta(); System.out.println(b.h+" "+b.getH()); Beta bb =

c++ decorator pattern, static polymorphism with templates and registering callback methods

陌路散爱 提交于 2019-12-20 07:28:08
问题 I am attempting to use static polymorphism to create a decorator pattern. As to why I do not use dynamic polymorphism, please see this QA. Basically, I could not dynamic_cast to each decorator so as to access some specific functionality present only in the decorators (and not in the base class A). With static polymorphism this problem has been overcome, but now I cannot register all the et() methods from the decorators back to the base class A (as callbacks or otherwise), thus when A::et()

Use base class as parameter for WCF Service

本秂侑毒 提交于 2019-12-20 07:17:27
问题 I have a multi-project solution. One Project is providing a DLL containing multiple classes. One of those classes is WorkerTemplate . Two other classes inherit from it namely ExecSQLWorker and CopyWorker class ExecSQLWorker : WorkerTemplate {}; class CopyWorker: WorkerTemplate {}; In my WCF Service i have my Interface like that: public interface IPQWService { [OperationContract] void EnqueueWorker(WorkerTemplate[] worker); } Now in my client application the WorkerTemplate[] consists of

Implementing callback (for C library) as pure virtual in C++ abstract class

烈酒焚心 提交于 2019-12-20 05:27:15
问题 I ran into a problem when using a C audio library (PortAudio built with ASIO SDK, but this isn't really relevant to this question; the technical details would only hinder me asking the question) . Simply put, a certain C function from that library requires me to give it a callback function that returns a certain value and accepts certain parameters; for the sake of clarity, let's say the callback should look like this: int callback(int arg) and the call to the library function looks like this

How to use virtual functions to achieve a polymorphic behavior in C++?

两盒软妹~` 提交于 2019-12-20 04:55:25
问题 I am new to these important features of C++, i have already read a few question/answers on these topics here and googled a few docs. But i am still confused with this... It would be great if some one can advice me some good online tutorial or book chapter which takes this concepts easy and slow and starts it from the basic. Also, if some one knows some on-hand exercise material that would be great. 回答1: Here's the best explanation of polymorphism that I've ever heard: There are many animals

Casting to Superclass, and Calling Overriden Method

你说的曾经没有我的故事 提交于 2019-12-20 04:51:59
问题 I have my next question. I have extended a class, Parrent and overridden one of its method in the Child class. I tried to cast the type to the superclass type, but I get the child's overridden method every time. This also happens when I use polymorphism. Questions are in the comments inside code below... Thanks in advance. class Parrent{ public void test(){ System.out.println("parentTest"); } } class Child extends Parrent{ @Override public void test(){ System.out.println("childTest"); } }

Ambiguous java hierarchy

送分小仙女□ 提交于 2019-12-20 04:37:02
问题 My question is why x.proc(z) below does print 57 instead of printing 39 ? class X { protected int v=0; public X() {v+=10; System.out.println("constr X");} public void proc(X p) {System.out.println(43);} } class Y extends X { public Y() {v+=5;System.out.println("constr Y");} public void proc(X p) {System.out.println(57);} public int getV() {return v;} } class Z extends Y { public Z() {v+=9;System.out.println("constr Z");} public void proc(Z p) {System.out.println(39);} } class Main { public