All Questions
Tagged with polymorphism class
410
questions
-1
votes
1
answer
41
views
How to set up classes for parsing custom data types in C#
I'm working on a saving/loading system, and I'm trying to serialize and deserialize various objects. I'm working in Unity, and I've created one custom class for the game objects and their behaviours, ...
-1
votes
1
answer
68
views
How to call a child method after casting an instance from parent to child class?
If I have a class B that inherits from class A and overrides one of its methods, and I have, in TypeScript:
const a: A = new A();
(a as B).toto();
Why is the toto() method that will be called the one ...
1
vote
1
answer
19
views
How to move logic from runtime type matches to inherited classes without bloating the classes
I learned F# before C#, so I am unused to inheritance and do not understand its best practices.
As I understand, the nearest object-oriented equivalent to the discriminated union (the "one of&...
-1
votes
1
answer
47
views
How to properly inherit from dataclass with base instance re-usage
Consider the following:
@dataclass
class Base:
prop1: str
prop2: str
@dataclass
class Derived1(Base):
isValid: bool = self.prop2.casefold() == 'valid'
@dataclass
class Derived2(Base):
isOpen:...
2
votes
2
answers
157
views
VBA Nested Class
I have a VBA nested class that compiles, but I would like to format it better for extended use.
Currently to go into the animal/cat class, Animal.clsCat needs to be used. My question is, is there a ...
0
votes
2
answers
111
views
C# class inheritance and polymorphism with polymorphic fields
I'm running into an occasional design issue with classes, mostly in some video game stuff. The problem arises when a class that extends another class (such as a Player) has a field that is also a ...
2
votes
3
answers
91
views
Avoid if statement in class member function based on constructor inputs?
I'm looking for ways to optimize some ray tracing code that I have. Say I have the following Triangle class such as the following (simplified for brevity):
class Triangle {
public:
Triangle(...
1
vote
1
answer
115
views
sizeof CRTP class implementing an interface
i have a question about the sizeof an instance of a class that uses CRTP to implement an interface, as the following example.
#include <iostream>
class Interface
{
public:
virtual int foo() {...
0
votes
0
answers
28
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
3
answers
74
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 ...
2
votes
1
answer
89
views
Can dynamic_cast<Derived*>(static_cast<Base*>(voidPtr)) ever go wrong if voidPtr is null pointer or pointing to object that is inherited from Base?
class Base {
// ...
};
class Derived : public Base {
// ...
};
class OtherDerived : public Base {
// ...
};
If I know for a fact that voidPtr of type void* is a null pointer or pointing ...
3
votes
1
answer
70
views
Inheritance polymorphism for subclasses of subclasses in c++
#include <iostream>
using namespace std;
class parent {
public:
virtual void f() { cout << "parent::f2()" << endl; }
};
class child : public parent {
public:
...
0
votes
0
answers
176
views
Inheriting from abstract base class without implementing any virtual methods in subclass [duplicate]
I have a base class Map which multiple classes inherit from (e.g. France, Germany, Spain, etc). The subclasses simply initialize a constant adjacency list in the base class (via the constructor ...
1
vote
1
answer
656
views
C++ cast std::any to base class without knowing derived class type
A bit of an obscure issue here but I need a way to cast std::any to its base class, without knowing what derived class it is.
In other words, given a base class:
struct HCallable {
int numArgs;
...
0
votes
0
answers
35
views
Do virtual methods in C++ not work as expected with stack allocated variables? [duplicate]
I have these simple classes:
class Entity {
public:
virtual std::string getName() {
return "Entity";
}
};
class Player : public Entity {
private:
std::string name_;
...