derived-class

C++ Access derived class member from base class pointer

一个人想着一个人 提交于 2019-11-26 22:33:08
问题 If I allocate an object of a class Derived (with a base class of Base ), and store a pointer to that object in a variable that points to the base class, how can I access the members of the Derived class? Here's an example: class Base { public: int base_int; }; class Derived : public Base { public: int derived_int; }; Base* basepointer = new Derived(); basepointer-> //Access derived_int here, is it possible? If so, then how? 回答1: No, you cannot access derived_int because derived_int is part of

In C# 4.0, is it possible to derive a class from a generic type parameter?

自作多情 提交于 2019-11-26 21:08:33
问题 I've been trying this, but I can't seem to figure this out. I want to do this... public abstract class SingletonType<TSingleton, TBaseClass> : TBaseClass where TSingleton : TBaseClass, new() where TBaseClass : class { static TSingleton _singleton; public static TSingleton Singleton => _singleton ?? (_singleton = new TSingleton()); } The plan was to use it like this which would sort of 'wrap' the singleton pattern around a base class... public class SingletonFoo : SingletonType<SingletonFoo,

Why does calling a method in my derived class call the base class method?

孤者浪人 提交于 2019-11-26 19:17:47
Consider this code: class Program { static void Main(string[] args) { Person person = new Teacher(); person.ShowInfo(); Console.ReadLine(); } } public class Person { public void ShowInfo() { Console.WriteLine("I am Person"); } } public class Teacher : Person { public new void ShowInfo() { Console.WriteLine("I am Teacher"); } } When I run this code, the following is outputted: I am Person However, you can see that it is an instance of Teacher , not of Person . Why does the code do that? Carsten There's a difference between new and virtual / override . You can imagine, that a class, when

Why we do create object instance from Interface instead of Class?

血红的双手。 提交于 2019-11-26 18:49:05
问题 I have seen many times an Interface instance generated from a class. Why does use an Interface in this way? An Interface instance created only itself with the help of the derived class and we can access only this interface members through this instance. How does this give an advantage? I'm so confused.. interface IPrint { void Print(); } class Sample : IPrint { public void Print() { Console.WriteLine("Print..."); } public void Sample() { Console.WriteLine("Sample..."); } } class Program {

Cast base class to derived class python (or more pythonic way of extending classes)

僤鯓⒐⒋嵵緔 提交于 2019-11-26 15:19:42
I need to extend the Networkx python package and add a few methods to the Graph class for my particular need The way I thought about doing this is simplying deriving a new class say NewGraph , and adding the required methods. However there are several other functions in networkx which create and return Graph objects (e.g. generate a random graph). I now need to turn these Graph objects into NewGraph objects so that I can use my new methods. What is the best way of doing this? Or should I be tackling the problem in a completely different manner? If you are just adding behavior, and not

Why doesn&#39;t a derived template class have access to a base template class&#39; identifiers?

久未见 提交于 2019-11-26 12:53:31
Consider: template <typename T> class Base { public: static const bool ZEROFILL = true; static const bool NO_ZEROFILL = false; } template <typename T> class Derived : public Base<T> { public: Derived( bool initZero = NO_ZEROFILL ); // NO_ZEROFILL is not visible ~Derived(); } I am not able compile this with GCC g++ 3.4.4 (cygwin). Prior to converting these to class templates, they were non-generic and the derived class was able to see the base class's static members. Is this loss of visibility in a requirement of the C++ spec or is there a syntax change that I need to employ? I understand that

Calling the base class constructor from the derived class constructor

我是研究僧i 提交于 2019-11-26 09:23:36
问题 I have a question: Say I have originally these classes which I can\'t change (let\'s say because they\'re taken from a library which I\'m using): class Animal_ { public: Animal_(); int getIdA() { return idA; }; string getNameA() { return nameA; } private: string nameA; int idA; } class Farm { public : Farm() { sizeF=0; } Animal_* getAnimal_(int i) { return animals_[i]; } void addAnimal_(Animal_* newAnimal) { animals_[sizeF]=newAnimal; sizeF++; } private: int sizeF; Animal_* animals_[max]; }

Why does calling a method in my derived class call the base class method?

狂风中的少年 提交于 2019-11-26 06:52:28
问题 Consider this code: class Program { static void Main(string[] args) { Person person = new Teacher(); person.ShowInfo(); Console.ReadLine(); } } public class Person { public void ShowInfo() { Console.WriteLine(\"I am Person\"); } } public class Teacher : Person { public new void ShowInfo() { Console.WriteLine(\"I am Teacher\"); } } When I run this code, the following is outputted: I am Person However, you can see that it is an instance of Teacher , not of Person . Why does the code do that?

Cast base class to derived class python (or more pythonic way of extending classes)

心不动则不痛 提交于 2019-11-26 03:54:40
问题 I need to extend the Networkx python package and add a few methods to the Graph class for my particular need The way I thought about doing this is simplying deriving a new class say NewGraph , and adding the required methods. However there are several other functions in networkx which create and return Graph objects (e.g. generate a random graph). I now need to turn these Graph objects into NewGraph objects so that I can use my new methods. What is the best way of doing this? Or should I be

Why doesn&#39;t a derived template class have access to a base template class&#39; identifiers?

被刻印的时光 ゝ 提交于 2019-11-26 03:07:46
问题 Consider: template <typename T> class Base { public: static const bool ZEROFILL = true; static const bool NO_ZEROFILL = false; } template <typename T> class Derived : public Base<T> { public: Derived( bool initZero = NO_ZEROFILL ); // NO_ZEROFILL is not visible ~Derived(); } I am not able compile this with GCC g++ 3.4.4 (cygwin). Prior to converting these to class templates, they were non-generic and the derived class was able to see the base class\'s static members. Is this loss of