inheritance

Convert List of Base Class to a List of Derived Class - possible?

£可爱£侵袭症+ 提交于 2020-07-30 02:40:08
问题 I coworker of mine told me that it was possible to convert a List<BaseClass> to a List<DerivedClass> (they didn't show me a working example). I didn't think it was possible to convert a parent class into a child class (I do know however that it is possible to do it the other way around). I've seen several related questions with people saying it can't be done and people saying it can - none of the proposed solutions have worked for me at all. I always get a: System.InvalidCastException. Unable

How do I avoid code repetition when defining a class of functions that only vary the data type of the parameters they handle?

微笑、不失礼 提交于 2020-07-22 06:00:47
问题 I have a program that performs database operations using SQLite.SQLiteAsyncConnection. I have two different models I'd like to work with, say Customers and Cars. Customers could be defined as public class Customer { [PrimaryKey] public string Id {get; set;} public string Name {get; set;} public int NumberOfKids {get; set;} public decimal Budget {get; set;} } and Cars could be defined as public class Car { [PrimaryKey] public string Id {get; set;} public int Year {get; set;} public string Make

C# No implicit conversion to inherited type

耗尽温柔 提交于 2020-07-22 05:51:09
问题 I'm hoping someone can explain this to me as I'm confused about it. I have a series of inheritance amongst my classes and interfaces, with a factory to instantiated the correct object. However, I am getting a Cannot implicitly convert type 'Conn' to IConn. An explicit conversion exists (are you missing a cast?) Here is an trimmed down version of the code. public abstract class Id { public int Id { get; set; } } public abstract class Id_SourceId { public int SourceId { get; set; } } public

Inheritance of Encodable Class

时间秒杀一切 提交于 2020-07-18 09:05:20
问题 I am writing a program using Swift 4 and Xcode 9.2. I have faced difficulties with writing encodable class (exactly class, not struct). When I am trying to inherit one class from another, JSONEncoder does not take all properties from sub class (child). Please look at this: class BasicData: Encodable { let a: String let b: String init() { a = "a" b = "b" } } class AdditionalData: BasicData { let c: String init(c: String) { self.c = c } } let encode = AdditionalData(c: "c") do { let data = try

Why does C++ require me to repeat template arguments of my base class in initalizer list?

人盡茶涼 提交于 2020-07-16 01:34:49
问题 I was porting some code from MSVC(without permissive-) to linux and I learned that if you call constructor of a template base class in initializer list of your class you must specify all the template parameters or you get an error. Seems kind of redundant, since if you make a mistake in retyping the template parameters it is a hard error: error: type 'Base<int, true>' is not a direct or virtual base of 'Derived' full code here: template <typename T, bool has_x> struct Base { Base(T t): t_(t){

Initialize subclass within class in python

核能气质少年 提交于 2020-07-10 04:53:51
问题 I am initializing a class along with two subclasses in Python using a dictionary. Would it be possible to check a key in the dictionary within the init and depending the result initialize either one of the two subclasses? For instance: Class Pet(): def __init__(self,dic): self.name=dic['name'] self.age=dic['age'] if dic['type']=='dog': #Initialize a dog which inherits all pet methods with name and age passed onto it elif dic['type']=='cat': #Initialize a dog which inherits all pet methods

Placement new base subobject of derived in C++

断了今生、忘了曾经 提交于 2020-07-09 08:26:04
问题 Is it defined behavior to placement-new a trivially destructible base object of a derived? struct base { int& ref; }; struct derived : public base { complicated_object complicated; derived(int& r, complicated_arg arg) : base {r}, complicated(arg) {} }; unique_ptr<derived> rebind_ref(unique_ptr<derived>&& ptr, int& ref) { // Change where the `ref` in the `base` subobject of // derived refers. return unique_ptr<derived>(static_cast<derived*>( ::new (static_cast<base*>(ptr.release()) base{ref}))

Placement new base subobject of derived in C++

橙三吉。 提交于 2020-07-09 08:23:05
问题 Is it defined behavior to placement-new a trivially destructible base object of a derived? struct base { int& ref; }; struct derived : public base { complicated_object complicated; derived(int& r, complicated_arg arg) : base {r}, complicated(arg) {} }; unique_ptr<derived> rebind_ref(unique_ptr<derived>&& ptr, int& ref) { // Change where the `ref` in the `base` subobject of // derived refers. return unique_ptr<derived>(static_cast<derived*>( ::new (static_cast<base*>(ptr.release()) base{ref}))

Angular component Inheritance and inheriting subscriptions

折月煮酒 提交于 2020-07-08 09:31:13
问题 Let below is a component which I will be extending in various other components ti reuse some code.. import { Component } from '@angular/core'; @Component ({ selector: 'my-app', template: ` <div> <h1>{{appTitle}}</h1> <div>To Tutorials Point</div> </div> `, }) export class AppComponent { appTitle: string = 'Welcome'; ngOnInit(){ this.registerSomeSubscriptions(); } registerSomeSubscriptions(){ this.subscribeSomething.subscribe((data)=>{ performSomeAction(); }) } } I can extend it like below

error: Invalid use of incomplete type

試著忘記壹切 提交于 2020-07-08 03:13:49
问题 I got the following problem, does anyone have a good idea? class Vector_2d; namespace Utils { class Align_vector : public Vector_2d { protected: bool check_range(int x, int y); public: enum alignment {left, right, up, down}; Align_vector(Alignment alignment); void set_alignment(Alignment alignment); Alignment get_alignment(); }; } the error is: error: invalid use of incomplete type ‘class Vector_2d’ But how is there an error? 回答1: class Vector_2d; This only declares a class by that name exits