Questions tagged [downcast]

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

589 questions
2
votes
3 answers

static_cast on derived classes when base turns from not polymorphic to polymorphic

I am reviewing C++ casts operator and I have the following doubt: for polymorphic classes I I should use polymorphic_cast I should never use of static_cast since down-casting might carry to undefined behavior. The code compiles this case…
Abruzzo Forte e Gentile
  • 14,423
  • 28
  • 99
  • 173
2
votes
6 answers

Cant copy construction be done without creating an explicit function in the pure virtual base class?

My objective is to do a deep copy of a class, but a virtual class is causing trouble. #include using namespace std; class Vir//pure virtual class { public: virtual void hi()=0; }; class Handler:public Vir { public: int…
Nav
  • 19,885
  • 27
  • 92
  • 135
2
votes
2 answers

Is There a way to use dynamic_cast When Casting to a Child?

Say that I that I have these classes: struct Parent {}; struct Child : public Parent { void func() {} }; Now say that I wanted to create a function like this: void foo(Parent* arg) { auto child = dynamic_cast(arg); if(child !=…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
2
votes
4 answers

Best practices to implement a Payload-containing class in C++?

I have a question about hierarchy, references and pointers... The question comes to my mind when I had tried to do the following stuff: class packet { public: int address; int command; /**< Command select the type of Payload that I must…
oclaril
  • 33
  • 1
  • 9
2
votes
1 answer

How to downcast a shared_ptr in the case of virtual inheritance?

Suppose I have a class C, inheriting from B, itself virtually inheriting from A: class A {}; class B : virtual public A {}; class C : public B {}; Now suppose I have a std::shared_ptr managing an object which I know for sure is of some class…
Boris Dalstein
  • 7,015
  • 4
  • 30
  • 59
2
votes
2 answers

Fetch value from Exception

I am getting a NumberFormatException and i want to fetch the value from the exception itself , which looks like But there is no getter available for the value, can anyone suggest any solution to fetch this value rather than reflection, or even…
Anand Kadhi
  • 1,790
  • 4
  • 27
  • 40
2
votes
2 answers

Downcast detecting tool for C#

Working with legacy moderate size project. I have implemented one feature using Decorator pattern, and it works great except that it breaks crappy code that uses downcast from interface to implementation. The question is: is there any tool or…
capone
  • 728
  • 5
  • 17
2
votes
2 answers

C-style cast on a non-polymorphic type

Suppose I have a base struct FOO which is essentially a C-style struct: struct FOO { double bar1; int bar2; }; And a C++ style struct (which has member functions, no member data, but no v-table): struct bar : public FOO { double…
P45 Imminent
  • 8,319
  • 4
  • 35
  • 78
2
votes
1 answer

SWIFT2 : EXC_BAD_ACCESS down casting a class conforming to ErrorType

I have a simple Object hierarchy : RESTError with 2 attributes (httpCode and message), and 4 subclasses of it. One of the subclasses, RESTBusinessError has two additional fields. I've simplified my code here below, but a RESTError variable (which is…
2
votes
0 answers

How to forbid downcast from base to derived class in .NET?

Is there a way to deny downcast of Base to Derived when Base is holding an instance of Derived? By default it's allowed. Public Class Base End Class Public Class Derived Inherits Base End Class Public Class Program Public Shared Sub Main() …
2
votes
0 answers

Return statement skipped in Swift

I'm trying to teach myself Swift via the Stanford iTunes U course (currently working on the calculator app), but I just ran into a super weird issue that I've never seen before and can't figure out how to solve: one of my return statements (and only…
2
votes
3 answers

Why would down casting be a bad practice in C++ and not in another language?

I once asked a question about how to design a piece of C++ code (which can be found here C++ - Good or bad practice?) and people told me that down-casting is considered a bad practice except for very specific scenarios. However, I've seen many…
Virus721
  • 8,061
  • 12
  • 67
  • 123
2
votes
4 answers

How do I create a class out of a superclass without downcasting?

I'm using spring-data-neo4j and I have two node entities, Person and Owner extends Person When I save person, it gets the label :Person, and when I save an owner, it gets the labels :Owner and :Person. Excellent. Exactly what I wanted. Sometimes we…
sparkyspider
  • 13,195
  • 10
  • 89
  • 133
2
votes
3 answers

Why is this downcast not allowed in java?

Let's say I have a superclass of Animal, and a subclass of Dog. We can upcast by saying: Animal a = new Dog(); We CANNOT downcast by saying: Dog b = new Animal(); So I do understand that an animal does not HAVE to be a dog. But, why would having…
rb612
  • 5,280
  • 3
  • 30
  • 68
2
votes
4 answers

Shallow copying a list with downcasting

I have the class herichary as follows CEntity---->CNode--->CElement I have a class Nodes : List and Class Elements : List Node class contain common item common across different project Element class has item specific to a…
Mohit Vashistha
  • 1,824
  • 3
  • 22
  • 49