polymorphism

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

一世执手 提交于 2020-01-27 02:23:46
问题 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 the new keyword? 回答1: C# not only supports method overriding, but also method hiding. Simply put, if a method is not overriding the derived method, it is hiding it

Can't make a vector of a class containing a ptr_vector<an_abstract_class>

十年热恋 提交于 2020-01-24 16:20:07
问题 I need to have a std::vector of boost::ptr_vector s. To make their management easier, I enclosed the boost::ptr_vector in a class ( Zoo ) and made a std::vector of it ( allZoos ). Look at a minimal code for reproducing this: #include <boost/ptr_container/ptr_vector.hpp> #include <boost/utility.hpp> class Animal { public: virtual char type() = 0; }; class Cat : public Animal { public: char type() { return 1; } }; class Zoo { public: boost::ptr_vector<Animal> animals; }; int main() { std:

How to pass any object that can display text to a C# method

孤人 提交于 2020-01-24 15:48:06
问题 I'm trying to write a method of a class that should get an object (such as textbox or label) as a parameter and whenever it needs to show a message to the user it uses the object to show the message. As the class will be used in other programs, it should be a little portable and it should implement the functioning to any text-based object such as labels or textboxs etc. Consider the following method: public void TcpEndPoint(string IP,/* a text-based object */) { // There's a need to show a

Access hidden property in base class c#

帅比萌擦擦* 提交于 2020-01-24 14:09:13
问题 In my ASP.NET Core API, I have a DTO class BaseDto and another DerivedDto that inherits from BaseDto and hides some of its properties, because they're required in DerivedDto . I also have a BaseModel class to which both BaseDto and DerivedDto will be mapped through another class Mapper . Something like the following code: using System.ComponentModel.DataAnnotations; public class BaseDto { public string Name { get; set; } } public class DerivedDto : BaseDto { [Required] public new string Name

Why is the dynamic_cast allowed to yield a null-pointer for polymorphic classes when the destination pointer is not of the type of a base class?

陌路散爱 提交于 2020-01-24 09:00:11
问题 Consider the following program #include <iostream> #include <iomanip> struct A { }; struct C { }; int main() { C *pc = nullptr; A *pa1 = dynamic_cast<A *>( pc ); std::cout << "pa1 == nullptr is " << std::boolalpha << ( pa1 == nullptr ) << '\n'; A *pa2 = pc; std::cout << "pa2 == nullptr is " << std::boolalpha << ( pa2 == nullptr ) << '\n'; } For the both pointer declarations, pa1 and pa2, the compiler reports an error that such an initialization is not allowed. For example the clang HEAD 10.0

C++ Templates polymorphism obstacle

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-24 03:25:09
问题 Interface: template <class T> class Interface{ public: typedef T Units; virtual T get() = 0; }; Implementation1: class Implementation1: public Interface<float> { public: float get() { return 0.0f; } }; Implementation2: class Implementation2: public Interface<int> { public: int get() { return 0; } }; Container (with errors): class Container{ private: Interface* floatGetter; int n; Timer::Units* array; public: Container(Interface* floatGetter, int n) { this->floatGetter= floatGetter; this->n =

Equality on constraints

守給你的承諾、 提交于 2020-01-24 02:55:27
问题 Basically, given {-# LANGUAGE PolymorphicKinds, ConstraintKinds, TypeFamilies #-} (and more, if necessary), does the (~) type-level operator work on type-level expressions of kind Constraint ? I tried googling the answer, but had no luck. 回答1: Yes, it is possible. Because types of kind Constraint are finite sets of atomic type constraints, you can test their equality very easily. The PolyKinds extension is not necessary, however. Also, there's very few situations when this kind equality would

Understanding polymorphism in Go

这一生的挚爱 提交于 2020-01-24 02:09:05
问题 I guess I got stuck in thinking about a polymorphism solution to my following problem: Let's say I have a BaseTX struct with fields for a transaction. Now I have two special types of transactions: RewardTX struct and AllowanceTX struct . RewardTX struct has at this moment only the composition of BaseTX struct . AllowanceTX struct has a composition of BaseTX struct and an AddField . I have also a function logicAndSaveTX() , which has some logic on fields from BaseTX but at the end is

TypeError: No to_python (by-value) converter found for C++ type

半腔热情 提交于 2020-01-24 01:46:25
问题 I'm trying to expose my C++ Classes to Python using Boost.Python. Here is a simplyfied version of what I'm trying to do: struct Base { virtual ~Base() {}; virtual char const *Hello() { printf("Base.Hello\n"); return "Hello. I'm Base."; }; }; struct Derived : Base { char const *Hello() { printf("Derived.Hello\n"); return "Hello. I'm Derived."; }; Base &test() { printf("Derived.test\n"); // ... // After some calculation, we get result reference `instance' // `instance' can be an instance of

Smart pointers as class members for polymorphism

三世轮回 提交于 2020-01-23 08:17:10
问题 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