inheritance

What's the advantage of this indirect function call?

若如初见. 提交于 2020-01-01 09:07:13
问题 I found the following code in a library: class Bar { public: bool foo(int i) { return foo_(i); } private: virtual bool foo_(int i) = 0; }; Now I'm wondering: Why would you use this indirection? Could there be any reasons why the above would be better than the simple alternative: class Bar { public: virtual bool foo(int i) = 0; }; 回答1: This is the Non-Virtual Interface Idiom (NVI). That page by Herb Sutter has a good bit of detail about it. However, temper what you read there with what the C++

ES6 modules and inheritance

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-01 08:54:41
问题 I have the following JavaScript files: src/js/classes/Lexus.js: import {Car} from 'src/js/classes/Car'; export class Lexus extends Car { constructor() { super("Lexus"); } } src/js/classes/Mercedes.js: import {Car} from 'src/js/classes/Car'; export class Mercedes extends Car { constructor() { super("Mercedes"); } } src/js/classes/Car.js: import {Lexus} from 'src/js/classes/Lexus'; //either of those imports works, but not both! import {Mercedes} from 'src/js/classes/Mercedes'; //either of those

ES6 modules and inheritance

大憨熊 提交于 2020-01-01 08:54:19
问题 I have the following JavaScript files: src/js/classes/Lexus.js: import {Car} from 'src/js/classes/Car'; export class Lexus extends Car { constructor() { super("Lexus"); } } src/js/classes/Mercedes.js: import {Car} from 'src/js/classes/Car'; export class Mercedes extends Car { constructor() { super("Mercedes"); } } src/js/classes/Car.js: import {Lexus} from 'src/js/classes/Lexus'; //either of those imports works, but not both! import {Mercedes} from 'src/js/classes/Mercedes'; //either of those

How to subclass an array class in Swift?

♀尐吖头ヾ 提交于 2020-01-01 08:42:00
问题 I am new in Swift. I have a base class: class foo{} I want to implement a foo collection class: class foos: Array<foo>{} But the compiler is complaining: inheritance from a non-protocol, non-class type of 'Array' I have tried other syntax (e.g. [foo] and NSMutableArray<foo> ) but none of them passes the compiler check. This should be simple but I have googled all day and could not figure it out. Does anyone know if it is possible and if it is, the correct syntax? 回答1: Swift's Array type is a

Extending enum in derived classes

亡梦爱人 提交于 2020-01-01 08:35:36
问题 I have a class hierarchy, and each class in it has an exception class, derived in a parallel hierarchy, thus... class Base { }; class Derived : public Base { }; class BaseException : public std::exception { enum {THIS_REASON, THAT_REASON}; }; class DerivedException : public BaseException { // er...what? }; I would like, in the DerivedException class, to extend the enumeration type to include a new value THE_OTHER_REASON, so that the DerivedException class could hold any of the three values.

taking advantage of inheritance in Controllers and Views

佐手、 提交于 2020-01-01 08:27:53
问题 I had posted this review on codereview.stackexchange.com a while ago... I feel it may be more suitable for stackoverflow, since it is more of a question than a code review. Its going to take a bit of explanation, please bear with me . I am developing an e-commerce website in ASP.NET MVC. Users can post advertisements of different types on the site. I am using inheritance to define my Ad types, and this question is about taking advantage of the hierarchical structure to remove repeated code in

How to Get Base Class Instance from a Derived Class

戏子无情 提交于 2020-01-01 08:05:42
问题 I don't know if this is possible, but I am trying to get the Base Class instance from a Derived Class. In C#, I can use the base keyword to access properties and methods of the Base Class (of course), but I want to use base itself. Attempting to do so results in a "Use of keyword 'base' is not valid in this context" error. Example Code public class SuperParent { public int SPID; public SuperParent() { } } public class SubChild : SuperParent { public SubChild(int pSPID) { base.SPID = pSPID; }

How to Get Base Class Instance from a Derived Class

喜欢而已 提交于 2020-01-01 08:04:18
问题 I don't know if this is possible, but I am trying to get the Base Class instance from a Derived Class. In C#, I can use the base keyword to access properties and methods of the Base Class (of course), but I want to use base itself. Attempting to do so results in a "Use of keyword 'base' is not valid in this context" error. Example Code public class SuperParent { public int SPID; public SuperParent() { } } public class SubChild : SuperParent { public SubChild(int pSPID) { base.SPID = pSPID; }

Entity Framework 4.1 Code First: Get all Entities with a specific base class

拜拜、爱过 提交于 2020-01-01 05:13:12
问题 I have a DbContext with set up different DbSet<T> s with all types that derive from the same base class: public class Foo : Entity { } public class Bar : Entity { } MyDbContext : DbContext { public DbSet<Foo> Foos { get; set; } public DbSet<Bar> Bars { get; set; } } Is it possible to get all entities which have the Entity base class in one query, like: DbContext.Set<Entity>(); // doesn't work I tried to introduce an explicit DbSet<Entity> to the DbContext, but that results in one big table

How to disable designer in derived classes in following generations

夙愿已清 提交于 2020-01-01 04:51:06
问题 In order to disable component designer in classes it is simple to add just [System.ComponentModel.DesignerCategory("")] attribute to it, however it does not work for any classes derived from this class in any generation. E.g: [System.ComponentModel.DesignerCategory("")] public class A:ServiceBase { } //Designer is disabled here public class B:A {} //Designer is enabled here [System.ComponentModel.DesignerCategory("")] public class B:A {} //Designer is enabled here too [System.ComponentModel