All Questions
Tagged with polymorphism casting
218
questions
0
votes
1
answer
42
views
Java - Why can't i take an array of a superclass, and get the subclass functions?
I'm trying to write a Chess Program from scratch. I have a superclass called "Piece", and subclasses for all the individual chess pieces (Pawn, Bishop, Knight, ect). I also have another ...
0
votes
1
answer
27
views
How to cast with additional bounds inside generic type parameter in Java
Is it possible to cast an object to fit an argument that is both a generic type parameter and with multiple bounds?
The situation i have is like the following:
public <T extends TypeA & TypeB&...
-6
votes
3
answers
115
views
Inheritance static methods [closed]
class Human {
private String name;
private int age;
public Human(String name, int age) {
this.name = name;
this.age = age;
}
public static void message() {
...
0
votes
1
answer
101
views
C++ - Hard Time With Polymorphism And Dynamic Casting [closed]
I'm trying to design an file system for a personal game engine I'm working on and I have a hard time using polymorphism while avoiding dynamic casting.
Problem
I have different kind of objects in my ...
1
vote
1
answer
48
views
How can I check if a generic class instance is actually an instance of a non-generic child?
I'm working on a card game in Unity right now.
The attributes for cards of different types are very similar, aside from the sets they can belong to, so I have them set up as sibling classes that ...
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;
...
1
vote
2
answers
96
views
Finding derived object in a vector of base
I found the following code in some project:
std::vector<Base*> objs_;
template <class TDerived>
T* Get() {
auto objIt = std::find_if(objs_.cbegin(), objs_.cend(), [](Base* it) {
...
0
votes
1
answer
130
views
C++ polymorphism and cast
I'm trying to figure out the behaviour of polymorphism with casting.
Using the code below, are you able to explain to me why the "child > parent > child" instructions sequence is OK, ...
0
votes
2
answers
317
views
Is upcasting really a casting?
I am confused about the term upcasting in c++ (for example here). Is it really a casting? For example when I cast a an int to a double, I expect the casted type to behave exactly as a double. However, ...
-2
votes
1
answer
75
views
Why is compareTo() receiving runtime error when comparing two objects?
When referencing the object "r2" at line 12, there is a runtime error. Why is this?
public static void main(String[] args) {
Rational r1 = new Rational(-2, 6);
System.out....
0
votes
0
answers
91
views
How to store reference to std::map object with different comparators?
I would like to create a variable that stores references to std::map objects. These map objects are of type std::map<int, int, Cmp> where Cmp could be either std::greater<int> or std::less&...
1
vote
2
answers
478
views
C# cast polymorphic object into specific type without knowing what that 'specific type' is
Thanks ahead, community!
As the title describes, I would like to cast an object that is in parent type to a child type, which is actually a child type, whilst this 'specific type' cannot be known ...
-1
votes
1
answer
49
views
Multi-type container C++. Casting to derived template class
I am trying to implement a multi-type container in C++ without using std::any, std::variant, boost::any, etc. The add() function adds new objects (int, string, or other Structures) by wrapping them in ...
0
votes
2
answers
119
views
Pass parent pointer in derived constructor
So I was doing something like this:
Base * pParentPtr
// ... pParentPtr is used
// Cast result pointer
Derived* castedResult = (Derived*)pParentPtr;
// Copy the referenced ...
1
vote
3
answers
230
views
What is/are the best way to handle primitive type polymorphism in c#?
What is/are the best way to handle primitive type polymorphism in c# ?
I'm stumbling on a lot of case where this is a question I have issues to answer in c#. I'm more proficient in java where it's ...