Questions tagged [downcast]

Downcasting permits an object of a superclass type to be treated as an object of any subclass type.

589 questions
5
votes
1 answer

Why can't I downcast pointer to members in template arguments?

If I make a pointer-to-base-member, I can convert it to a pointer-to-derived-member usually, but not when used within a template like Buzz below, where the first template argument influences the second one. Am I fighting compiler bugs or does the…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
5
votes
3 answers

How do I down-cast a c++ object from a python SWIG wrapper?

The problem: I've wrapped some c++ code in python using SWIG. On the python side, I want to take a wrapped c++ pointer and down-cast it to be a pointer to a subclass. I've added a new c++ function to the SWIG .i file that does this down-casting, but…
SuperElectric
  • 17,548
  • 10
  • 52
  • 69
5
votes
1 answer

Does c++ guarantee down-casting grandmother base class to grand child class like curiously recurring template pattern?

I would like to know whether c++ guarantees down-casting grandmother base class to grand child class like curiously recurring template pattern. The following code works well in my environment. However I am not sure it works any…
mora
  • 2,217
  • 4
  • 22
  • 32
5
votes
2 answers

dynamic_cast "this" to derived type: when is it legal?

Here is a code that obviously doesn't work, since downcasting "this" in a constructor is illegal: #include class A { protected: virtual ~A() {} public: A(); }; class B : public A { }; A::A() { …
sunmat
  • 6,976
  • 3
  • 28
  • 44
5
votes
2 answers

How to downcast from non-polymorphic virtual base class?

Is there a way to downcast from a virtual base class to a derived class when there are no virtual functions involved? Here's some code to demonstrate what I'm talking about: struct Base1 { int data; }; struct Base2 { char…
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
5
votes
3 answers

How to force downcast on generics

Given the code below: class Animal { } class Dog : Animal { } class Cage { private T animal; public Cage(T animal) { this.animal = animal; } public T Animal { get { return animal;} } } class…
user1778378
  • 51
  • 1
  • 2
4
votes
2 answers

Is it safe to downcast objects (not pointers) to their known derived type?

Would there be a danger of slicing result Compare(const Osp::Base::Object &obj1, const Osp::Base::Object &obj2, int &cmp) const { cmp = ((const Block)obj1).NumSuperBlocks() - ((const Block)obj2).NumSuperBlocks(); } Where class Block : Object…
John
  • 6,433
  • 7
  • 47
  • 82
4
votes
1 answer

Lifetime error while attempting to downcast

I have a linked list of errors which I'm trying to traverse. Given MyError, which is a linked list of errors with an optional code, my goal is to traverse the chain and return the first non-None code: type BoxedError = Box
Shmoopy
  • 5,334
  • 4
  • 36
  • 72
4
votes
1 answer

How to downcast integer multiindex level?

I have a dictionary of large scale MultiIndex series where both index levels are datetime values. The abstract short example of one of it is: idx_level_0 = pd.date_range('2020-01-01', '2020-04-01', freq = 'M') idx_level_1 =…
igharok
  • 95
  • 3
4
votes
1 answer

In C++, is it possible to use CRTP with a private base?

In C++ I have many classes, unrelated by inheritance, that define a method std::string get_name() const. There are several utility functions that many classes need that are implemented in terms of get_name(). I would like classes that implement…
user1806566
  • 1,078
  • 6
  • 18
4
votes
5 answers

How to avoid downcasting when trying to extend a Java object

I get several objects of type Foo from a call to an external API. Locally I want to process those objects with a little added information so I have a subclass FooSon that adds those extra fields. How can I convert all those objects I get, to my new…
Omar Kohl
  • 1,372
  • 2
  • 15
  • 32
4
votes
2 answers

dynamic_cast in C++ doesn't work without having RTTI on; Why?

Virtual methods work without RTTI, shouldn't that mean there's enough data around at runtime to figure out which class it is? (i want to find out how i can downcast and then check if it was a pointer to the right type. i can't modify the classes…
Matthias
  • 105
  • 1
  • 9
4
votes
2 answers

How to access class specific methods without instanceof or getClass

Suppose I have the following interface and implementation: interface Weapon{ int attack(); } public class Sword implements Weapon { //Constructor, and Weapon interface implementation //... public void…
user8753793
4
votes
3 answers

Polymorphism and downcasting questions

I'm reading up a book on Java and currently on the Polymorphism topic as well as how to downcast a reference variable. However, I'm pretty stuck with understanding the concept of downcasting. Below is the uml for the example I'm following. For all…
Scorpiorian83
  • 469
  • 1
  • 4
  • 17
4
votes
2 answers

Downcasting a generic type in C# 3.5

Why can I only upcast a generic and not downcast it? How is it not clear to the compiler that if my constraint says where T : BaseClass and U is derived from BaseClass that (U)objectOfTypeT is valid?
the_drow
  • 18,571
  • 25
  • 126
  • 193