polymorphism

C++ class method, returns vector<subclass>

六月ゝ 毕业季﹏ 提交于 2020-01-05 07:51:10
问题 I'm having a bit of trouble with a method I'm trying to write for a class. I have class symbol and class terminal. class terminal extends class symbol, but one of the methods of class symbol needs to return a vector. E.g.: #ifndef SYMBOL_H #define SYMBOL_H #include "terminal.h" #include <vector> using namespace std; class symbol { public: vector<terminal> first(); virtual void polymorphable(); }; #endif With class terminal defined: #ifndef TERMINAL_H #define TERMINAL_H #include "symbol.h"

Should/how can I avoid downcasting in this case?

安稳与你 提交于 2020-01-05 05:10:49
问题 Say I have a base and derived class, where the derived class implements some additional manufacture specific functionality: class Device { // Base class } class DeviceFromSpecificManufacture : public Device { // Derived } When my program runs, it requires the user to select a device from an array of available devices. At this point, it's fine for me to use the base class as I only require basic device functionality (nothing specific to the manufacture): std::vector<std::shared_ptr<Device>>

FindObjectOfType returning null

偶尔善良 提交于 2020-01-05 04:56:10
问题 The issue I am having is with a dropped item i pick up adding ammo to a gun. Built a Gun class with all the methods and variables. Built a Rifle class derived from the Gun class The Rifle works perfect No Issues I now am adding a "PickUp" system where x amount of enemies drop a pickup. This is the script on the item to pick up public class AddARAmmo : MonoBehaviour { private Rifle rifle; private void Awake() { rifle = FindObjectOfType<Rifle>(); } private void OnTriggerEnter(Collider other) {

Method called by parent constructor behaves as child method

对着背影说爱祢 提交于 2020-01-05 04:12:06
问题 I am testing some polymorphism in java and my code looks like the following: class Base { int value = 0; public Base(){ System.out.println("Came Here And Value is: " + value); addValue(); } String addValue(){ System.out.println("Calling Bases' addValue and value is currently: " + value); value += 10; return ""; } int getValue(){ return value; } } class Derived extends Base{ public Derived(){ System.out.println("Calling Derived constructor and value currently is: " + value); addValue(); }

How to deserialize polymorphic LinkedList with Jackson JSON and JAVA?

时光毁灭记忆、已成空白 提交于 2020-01-05 04:03:19
问题 I´m having some problems when i try to deserializing a Java class. My classes are : Abstract Class A { Long ID String name ... } Class B extends A{ String Type String Value } Class C extends A{ List<A> typedList; } And my Serialization/Deseralization Methods: public String serialize(T object) { String returnValue = ""; try { returnValue = mapper.writeValueAsString(object); } catch (JsonGenerationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch

Polymorphism with copy constructor

冷暖自知 提交于 2020-01-04 15:17:28
问题 Here is the code I use. I'd like to know if what I did is correct and safe. Normally it compiles and the tests I did are successful. But as it is the first time that I use dynamic_cast and static_cast , I'd like to be sure that I didn't miss anything. Can you please check : that I don't forget to delete any pointer (I don't really understand what my clone method does...) if I use correctly dynamic_cast and static_cast that there is not a more efficient of doing it But my main concern is that

Protocols versus polymorphism in swift

我的梦境 提交于 2020-01-04 05:51:54
问题 I am rather new to object-oriented programming and I am attempting to wrap my head around protocols, delegates, and polymorphism. I recently watched a training video that promoted that when you have two classes that are similar with similar method implementations, a protocol is the best solution for achieving this elegantly. That makes sense. However, some additional research has led me to discover polymorphism and it sounds like that is also a preferred approach, whereas you could simply use

static method with polymorphism in c++

五迷三道 提交于 2020-01-04 02:53:12
问题 I have a weird issue using polymorphism. I have a base class that implements a static method. This method must be static for various reasons. The base class also has a pure virtual method run() that gets implemented by all the extended classes. I need to be able to call run() from the static class. The problem, of course, is that the static class doesn't have a this pointer. This method can be passed in a void * parameter. I have been trying to come up with a clever way to pass the run method

Derived class defines function via base class

断了今生、忘了曾经 提交于 2020-01-04 01:51:32
问题 Consider the following code: struct A { virtual void f() = 0; }; struct B { void f(); }; struct C : public A, public B {}; int main() { A* a = new C(); B* b = new C(); C* c = new C(); // All these calls should result in B::f a->f(); b->f(); c->f(); } The compiler states that C is abstract. How can this situation be resolved? The issue seems similar to diamond inheritance, but I fail to see the solution. EDIT: Thanks, this is the working example: #include "stdio.h" struct A { virtual void f()

Virtual destructor in polymorphic classes

心已入冬 提交于 2020-01-03 18:05:50
问题 I understand that whenever you have a polymorphic base class, the base class should define a virtual destructor. So that when a base-class pointer to a derived-class object is deleted, it will call the destructor of the derived class first. Correct me if i am wrong here. also, if the base-class destructor were to be non-virtual, it would be undefined behavior to delete a baseclass pointer to a derived object. Correct me if i am wrong aswell. so my question is: Why is it exactly, that when the