10,431 questions
Best practices
0
votes
3
replies
94
views
Code design conundrum: runtime polymorphism, templates, compile times, and OpenMP
I'm struggling to finalise the design of my C++17 library.
One of the primary goals is to use runtime polymorphism to allow users to extend or rewrite default features of the library for their own use ...
3
votes
1
answer
129
views
Why can’t I efficiently move-construct `std::polymorphic<Base>` from `std::polymorphic<Derived>`?
As I understand it, std::polymorphic<T> and std::indirect<T> are const-propagating wrapper types for an owning pointer to a T object, where the target of the pointer is copied with the ...
3
votes
2
answers
92
views
Automatically set timestamps on polymorphic relation table
I have a ProductLandedCost model with a morphToMany relationship to various other models: Warehouse, for example:
class Warehouse extends Model
{
// ...
public function productLandedCosts(): ...
2
votes
3
answers
119
views
How to Unit Test Multi-Stage Member Functions in Polymorphic Classes Used with std::variant?
I have three processor classes that share a common interface for polymorphic behavior using std::variant and std::visit. Each processor has a processData() member function that internally consists of ...
2
votes
2
answers
209
views
Is it possible/how to let parent class thread invoke function in child class that overrides parent function?
I am trying to implement a Parent and Child class. The Parent class would start a thread and run a routine function. The Child class would override the routine function if needed.This seems perfectly ...
Best practices
1
vote
6
replies
225
views
Base class and derived classes in Qt
I'm building a Qt wrapper for RtMidi. As starting point I created two separate classes QMidiOut and QMidiIn that wrap RtMidiOut and RtMidiIn.
I don't want to duplicate code for the common methods (e.g....
5
votes
1
answer
140
views
What is the formal name for GHC automatically adapting less-constrained functions to more-constrained rank-2 arguments? [closed]
Consider this Haskell code that compiles successfully:
{-# LANGUAGE RankNTypes #-}
-- Applies a polymorphic function to different types, expecting 3 constraints
applyToMany :: (forall a. (Show a, Eq ...
0
votes
0
answers
65
views
Issues with polymorphis in Swagger (springdoc) in Spring Boot after upgrading to 3.5
I have created a very simple demo project with one controller endpoint:
Spring Boot 3.5.6
Springdoc 2.8.13
This was working much better in Spring Boot 2.7.x
Full project here: https://github.com/...
-1
votes
3
answers
196
views
Python function change behaviour depending on type of argument
I'm playing around with OOP in Python, and I have something like this:
class Person:
def __init__(self,name, age):
self.name = name
self.age = age
self.hobbies = []
...
0
votes
2
answers
69
views
MyIntListImpl is not abstract and does not override abstract method getItem() in IMyDummyList [duplicate]
I don't understand why my class that extends an abstract class (which implements an interface) does not override the interface's single method. I get this error when I try to compile:
MyIntListImpl ...
0
votes
0
answers
164
views
How to reproduce C++ deleting destructor for a custom allocator?
I am aware that calling delete this in a member function is not UB in itself. In fact, compiler is doing the very same thing when one calls delete ptr, ptr being a pointer to a polymorphic object (...
1
vote
1
answer
111
views
Polymorphic deserialize JSON using DI container to create instances
Consider these model classes:
[JsonPolymorphic(TypeDiscriminatorPropertyName = "$type")]
[JsonDerivedType(typeof(A), typeDiscriminator: nameof(A))]
[JsonDerivedType(typeof(B), ...
3
votes
1
answer
99
views
What does "single run-time representation" mean in Bjarne Stroustrup's description of type erasure in "The C++ Programming Language"?
In The C++ Programming Language - 4th edition, in §25.3 at page 731-732 Bjarne Stroustrup shows a possible implementation of a Vector template class that's been specialized for all T* template ...
1
vote
0
answers
72
views
Kotlin serialize simple class with explicit Serial Name
I've tried to use kotlinx.serialization with the third party Yaml plugin from @charleskorn.
In the past I already did something with Python using pyyaml, where I explicitly set a custom yaml_tag for ...
3
votes
2
answers
114
views
Is casting from a pointer polymorphic base object to pointer to a derived object via explicit cast and accessing only functions ill-formed? [duplicate]
Given a base/derived class polymorphic relationship (established below via the virtual destructor), if I explicitly (C-style) cast a pointer-to-a-base object to a pointer-to-a-derived object, is this ...
1
vote
2
answers
129
views
C++ polymorphism across boundaries in shared libraries
I am developing a simple logic-gate simulation.
For each logic gate type the following class is implemented:
class Gate {
public:
explicit Gate();
virtual ~Gate() = default;
virtual void ...
1
vote
1
answer
102
views
How to Track Full Update History of TPT Entities in EF Core with MySQL [duplicate]
I'm designing a .NET Core application using EF Core with a MySQL database, following a Table-per-Type (TPT) inheritance strategy.
I have a base Report class with several derived types such as ...
0
votes
0
answers
66
views
How to map class hierarchy with Mapster to a flattened dto
I have this class hierarchy:
public abstract class Device
{
public string Code {get;set;}
}
public class Motor: Device
{
public int SerialCode {get;set;}
}
public class Battery: Device
{
...
1
vote
4
answers
153
views
Why is the base class _vptr incorrect when it is passed as lpvoid to be derefenced later?
My app crashes when I try to pass the base class as lpvoid then typecasting back to the base class, but works fine if I use a different method by passing the pointer to the base class as a function ...
1
vote
1
answer
69
views
C++ Polymorphic Iterators in Abstract Base Class with Custom Child Implementations
I'm working on a C++ project where I have an abstract base class Store that represents a container of Bin objects. Each subclass of Store (e.g., DenseStore, SparseStore) uses a different internal data ...
2
votes
4
answers
217
views
Inheritance - intended polymorphism isn't working
I have a simple c++ base class and two derived classes:
class base;
class der1;
class der2;
class base{
public:
virtual void act(base*); // ignore for now
virtual void print(){
cout &...
3
votes
1
answer
176
views
Smart pointers and polymorphism - Use in a model class is causing casting issues
I'm attempting to make a game in C++ where pieces will move around a gameboard.
In our team, we're trying to be good and use as much modern methodology as we can, given we've gotten rusty and now have ...
3
votes
1
answer
145
views
How to properly implement polymorphism with base class pointers in C++?
I'm learning Object-Oriented Programming in C++ and trying to understand polymorphism with base class pointers.
I created a base class Animal and a derived class Dog, both with a speak() method. I ...
0
votes
0
answers
36
views
Why does V8 deoptimize certain closures with inline caching despite seemingly monomorphic access patterns?
I'm debugging a performance regression in a hot code path and found that V8 is deoptimizing a function that uses closures in a monomorphic way. Here’s a simplified version of the pattern
function ...
0
votes
1
answer
43
views
How to separate rendering from logic properly?
I'm trying to create a simple simulation with shapes drawn on the screen. I've heard that it's good practice to separate the logic from the rendering, but I'm still not sure how to handle this ...
2
votes
3
answers
163
views
Polymorphism in std::variant when all template types are derived from the same base class
I want to store objects of different types in a std::set and rely on std::set's sorting to later access them by some key variable which is present in every type. To store different types I am using ...
0
votes
0
answers
36
views
How to handle existing data when introducing an intermediate model between a base model and a child model in a Django Polymorphic inheritance hirarchy
Current models:
class Task(PolymorphicModel):
pass
class DownloadTask(Task):
pass
New models:
And now i want to do add a new layer called VirtualTask:
class Task(PolymorphicModel):
pass
...
0
votes
3
answers
138
views
Polymorphic std::format that allows overriding
In order to use C++20's format capabilities with custom types, we have to provide template specializations of std::formatter for every type we want to format. The nice thing about this approach is ...
1
vote
1
answer
64
views
CRTP sub class chosen at run-time [duplicate]
I need a Parent-Child class using CRTP for performance reasons. Something like this:
template<class CHILD>
struct Parent
{
};
struct Child1 : public Parent<Child1>
{
};
but I need a way ...
0
votes
0
answers
74
views
Why do I have to override all functions with the same name? [duplicate]
Today I have found an interesting problem. Base is basically a class with well-defined and stable API with virtual functions encouraging to redefine their behaviour:
class Base {
public:
virtual ...
2
votes
1
answer
59
views
Is "Associated Types with Class" known to have some typos in it? Or are associated types far different today than what the paper describes?
I'm reading Associated Types with Class, and I'm running into code that doesn't seem to be compilable. Is it just a typo in the paper, or is that things have changed a lot since 2004, when the paper ...
2
votes
1
answer
230
views
Can I have my cake and eat it w.r.t sequences of polymorphic value types?
I am designing a certain part of a library and considering whether or not to use polymorphic value types of some kind . If you don't know what those are and what the motivation for them might be, see ...
1
vote
0
answers
105
views
How can I transform a monolithic state machine into a polymorphic state pattern when each state has unique extended methods and interfaces?
Please excuse me for how long this question is. I tried to shorten it as much as possible while still listing all of the various things I've tried and the problems I've run into.
I have a state ...
1
vote
1
answer
68
views
How to combine IntoKeys and IntoIter in the same function?
This code doesn't compile:
use indexmap::IndexMap;
use either::Either;
type Atom = Either<IndexMap<String, u64>, Vec<String>>;
fn flatten(atoms: Vec<Option<Atom>>) ->...
1
vote
1
answer
98
views
Procedure Overloading with an allocatable variable as parameter in fortran
I am learning oop in Fortran. And I wonder about the interest of overloading type bound procedure when the type of an argument is not known at compile time.
Let me explain, step by step my problem (...
0
votes
1
answer
235
views
Spring GraphQL @oneOf input: How to map to polymorphic Java types?
I'm using Spring GraphQL with graphql-java and want to leverage the new @oneOf directive introduced in GraphQL 2022.
Here's a simplified version of my schema:
type Mutation {
createAnimal(input: ...
1
vote
2
answers
145
views
How to properly clone an object that contains a polymorphic object which may contain a vector to more of those polymorphic types?
I'm limited to C++17. I need to make a tree structure (not necessarily binary) that can be deep copied into 2 independent clones. Currently, I have an interface Node class where there's 2 ...
0
votes
1
answer
86
views
How to get to polymorphic grandchildren in Rails ActiveRecord
I have an entity that has invoices and offers.
class Entity < ApplicationRecord
has_many :invoices, dependent: :restrict_with_error
has_many :offers, dependent: :restrict_with_error
Invoices ...
0
votes
1
answer
76
views
Add union of subclasses to vector of base class in C++
I have a base class A, and two subclasses B and C which inherit from A. I would like to be able to store a union of B and C in an std::vector<A*>, but it seems that it may not be possible. Here'...
0
votes
0
answers
30
views
Apartment inherits Realty. TypeError: Cannot read properties of undefined (reading 'ownColumns')
I have these models: https://pastebin.com/5d5mvPgF. So I've created an abstract Realty class and Apartment entity class which inherits Realty.
Data source:
import "reflect-metadata";
import {...
0
votes
0
answers
41
views
Laravel many to many relation between TWO polymorphic types
I want to create a relationship between parents (which can be Mother or Father models) and their children (Son and Daughter models). For exemple purposes, they are defined like this:
father:
id
...
3
votes
1
answer
124
views
Can an object that owns a unique pointer be unique pointed to?
I have the following minimal reproducible example where it seems that my unique-pointer-containing object B is not allowed to contain a unique pointer to another instance of B:
#include <iostream&...
-4
votes
1
answer
70
views
Search duplicate items with the function morphToMany in Laravel
I want to create a routine function in my project.
I've created it in Laravel 10 with Alpine, Tailwind, Livewire and Flowbite.
enter image description here
enter image description here
enter image ...
1
vote
2
answers
96
views
How to do an early return of a child in the parent class in OOP
I'm a student programmer and we are learning to use unity. I have this problem in C#.
So I have a weapon class and a chil named gun.
On weapon :
public override void PrimaryInteraction() {
if (...
2
votes
0
answers
67
views
BeanCreationException in SpringBoot
I have two classes extending the same parent class; AbstractVaultConfiguration. My base classes @override the same two methods from AbstractVaultConfiguration which are clientAuthentication and and ...
3
votes
1
answer
140
views
How to prevent code heavily relying on polymorphism from being littered with `make_shared` all over the place?
I am writing a UI framework for a microcontroller, in which I want to do a hierarchical menu system.
For that in particular I have the following classes:
class MenuNode {
public:
MenuNode(const ...
0
votes
1
answer
68
views
undefined method `primary_key' for String:Class
Using Signed Global IDs with Polymorphic Select Fields in Rails Forms:
My models:
class Comment < ApplicationRecord
belongs_to :commentable, polymorphic: true
end
class Post < ...
-1
votes
1
answer
65
views
WTF there are many definitions for polymorphism
I’m really confused about what polymorphism in OOP actually means. Everyone explains it differently, and I’m not sure which definition is correct:
In some places, polymorphism is explained as dynamic ...
1
vote
4
answers
165
views
Polymorphism and abstract classes in Java
Let's say I have the following classes:
public abstract class Animal {
public abstract void talk();
}
class Dog extends Animal {
@Override
public void talk() {
System.out.println(&...
0
votes
0
answers
23
views
I am trying to achieve polymorphic field for my mongo using spring JPA
here is my Value Class @Data
@NoArgsConstructor
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
property = TYPE,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
visible = true)
@...