derived-class

Python print isn't using __repr__, __unicode__ or __str__ for unicode subclass?

你。 提交于 2019-11-29 18:24:22
问题 Python print isn't using __repr__ , __unicode__ or __str__ for my unicode subclass when printing. Any clues as to what I am doing wrong? Here is my code: Using Python 2.5.2 (r252:60911, Oct 13 2009, 14:11:59) >>> class MyUni(unicode): ... def __repr__(self): ... return "__repr__" ... def __unicode__(self): ... return unicode("__unicode__") ... def __str__(self): ... return str("__str__") ... >>> s = MyUni("HI") >>> s '__repr__' >>> print s 'HI' I'm not sure if this is an accurate

Call derived class method from base class reference

我怕爱的太早我们不能终老 提交于 2019-11-29 14:05:35
问题 class Material { public: void foo() { cout << "Class Material"; } }; class Unusual_Material : public Material { public: void foo() { cout << "Class Unusual_Material"; } }; int main() { Material strange = Unusual_Material(); strange.foo(); //outputs "Class Material" return 0; } I would like for this to result in the "Class Unusual_Material" being displayed to the console. Is there a way I can achieve this? In my program I have a class Material from which other more specific materials are

C# accessing protected member in derived class [duplicate]

浪子不回头ぞ 提交于 2019-11-29 13:37:28
This question already has an answer here: Why can't I access C# protected members except like this? 7 answers I wrote the following code: public class A { protected string Howdy = "Howdy!"; } public class B : A { public void CallHowdy() { A a = new A(); Console.WriteLine(a.Howdy); } } Now, in VS2010 it results in the following compilation error: Cannot access protected member 'A.a' via a qualifier of type 'A'; the qualifier must be of type 'B' (or derived from it). This seems quite illogical to me - why can't I access the protected field of the class instance from a method of the class, which

how to get derived class name from base class

梦想与她 提交于 2019-11-29 10:41:04
问题 I have a base class Person and derived classes Manager and Employee . Now, what I would like to know is the object created is Manager or the Employee . The person is given as belows: from Project.CMFCore.utils import getToolByName schema = getattr(Person, 'schema', Schema(())).copy() + Schema((TextField('FirstName', required = True, widget = StringWidget(label='First Name', i18n_domain='project')), TextField('Last Name', required = True, widget = StringWidget(label='Last Name', i18n_domain=

How to access protected method in base class from derived class?

让人想犯罪 __ 提交于 2019-11-29 09:59:41
Here is a sample of code that annoys me: class Base { protected: virtual void foo() = 0; }; class Derived : public Base { private: Base *b; /* Initialized by constructor, not shown here Intended to store a pointer on an instance of any derived class of Base */ protected: virtual void foo() { /* Some implementation */ }; virtual void foo2() { this->b->foo(); /* Compilator sets an error: 'virtual void Base::foo() is protected' */ } }; How do you access to the protected overrided function? Thanks for your help. :o) Protected members in a base-class are only accessible by the current object. Thus,

Get Caller derived-class when calling a base-class static method

别等时光非礼了梦想. 提交于 2019-11-29 07:47:18
I was wondering if it's possible (even via reflection et similia) to get the caller derived-class inside of a called base-class static method. For example, I've a base-class with a static method defined: public MyBaseClass { public static void MyBaseClassStaticMethod() { /** ... **/ } } and a derived-from-it class: public MyDerivedClass : MyBaseClass { } then I call: MyDerivedClass.MyBaseClassStaticMethod() Is it possibile, inside of method MyBaseClassStaticMethod , to know which is the caller derived type ? (i.e. MyDerivedClass ) I just need a string... No, this is not possible - by no means.

Pointer to array of base class, populate with derived class

◇◆丶佛笑我妖孽 提交于 2019-11-29 07:31:43
问题 If I have a base class, with only virtual methods and 2 derived classes from the base class, with those virtual methods implemented. How do I: // causes C2259 BaseClass* base = new BaseClass[2]; BaseClass[0] = new FirstDerivedClass; BaseClass[1] = new SecondDerivedClass; or: // causes "base is being used without being initialized" BaseClass* base; // causes CC59 again BaseClass* base = new BaseClass; base[0] = FirstDerivedClass(); base[1] = SecondDerivedClass(); (or something similar) ...so

C#: How do I call a static method of a base class from a static method of a derived class?

帅比萌擦擦* 提交于 2019-11-29 03:25:19
In C#, I have base class Product and derived class Widget. Product contains a static method MyMethod(). I want to call static method Product.MyMethod() from static method Widget.MyMethod(). I can't use the base keyword, because that only works with instance methods. I can call Product.MyMethod() explicitly, but if I later change Widget to derive from another class, I have to revise the method. Is there some syntax in C# similar to base that allows me to call a static method from a base class from a static method of a derived class? static methods are basically a method to fallback from object

How to resolve “pure virtual method called”

拈花ヽ惹草 提交于 2019-11-28 20:33:43
I understand why this is happening, but I'm stuck trying to resolve it...here is what my code is doing when the error is generated (thus, leading to a crash) when my program exits... pure virtual method called SomeClass::~SomeClass() { BaseClassObject->SomePureVirtualMethod(this); } void DerivedClass::SomePureVirtualMethod(SomeClass* obj) { //Do stuff to remove obj from a collection } I never have a call to new SomeClass but I have a QList<SomeClass*> which I append SomeClass* objects to. The purpose of this destructor in SomeClass is to tell DerivedClass to remove a specific instance of

Virtual method called from derived instead of base

偶尔善良 提交于 2019-11-28 13:54:14
Can someone explain to me why is the overridden method being called when I cast the class into the base one: class Base { public virtual void VirtualMethod() { Console.WriteLine("Base virtual method"); } } sealed class Derived : Base { public override void VirtualMethod() { Console.WriteLine("Overriden method"); } } static void Main(String[] args) { Derived d = new Derived(); ((Base)d).VirtualMethod(); } I mean this code prints: Overriden method and not Base virtual method Its a run-time or compile-time future? I know i can call the Base's virtual method from the derived by calling base