polymorphism

Smart pointers as class members for polymorphism

怎甘沉沦 提交于 2020-01-23 08:17:06
问题 I'm new to smart pointers and I would be really grateful if somebody could give me a hint whether the way I'm handling smart pointers as class members is correct. More precisely, the solution that I would like to achieve is in the context of class polymorphism and should be ideally exception-safe. Given a container of heterogeneuous objects ( std::vector<shared_ptr<CBase> > my_vector ), the usual way to add elements is: my_vector.push_back( shared_ptr<CBase>(new CChild(1))) , so that later on

Polymorphism (inheritance) and value types

不问归期 提交于 2020-01-23 01:35:06
问题 I have a bunch of types, PixelMeasure , PointMeasure , CentimeterMeasure and so on, that represent a value with a unit. I would like them to have value semantics: e.g. effectively immutable, don't have to worry about memory allocation, and polymorphism: I can return an object of type Measure and can operate on it without knowning what specific kind it is. I would also like to be able to put multiple different Measure s into a container. It seems these are mutually exclusive in C++. For

Calling derived class through base class function pointer

狂风中的少年 提交于 2020-01-22 18:59:25
问题 Can I call a derived class through a base class function pointer , as shown in the example below? I understand that my example works , but is it guaranteed to always do so (Assuming the object actually implements the function!), or is this just an idiosyncrasy of the compiler I'm using? By this logic can't one simply derive all their classes from "CBase" (which in this case is empty so I guess no overhead) and ignore the type in the function pointer? #include <iostream> struct CBase { };

Polymorphism Through Extension Methods?

狂风中的少年 提交于 2020-01-22 13:48:46
问题 I have a class library which contain some base classes and others that are derived from them. In this class library, I'm taking advantage of polymorphism to do what I want it to do. Now in a consuming application, I want to change the behavior of some code based on the runtime type of the child classes. So assume the following: public class Base { } public class Child1 : Base { } public class Child2 : Base { } Now in the consuming application I want do something as follows (note that all of

GWT 2.4.0 RequestFactory polymorphism

懵懂的女人 提交于 2020-01-22 12:44:47
问题 Does GWT 2.4 support this case: @Entity class MyBase {...} @Entity class MyChild1 extends MyBase {...} @Entity class MyChild2 extends MyBase {...} ... @ProxyFor(MyBase.class) class MyBaseProxy extends EntityProxy {...} @ProxyFor(MyChild1.class) class MyChild1Proxy extends MyBaseProxy {...} @ProxyFor(MyChild2.class) class MyChild2Proxy extends MyBaseProxy {...} ... @Service(ArticleBase.class) public interface MyBaseRequest extends RequestContext { Request<MyBaseProxy> getStuff(); // MyChild1

why overridden method calling from Subclass if i have done up-casting?

不打扰是莪最后的温柔 提交于 2020-01-21 07:45:08
问题 i have just started learning java::Inheritance and confused while mixing Up-Casting. class Example{ public void methodOne(){ System.out.println("Example::Method_1"); } public void methodTwo(){ System.out.println("Example::Method_2"); } } public class Test extends Example{ public void methodTwo(){ //Method overriding System.out.println("Test::Method_2"); } public void methodThree(){ System.out.println("Test::Method_3"); } public static void main(String[] args){ Example exa = new Test(); //

why overridden method calling from Subclass if i have done up-casting?

北城以北 提交于 2020-01-21 07:44:27
问题 i have just started learning java::Inheritance and confused while mixing Up-Casting. class Example{ public void methodOne(){ System.out.println("Example::Method_1"); } public void methodTwo(){ System.out.println("Example::Method_2"); } } public class Test extends Example{ public void methodTwo(){ //Method overriding System.out.println("Test::Method_2"); } public void methodThree(){ System.out.println("Test::Method_3"); } public static void main(String[] args){ Example exa = new Test(); //

How is covariance cooler than polymorphism…and not redundant?

雨燕双飞 提交于 2020-01-21 01:44:05
问题 .NET 4 introduces covariance. I guess it is useful. After all, MS went through all the trouble of adding it to the C# language. But, why is Covariance more useful than good old polymorphism? I wrote this example to understand why I should implement Covariance, but I still don't get it. Please enlighten me. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Sample { class Demo { public delegate void ContraAction<in T>(T a); public interface

Why does it store or allocate memory for super class variables, in sub class object?

淺唱寂寞╮ 提交于 2020-01-20 07:51:18
问题 In the following code- class Mammal { String name = "furry "; String makeNoise() { return "generic noise"; } } class Zebra extends Mammal { String name = "stripes "; String makeNoise() { return "bray"; } } public class ZooKeeper { public static void main(String[] args) { new ZooKeeper().go(); } void go() { Mammal m = new Zebra(); System.out.println(m.name + m.makeNoise()); Zebra z = new Zebra(); System.out.println(z.name + z.makeNoise()); } } Both objects ( m and z ), if I see in debug

Why does it store or allocate memory for super class variables, in sub class object?

痞子三分冷 提交于 2020-01-20 07:49:05
问题 In the following code- class Mammal { String name = "furry "; String makeNoise() { return "generic noise"; } } class Zebra extends Mammal { String name = "stripes "; String makeNoise() { return "bray"; } } public class ZooKeeper { public static void main(String[] args) { new ZooKeeper().go(); } void go() { Mammal m = new Zebra(); System.out.println(m.name + m.makeNoise()); Zebra z = new Zebra(); System.out.println(z.name + z.makeNoise()); } } Both objects ( m and z ), if I see in debug