FAQ Database Discussion Community


What use does the new as! operator have?

swift,operators,downcasting
I don't quite understand what new functionality the as! operator is supposed to add. Apple's documentation says: The as! operator performs a forced cast of the expression to the specified type. The as! operator returns a value of the specified type, not an optional type. If the cast fails, a...

dynamic_cast “this” to derived type: when is it legal?

c++,inheritance,dynamic-cast,downcasting
Here is a code that obviously doesn't work, since downcasting "this" in a constructor is illegal: #include <cassert> class A { protected: virtual ~A() {} public: A(); }; class B : public A { }; A::A() { assert(dynamic_cast<B*>(this)); } int main(void) { B b; return 0; } As expected, when...

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

java,c#,c++,downcasting
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 pieces of code in C#...

Virtual destructor of base class is not called when downcasting is done via dynamic cast

c++,casting,downcasting
When I run the following code, I get the following issue. Deleting derived pointer d1 does not call the destructor of the base class. Deleting derived pointer d2 calls the base destructor. Why is the type of cast (dynamic or static) affecting whether base destructor is called or not. class...

C++ Avoiding down-casting or variants

c++,design,variant,downcasting,visitor-pattern
I've been facing a design issue for a while : I am parsing a source code string into a 1-dimensional array of token objects. Depending on the type of a token (litteral, symbol, identifier), it has some token-type-specific data. Litterals have a value, symbols have a symbol type, and identifiers...

adding method to derived class and calling it from base pointer

c++,inheritance,polymorphism,downcasting
Let's say I have the following class hierarchy: class A { public: virtual ~A(); }; class B : public A {}; class AUser { public: virtual ~AUser(); protected: A* m_pA; }; class BUser : public AUser {}; Now I want to add a method to B which is not relevant...

Swift Downcasting AnyObject (Float) to String

ios,string,swift,double,downcasting
I'm currently working with some dummy data, before I start using an actual API. But I'm having some problems filling one of my labels, with a number, that needs a symbol added to it. Rather than explaining everything, here's a screencap of an early design below: I have a double,...

casting base class to derived class via reflection

java,reflection,downcasting,downcast
Hi I have to construct an object from an object. Since the base class has more than 50 fields i dont want to do things like //obj1 is an instance of BaseClass DerivedClass obj2 = new DerivedClass(); obj2.setField1(obj1.getField1()); obj2.setField2(obj1.getField2()) .... so on As you see from the title i want...

Swift's pow() function won't accept Doubles as arguments

swift,generics,pow,downcasting,type-constraints
I created this infix operator ^^ as a substitute to using the pow function: infix operator ^^ { associativity left precedence 155 } func ^^ <T: IntegerLiteralConvertible>(left: T, right: T) -> T { return pow(left as Double, right as Double) } I used the IntegerLiteralConvertible protocol as a type constraint...

C# Upcasting / Polymorphism Issue

c#,polymorphism,downcasting,up-casting
I believe this question is fairly basic but I am having trouble finding an answer to this question. In C# let's say I have 3 classes: A, B, C B derives from A C derives from B Now, if I wanted a list or array of objects of type class...

Downcasting this in an abstract base-class, is there any way to force it?

c#,generics,inheritance,c#-3.0,downcasting
Is there anyway to force a downcast in the abstract base-class when the derived type is actually known there (due to complicated generics)? Right now my ugly workaround is to implement an abstract protected property This that simply return this... so the usual stuff about downcasting not being possible due...

Why downcast and then assign to base-class in C++?

c++,casting,dynamic-cast,downcasting
I have stumbled upon the following code structure and I'm wondering whether this is intentional or just poor understanding of casting mechanisms: struct AbstractBase{ virtual void doThis(){ //Basic implementation here. }; virtual void doThat()=0; }; struct DerivedA: public AbstractBase{ virtual void doThis(){ //Other implementation here. }; virtual void doThat(){ //...