derived-class

Using inheritance in constructor (publix X () : y)

巧了我就是萌 提交于 2019-12-10 16:37:33
问题 I have just seen following code but I do not understand the derivation of base class right in the constructor declaration. What is this and is this possible with ordinal methods? public SplashAppContext(Form mainForm, Form splashForm) : base(splashForm) { this.mainForm = mainForm; splashTimer.Tick += new EventHandler(SplashTimeUp); splashTimer.Interval = 2000; splashTimer.Enabled = true; } 回答1: It's calling a base class constructor, passing the argument splashForm of the type Form to it. You

override List<baseClass> with List<derivedClass>

雨燕双飞 提交于 2019-12-10 14:07:52
问题 I have base classes like this: public class Scene { public IList<SceneModel> Models {get; set;} } public class SceneModel { } and derived classes like this: public class WorldScene : Scene { public override IList<WorldModel> Models {get; set;} } public class WorldModel : SceneModel { } So my question is, how do I manage this. As it stands the compiler isn't happy with this (and to be honest it looks a bit weird to me anyway). So is what I'm trying to do impossible? And if so, why? Or is it

Calling a function from a derived template class

风格不统一 提交于 2019-12-09 19:00:54
问题 My base class: //Element.h class Element { public: Element(); virtual ~Element(){}; // not sure if I need this virtual Element& plus(const Element&); virtual Element& minus(const Element&); }; Derived template class: //Vector.h #include "Element.h" template <class T> class Vector: public Element { T x, y, z; public: //constructors Vector(); Vector(const T& x, const T& y = 0, const T& z =0); Vector(const Vector& u); ... //operations Element& plus(const Element&) const; Element& minus(const

Using declaration (Derived class)

会有一股神秘感。 提交于 2019-12-08 19:44:04
问题 struct B1{ int d; void fb(){}; }; struct B2 : B1{ using B1::d; using B1::fb; int d; // why this gives error? void fb(){} // and this does not? }; int main(){} Is it because, B1::fb() is treated as B1::fb(B1*) and B2::fb() treated as B2::fb(B2*) ? That is, does the implicit parameter, help in distinguishing these? $13.3.1/4- For nonconversion functions introduced by a using-declaration into a derived class, the function is considered to be a member of the derived class for the purpose of

When should a virtual method be pure?

醉酒当歌 提交于 2019-12-08 17:34:54
问题 I have found some code that I am working on, and was wondering what the best design implementation is. If a base class defines a method as virtual, but implements an empty body as well, thus not requiring the derived classes to implement a body, should it not be made pure instead? virtual void AMethod1() {} // 1 virtual void AMethod2() {assert(false);} // 2 virtual void AMethod3() = 0; // 3 Current code. Idea1: Alerts user that this derived object has not implemented this method body. Idea2:

Using Visitor Pattern to detect intersection between two shapes

房东的猫 提交于 2019-12-08 13:09:02
问题 I realize this is a very specific question so it would be helpful if the answer people give includes explicit codes on how to do this. Thanks. I have an abstract base class Shape: class Shape { ..... virtual bool GetIntersection(Shape* _shape) = 0; } class Circle : public Shape {...} class Triangle : public Shape {...} Both these derived classes overrides GetIntersection; I have: //main.cpp .... Shape* shape; Shape* circle = new Circle; if(input == 0) shape = new Circle; else shape = new

Problem allocating derived class array with new

烂漫一生 提交于 2019-12-08 09:19:16
问题 I have a simple program $ cat a.cpp #include <iostream> class MyClass { public: virtual void check() { std::cout << "Inside MyClass\n"; } }; class MyClass2: public MyClass { public: int* a; virtual void check() { std::cout << "Inside MyClass2\n"; } }; int main() { MyClass *w, *v; w = new MyClass2[2]; v = new MyClass2; std::cout << "Calling w[0].check\n"; w[0].check(); std::cout << "Calling v->check\n"; v->check(); std::cout << "Calling w[1].check\n"; w[1].check(); } $ g++ a.cpp $ ./a.out

Class derived from Generic Repository

牧云@^-^@ 提交于 2019-12-08 08:26:51
问题 I have a Generic Repository class, see below, which is used to perform common Data Access functions, ie, Add, GetByID etc. public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class { internal GolfEntities context; internal DbSet<TEntity> dbSet; public GenericRepository(GolfEntities context) { this.context = context; this.dbSet = context.Set<TEntity>(); } public TEntity GetByID(object id) { return dbSet.Find(id); } I would like to create another Repository

How can I implement a Singleton class that can be derived from in WPF?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 08:15:02
问题 Some time ago I learned of the Singleton implementation that only permits a single instance of a class object by hiding the class initializer and using a private static reference of the object within itself, and a public GETTER that references that private reference - public class Foo : IDisposable{ private static Foo _Instance; public static Foo Instance{ get{ return Foo._Instance ?? new Foo(); } private Foo(){ Foo._Instance = this; } public void Dispose(){ Foo._Instance = null; } } I love

Sub-classing the argparse Argument Parser

↘锁芯ラ 提交于 2019-12-07 21:07:48
问题 I am trying to write a parser class derived from the Python argparse ArgumentParser class. The outlines of the following code work fine on the command line but generate an error I am struggling to understand in the context of my module. The code (stripped down a little to remove the unimportant stuff) is as follows: class SansParser(argparse.ArgumentParser): """Argument parser for preparing a SansModel fit or calculation """ def __init__(self): """Initialisation method for the parser class"""