downcast Questions
4
In order to print dataframes nicely using tabulate, so that NaN and NaT are printed as empty cells, I've been using this successfully:
print(tabulate(df.astype(object).fillna("")))
Now, ...
33
Solved
Is it possible to assign a base class object to a derived class reference with an explicit typecast?
Is it possible to assign a base class object to a derived class reference with an explicit typecast in C#?.
I have tried it and it creates a run-time error.
Mikkimiko asked 8/4, 2009 at 11:12
3
In my example:
At upcasting, shouldn't the second d.print() call print "base"?
Isn't it "d" derived object upcasted to a base class object?
And at downcasting, what advantages does it have?
Cou...
Gilbertina asked 30/1, 2016 at 12:59
7
Solved
I have my base class as follows:
class point //concrete class
{
... //implementation
}
class subpoint : public point //concrete class
{
... //implementation
}
How do I cast from a point objec...
Cranial asked 7/8, 2012 at 22:27
1
Solved
To my surprise, gcc 11.2 accepts this code, but only in C++20 mode:
struct Base {};
struct Derived : Base { int i; };
int f(Base& b) { return static_cast<Derived>(b).i; }
// ^~~~~~~ oops,...
Winepress asked 10/8, 2021 at 7:32
5
I am being very confused about dynamic_cast. Material from C++ Primer and cppreference(rule 5) can't help me understand. (cppreference is way much harder than the book and I read them both very car...
Blame asked 28/9, 2018 at 13:57
1
Consider the following classes:
template <class Derived>
class BaseCRTP {
private:
friend class LinkedList<Derived>;
Derived *next = nullptr;
public:
static LinkedList<D...
Steinke asked 6/4, 2020 at 13:32
6
I know that downcasting is basically casting parent class pointer or reference to the derived class reference or pointer and for that you use dynamic_cast operator. But i can hardly think of any ex...
Thirst asked 7/7, 2020 at 14:33
2
Solved
I have the following code:
use std::thread;
use std::panic;
pub fn main(){
thread::spawn(move || {
panic::catch_unwind(|| {
// panic!("Oh no! A horrible error.");
let s: Option<u32> = N...
Jobholder asked 25/2, 2017 at 15:56
6
Solved
I am new to C# (and OOP). When I have some code like the following:
class Employee
{
// some code
}
class Manager : Employee
{
//some code
}
Question 1: If I have other code that does this:
...
3
Solved
I've been playing around with Swift and discovered that when down casting an object to be inserted into a dictionary, I get a weird warning: Treating a forced downcast to 'String' as optional will ...
Phosphate asked 14/6, 2014 at 19:38
3
Solved
I have two classes - one which inherits from the other. I want to know how to cast to (or create a new variable of) the sub class. I have searched around a bit and mostly 'downcasting' like this se...
5
Solved
I have a class where it may be necessary to change the object to a descendent class further down the line. Is this possible? I know that one option is to return a copy of it but using the child cla...
Grantgranta asked 2/11, 2010 at 17:9
9
Solved
What is the difference between up-casting and down-casting with respect to class variable?
For example in the following program class Animal contains only one method but Dog class contains two met...
Caracole asked 1/5, 2014 at 18:30
2
Solved
The following code is almost exact replica from Apple Documentation and compiles without errors:
guard let firstItem = (rawItems! as? Array<Dictionary<String, Any>>)?.first else {
thr...
Zippora asked 12/8, 2018 at 4:1
2
Solved
pub struct WidgetWrap {
// ...
widget: RefCell<Box<Any>>,
}
At some point I want to cast Box<Any> to Box<WidgetTrait>
let mut cell = widget.borrow_mut();
let w = cell.d...
1
In the former version, to get a float value from a [String: Any] dictionary, I can use let float = dict["somekey"] as? Float, but in swift4.1, it doesn't work. It seems the type of dict["somekey"] ...
1
Solved
I have the following struct:
#[derive(Debug)]
pub struct Entry {
pub index: usize,
pub name: String,
pub filename_offset: u64,
pub entry_type: EntryType,
}
#[derive(Debug)]
pub enum EntryType...
Ammo asked 5/1, 2018 at 20:24
1
Solved
Someone asked this question about string appending. It's string s; s = s + 2; not compiling. People gave answers stating that operator+ is defined as a template function while operator+= is not, so...
Saunders asked 4/8, 2017 at 12:3
6
Solved
Consider the following code:
struct Base {};
struct Derived : public virtual Base {};
void f()
{
Base* b = new Derived;
Derived* d = static_cast<Derived*>(b);
}
This is prohibited by th...
Tamatave asked 20/9, 2011 at 12:10
1
Solved
Note that byte is an 8-bit type (uint8_t) and unsigned int is a 16-bit type (uint16_t).
The following doesn't produce the results that I expect. I expect it to underflow and the result to always b...
1
I'm trying to manipulate ASTs in Rust. There will be lots of manipulations, and I want my trees to be immutable, so to save time all references will be Rcs.
My tree nodes will look like this:
enu...
Prostomium asked 13/10, 2016 at 14:39
9
Solved
Given the following in Swift:
var optionalString: String?
let dict = NSDictionary()
What is the practical difference between the following two statements:
optionalString = dict.objectForKey("So...
Hierarchize asked 7/9, 2014 at 9:7
2
Solved
I am porting some Python to Rust as a learning exercise and need to take input from either a file or stdin. I keep a handle to my input in a struct so I thought I'd just make a Box<io::Read> ...
2
Solved
I'm trying to cast a base class object to a derived class object with dynamic_cast, but dynamic_cast returns null. Is it possible to downcast using dynamic_cast?
struct A {
virtual ~A() {}
};
st...
Louralourdes asked 12/5, 2016 at 12:46
1 Next >
© 2022 - 2024 — McMap. All rights reserved.