Questions tagged [polymorphism]
In computer science, polymorphism is a programming language feature that allows values of different data types to be handled in a uniform manner.
10,247
questions
0
votes
0
answers
77
views
Pure virtuals and good practice
EDIT
Handler(Accelerometer &a): acc(a){} would be the proper way of assigning the reference and Accelerometer &acc would be the proper way to define the reference member.
Background
The ...
0
votes
2
answers
42
views
Spring and Factorymethod
I would like to instantiate a class in a factory method and execute the respective implementation of the method defined in the interface.
My problem is that I don't want to create the dependencies of ...
-2
votes
0
answers
7
views
How to do OOPS in python?
I have heard the term object-oriented programming in python but I have know idea about this.
I tried to learn but I don't Understand classes, method, functions, attribute. and I want to learn about ...
-2
votes
1
answer
26
views
Write a python program using inheritance and polymorphism
Certainly! Let's delve into a detailed Python question related to object-oriented programming and inheritance:
Question: Understanding Python Inheritance
Consider the following scenario where you are ...
-1
votes
0
answers
36
views
Abstract Base Classes vs Inheritance + method overloading [closed]
I am new to abstract base classes, and trying to get a better understanding by providing examples. Please let me know if ABC is even necessary for my use-case, or could I just do normal inheritance, ...
-3
votes
1
answer
26
views
What is Polymorphism in python? [closed]
Give a detailed intro about Polymorphism
We had tried to find and solve the problems related to it but from where I have studied or the platform I have done python it is not understable as per my ...
0
votes
0
answers
87
views
Storing any data type in container inside another container
I need to have a container inside other container that stores any type of data:
std::vector<std::vector<std::any>> vectors(5);
I need to use vector methods on any element of outer vector ...
1
vote
0
answers
41
views
How to deal with libraries requiring unique_ptr as inputs in pybind11?
C++ libraries that involve dynamic runtime polymorphism may choose to use std::unique_ptr to pass instances around. Given a basic type hierarchy like:
struct Animal
{
virtual ~Animal() = default;
...
1
vote
0
answers
94
views
Why is dynamic polymorphism faster than using std::variant? [duplicate]
I know that because of virtual tables, cache misses and memory allocation for pointers to the base class, dynamic polymorphism is slow. Especially when compared to static polymorphism.
I decided to ...
0
votes
1
answer
46
views
JavaFX ObservableList<SuperClass> does not keep SubClass attributes
I have an ObservableList<superclass> that holds subclass objects. When I try to access attributes specific to the subclass, they seem to be missing despite being passed to the superclass list.
...
1
vote
0
answers
73
views
Implementing an Object type in C++ [closed]
I'm currently working through the "Crafting Interpreters" book by Robert Nystrom. He uses Java to implement the visitor pattern in which the visitor functions return the Object type. C++ ...
1
vote
2
answers
47
views
C# - Extend/Add interface to parameter of overridden method
I am pretty new to C#. I am trying to derive a class UIObjectContainer : UIObject which overrides a method FillProperties which 'fills' in a parameter Properties (just holds misc data).
I want to ...
0
votes
0
answers
33
views
ForeignId Laravel with different type of categories
I have an item table, each item can have different types of categories.
Items
Schema::create('cocktails', function (Blueprint $table) {
// Main data
$table->id();
$table->string('...
1
vote
1
answer
64
views
Polymorphism in Rust. AsRef vs Deref
I want to create a function that can take as an argument String, &str, Rc<String>, Arc<String>, etc. String is just an example, the parameter can be of any type. I can do this using ...
0
votes
1
answer
71
views
Is it possible to leverage polymorphism from within a generic method in C#?
string Foo(string key, string defaultValue)
{
return Lookup(key);
}
bool Foo(string key, bool defaultValue)
{
return bool.Parse(Lookup(key));
}
T Bar<T>(string key, T defaultValue)
{
...
0
votes
1
answer
53
views
C++ virtual template member function [duplicate]
#include <iostream>
class Data_X {
public:
Data_X(int x): x(x) {}
int getData() {
return x;
}
private:
int x;
};
class parent {
public:
template <typename T>
T* ...
-1
votes
1
answer
61
views
template specialization of unique_ptr generating member function
I am trying to make comm class with template variable.
My colleague ask me to use std::unique_ptr for memory management.
But I think I failed to implement polymorphysm.
I wrote code as the following.
...
-4
votes
0
answers
107
views
automatic casting objects with std::type_info
Smallest reproducible example: https://onlinegdb.com/Ce4lAL9p3
In this code, I am creating a macro to handle editing any variable:
#pragma once
#include "Object.h"
#include "Editable.h&...
0
votes
0
answers
18
views
Class loader issue - duplicate class definition for name -LinkageError
I have created a class with the following attributes and methods.
I think there is a fundamental issue with my implementation. When I import this signalwrapper class into my test framework, I receive ...
0
votes
1
answer
50
views
Executing array of child objects methods in C++
Suppose I have a base class A and two derived classes B and C. I want to create an array of function pointers that consist of virtual members of both B and C and execute them.
How do I do this?
I ...
1
vote
3
answers
180
views
How to "merge" 2 functions (const & non-const i/o) into one?
Is there a syntax for merging the following 2 functions into one, so that if input is const, result is const, and if input is non-const, result is non-const?
char* f1(char* x) {
return x+1;
}
...
1
vote
3
answers
117
views
Call a child class method from a parent class variable without using instanceof or casting [duplicate]
I have 4 classes
public class User {
private Account account;
public void setAccount(Account account) {
this.account = account;
}
public Account getAccount() {
return ...
-2
votes
0
answers
23
views
Is there a better way to access a child class method when looping through a list of the parent class than using instanceof? [duplicate]
My question is very similar to one posted a long time ago:
Get a child class when looping through its parent class java
, however, when applying the solution given I felt like there must be a more ...
0
votes
0
answers
72
views
C++: contigouos memory for polymorphic objects
I'm currently working on a multiplayer board game and manage the playing fields using a simple std::vector<T>.
However, I don't store the concrete objects in the vector, but only the pointers to ...
1
vote
1
answer
28
views
How to use polymorphism to transform an object from one type into another with "transformer" classes inheritance
I'm developing a GraphQL API. The GraphQL types and the database types are not the same because I didn't want my database specific structure to leak to the API consumers. Basically I want to rename ...
1
vote
1
answer
90
views
Generic ActionResule<T> in .NET Core doesnot respect the type T
In the below example, GET method has return type of
ActionResult<SomeAltResult>
but the return statement returns OKResult with an IEnumerable of WeatherForecast and still it is compiling ...
0
votes
0
answers
14
views
How can I create one method called with different object types call a strongly typed method with that parameter?
I currently have two methods, identical except for the object type being passed in and a method call using that object.
Objects IN:
PostmarkMessage
TempatedPostmarkMessage
Method call (accepts ...
0
votes
2
answers
88
views
Getting an optional of a class derived from abstract class, from a class derived from abstract class
I have something similar to the code below, where a group of classes with similar shared behaviours (Tool1, Tool2), all inherit from an abstract class (ITool). All these classes own their own optional ...
0
votes
0
answers
17
views
modelBuilder for Polymorphic Relations in EF Core
I'm trying to make a Polymorphic association but my modelBuilder is not working well
Employee Class
public class Employee
{
public int Id { get; set; } // Unique identifier for the employee
...
0
votes
1
answer
34
views
Typescript Polymorphic component typing not working
I'm trying to create a polymorphic component, but I don't understand the error that is throwing. Would appreciate some assistance and perhaps an improved version.
The expected outcome is that I should ...
-2
votes
1
answer
47
views
Java polymorphism top level method is called
I have an inherited object in a java project and I am trying to call the overriden method in the child object. For some reason the parent version of the method is being called. It was my understanding ...
0
votes
1
answer
56
views
Implementing Type-Safe Factory Methods for Generic Types in Rust
Newbie to Rust here, though I have spent quite some time in low-level / kernel driver / embedded C and have some light experience with C++11 and newer. I suspect part of my problem will be un-...
0
votes
1
answer
82
views
How to implement One To Many (Polymorphic) in symfony
Hi I have three entities User, Admin, Log
Both the admin and user have many logs.
Each log has one owner might be Usermight be Admin.
In this case, I was searching for a similar system as Laravel ...
3
votes
2
answers
82
views
AUTOSAR rule A5-0-4 stance on pointer arithmetic
AUTOSAR rule A5-0-4 states
Pointer arithmetic shall not be used with pointers to non-final
classes.
It provides the following rationale:
Pointer arithmetic is only well defined if the pointed-to ...
2
votes
1
answer
58
views
Java type which accepts a class AND its interfaces (contravariance)
I have a class and an interface like this:
interface Employee {...}
class Developer implements Employee {...}
Then I have classes which implement steps in the onboarding process:
interface ...
0
votes
0
answers
45
views
Regarding 'Shape* ps2 = new Ellipse;' Is my understanding correct?
Shape is an interface.Shape declares a pure virtual function GetArea. GetArea is implemented in Ellipse and Rectangle respectively
//code1
Shape* ps2 = new Ellipse;
ps2->GetArea();
Can code1 be ...
0
votes
1
answer
108
views
What are the pros and cons of template and polymorphism based approaches in C++?
I wrote a program where the type of a variable is not known at compile time. It is asked to the user at run time. In the first program version, a part of the code was repeated. The modifications ...
0
votes
1
answer
52
views
Declaring a method do(int in) in a superclass and a method do(double in) in a subclass
I am reviewing some importants rules in Java.
My questions are :
Declaring a method do(double in) in a superclass and a method do(int in) in a subclass.
Is the method do(int in) in the subclass is ...
0
votes
0
answers
19
views
How to build a View in SwiftUI that displays different type of data models in a polymorphic-like way?
Trying SwiftUI again after a few years ... I have some difficulties mapping my UI idea to SwiftUI, maybe thinking too much in an OOP way?
I want a list of different data models to be rendered by their ...
0
votes
1
answer
62
views
C++ Derived Member Object in Derived Class and how to initialize
It seems like a case the you face frequently in C++, but for which I haven't really found a answer.
We have a base class Car with a member object Engine. Now we derive Car to RaceCar. That racecar has ...
0
votes
1
answer
76
views
What is the point of a covariant return type in a virtual function?
The most common usage of a covariant return type I've seen is via virtual functions. The simplest example I can come up with looks something like this:
class Base {
public:
virtual Base& get() { ...
0
votes
1
answer
125
views
How to determine the type of something in a derived class?
I'm making a farming simulator in which I have the base class Produce, which is inherited from by two classes Crops and Animals. Whenever time progresses I am looking to check through my array of ...
1
vote
1
answer
67
views
Why is my "setter" function, stored in a std::map, not setting value correctly?
I'm trying to code a basic game engine, and have been running into a problem regarding dynamic component creation. I have a minimal working example here:
#include <bitset>
#include <variant&...
0
votes
1
answer
83
views
Is constraining template parameters bad practice?
In C++, it is legal to assume a template parameter has certain fields or methods according to this source. In case it does not, it will simply fail to compile.
template<class Container>
void ...
0
votes
0
answers
8
views
How to get data on polymorphisms of TP53 (specifically on codon 72) in TCGA
I would like to know if there is any possibility to get polymorphism data of codon 72 of TP53 for different cancers in TCGA.
Using TCGAbiolinks I could retrieve the "Simple Nucleotide Variation&...
1
vote
0
answers
34
views
Why is the virtual keyword required to allow the type of substution behavior I'm looking for in c# [duplicate]
I'm trying to get a certain type of overriding behavior in c#, I have found that I need to have virtual flag on the function definitions in a base class that I want to override with the derived class. ...
0
votes
2
answers
77
views
String manipulation macro: expected specifier-qualifier-list before ‘,’ token
I am trying to write a macro which will automate definition of polymorphic types achived by composition and function pointers.
I don't really know a lot about macro string manipulation. This is how I ...
0
votes
2
answers
78
views
Does polymorphism allow to override all the methods of its superclass in subclass?
Please explain this with an example.!
It is not mandatory for a subclass to be overriding all the methods from the superclass.
It provides the option to override methods, but it's not a requirement.
...
0
votes
3
answers
72
views
Pattern for doing a different thing based on which subclass is received?
Currently I have something similar to the following data classes and I want the receiver of CreateScheduledEventRequest to perform different actions when ScheduleInfo is a RepeatingTask versus when it ...
0
votes
3
answers
52
views
Should I use abstract class plus child class or ENUM or abstract class plus interface? [closed]
Was solving a task that went like this
Create a tree, then create an oak, fig, pine, apple, orange tree...
Keep in mind some trees can have fruit.
Then create a forest that has trees and make all the ...