derived-class

Can a factory somehow determine all possible classes, by looking in a particular file?

余生长醉 提交于 2020-01-03 05:43:04
问题 Is it possible to create a Factory that generates all derived classes (and class names) at runtime, based on all the classes that are specified in a particular file? For example, given a specific file where users are constantly adding derived classes: class Laptop: public Computer { public: virtual void Run(){mHibernating = false;} virtual void Stop(){mHibernating = true;} private: bool mHibernating; // Whether or not the machine is hibernating }; class Desktop: public Computer { public:

Can I pass arguments to a base constructor from a derived class's default constructor?

≡放荡痞女 提交于 2020-01-02 02:43:07
问题 Suppose I have an abstract base class Deck: public abstract class Deck { public List<Card> cards; public Deck(string[] values, string[] suits) {...} ... } and a derived class EuchreDeck: public class EuchreDeck : Deck { string[] values = new string[] { "9", "10", "J", "Q", "K", "A" }; string[] suits = new string[] { "clubs", "spades", "hearts", "diamonds" }; public EuchreDeck() : base(values, suits) // Error. {} ... } I want the ability to instantiate EuchreDeck and have the two string arrays

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; }

How do derived class constructors work in python?

被刻印的时光 ゝ 提交于 2020-01-01 01:17:12
问题 I have the following base class: class NeuralNetworkBase: def __init__(self, numberOfInputs, numberOfHiddenNeurons, numberOfOutputs): self.inputLayer = numpy.zeros(shape = (numberOfInputs)) self.hiddenLayer = numpy.zeros(shape = (numberOfHiddenNeurons)) self.outputLayer = numpy.zeros(shape = (numberOfOutputs)) self.hiddenLayerWeights = numpy.zeros(shape = (numberOfInputs, numberOfHiddenNeurons)) self.outputLayerWeights = numpy.zeros(shape = (numberOfHiddenNeurons, numberOfOutputs)) now, I

Why is 'virtual' optional for overridden methods in derived classes?

佐手、 提交于 2019-12-31 19:59:05
问题 When a method is declared as virtual in a class, its overrides in derived classes are automatically considered virtual as well, and the C++ language makes this keyword virtual optional in this case: class Base { virtual void f(); }; class Derived : public Base { void f(); // 'virtual' is optional but implied. }; My question is: What is the rationale for making virtual optional? I know that it is not absolutely necessary for the compiler to be told that, but I would think that developers would

Virtual Table C++

淺唱寂寞╮ 提交于 2019-12-29 14:17:11
问题 I read a lot of people writing "a virtual table exists for a class that has a virtual function declared in it". My question is, does a vtable exists only for a class that has a virtual function or does it also exist for classes derived from that class. e.g class Base{ public: virtual void print(){cout<<"Base Print\n";} }; class Derived:public Base{ public: void print(){cout<<"Derived print\n";} }; //From main.cpp Base* b = new Derived; b->print(); Question: Had there been no vtable for class

How do I use an XmlSerializer to deserialize an object that might be of a base or derived class without knowing the type beforehand?

风格不统一 提交于 2019-12-28 11:53:44
问题 In C#, how do I use an XmlSerializer to deserialize an object that might be of a base class, or of any of several derived classes without knowing the type beforehand? All of my derived classes add additional data members. I've made a simple GUI that can serialize and deserialize class objects. It will serialize objects as whatever inherited class (or even just the base class) is appropriate based on which fields the user chooses to populate. I have no issues with the serialization; the

How do I use an XmlSerializer to deserialize an object that might be of a base or derived class without knowing the type beforehand?

☆樱花仙子☆ 提交于 2019-12-28 11:53:08
问题 In C#, how do I use an XmlSerializer to deserialize an object that might be of a base class, or of any of several derived classes without knowing the type beforehand? All of my derived classes add additional data members. I've made a simple GUI that can serialize and deserialize class objects. It will serialize objects as whatever inherited class (or even just the base class) is appropriate based on which fields the user chooses to populate. I have no issues with the serialization; the

Automatic counter for derived class / Alternative?

爷,独闯天下 提交于 2019-12-25 08:58:47
问题 Right now I have two last problem with the first part of my library. And the first one is this thing not possible in C++ without hack (if I want the constexpr version), it's a derived class counter : class FooBase { protected: static int Counter; }; class Foo : public FooBase { public: static const int Type; }; const int Foo::Type = ++FooBase::Counter; struct FooTest : public Foo {}; Must be in a source file: int FooBase::Counter = 0; Why I need this counter? Well I use it as a type and an