Questions tagged [downcast]

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

589 questions
0
votes
1 answer

Can I Initialize a derived class reference with a base class reference to derived class instance?

I have something like the following: class A { ... }; class B : public A { ... }; // ... B b; const A& aref(b); // ... const B& bref(aref); and when I compile, I get: no suitable user-defined conversion from "const A" to "const B" exists Now,…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
0
votes
0 answers

ostream to ostringstream(downcasting) in C++ with Qt & MVC

I need to convert a ostream's object in a ostringstream's object. This is the code: //gerarchia_view.h class bagaglio_view{ private: std::ostream& os; std::string data; public: bagaglio_view(); std::string print_bagaglio(const…
BottCode
  • 31
  • 11
0
votes
3 answers

C# How to go from base class to derived one and not violate encapsulation

The question of downcasting has been raised on SO before, but I couldn't find any explanation on how to avoid it properly. Consider this hierarchy, for example. abstract class NetworkDevice { //some common fields & properties } class Router :…
Korli
  • 478
  • 4
  • 9
0
votes
0 answers

Unable to cast object of type 'Parent' to type 'Child'

I was trying to cast my parent into my child (aka Down casting). Below is the example for the same: public class Parent { public void ParentMethod() { Console.WriteLine("In Parent Method"); } } public class Child : Parent { …
HarshSharma
  • 630
  • 3
  • 9
  • 34
0
votes
3 answers

Swift function overriding Objective-C method

I have an Objective-C method (declared in the class named "BaseViewModel" of my old framework) that is : -(void) updateFromManagedObject:(__kindof NSManagedObject *)entity; I want to use it in mutiple Swift classes. Each Swift class will use a…
QLag
  • 823
  • 1
  • 13
  • 33
0
votes
2 answers

"downcast from CLplacemark? to Clplacemark only unwraps optional"(swift2)

how to fix this error : "downcast from CLplacemark? to Clplacemark only unwraps optional" so i use this code : if let p = CLPlacemark(placemark: placemarks.first as? CLPlacemark){} and change placemarks[0] array but not work and my code is :…
user1707895
  • 13
  • 1
  • 4
0
votes
3 answers

How to downcast UIViewController in Swift

I have several view controllers inherited from UIViewController. The views are embedded in Tab bar controller. I implement a custom view transition controller so that view can be switched when I tap on tabbar item. Is it possible that I can know…
thsieh
  • 620
  • 8
  • 24
0
votes
1 answer

Declaring first, downcasting later

Say I have BaseClass, DerivedClassOne, DerivedClassTwo, a magical factory method giveMeAnObjectOfType(type: String) -> BaseClass, and the following code func myBeautifulFunction(index: Int) -> BaseClass { let type : String =…
Morpheu5
  • 2,610
  • 6
  • 39
  • 72
0
votes
1 answer

How do I downcast a class to its base type to set values using Activator.CreateInstance?

Given //all types of T inherit class name of BaseClass... public void Test(Action CallBack){ var obj = (T) Activator.CreateInstance(); //Debugger shows obj of proper type and shows its proper baseclass //now I want to change the base…
JWP
  • 6,672
  • 3
  • 50
  • 74
0
votes
2 answers

Can you downcast a NSIndexPath to NSInterger

I have a AVPlayer class that I'm using in a detail view that takes a indexPath of type Int. I am getting the indexPath from the table view tableViewDidSelectAtIndexPath before sending it to the detail VC to use. I tried to just simply downcast the…
user4671001
  • 309
  • 2
  • 4
  • 12
0
votes
1 answer

Downcast from '[PFObject]?' to '[PFObject]' only unwraps optionals

I am saving a parse query to a array but i ket the following error on if let objects = objects as? [PFObject] And the following error happens Downcast from '[PFObject]?' to '[PFObject]' only unwraps optionals. any one know how to solve this? func…
user4174219
  • 427
  • 5
  • 13
0
votes
1 answer

Can not cast from source type to destination type

I am writing a game in Unity, and I am trying to use polymorphism to access 2 subclasses but keep getting an error: Can not cast from source type to destination type I have a serialized list of Places, and I am trying to downcast to a Property,…
0
votes
2 answers

swift variable type won't change after downcast if the variable has been declared?

I intended to downcast a UITableViewCell to different subclasses based on which section it is in. Suppose a subclass of UITableViewCell is CellOne and it has a var nameLabel. In one case, I downcast (as!) the UITableViewCell returned by…
ninikikki
  • 3
  • 2
0
votes
1 answer

Same multiple downcasts in equals; will they be optimised?

I tend to write my equals method in Java as a one liner... class Test { private String a = ""; private Integer b = Integer.MIN_VALUE; private Long c = Long.MIN_VALUE; public Test(final String a, final Integer b, final Long c) { …
Jagger
  • 10,350
  • 9
  • 51
  • 93
0
votes
0 answers

Coredata downcasting issue

I've the following code which is for editing my contact, but somehow I'm receiving this error: NSString is not a subclass of category. This is my code func editContact() { let index = pickerView.selectedRowInComponent(0) let…