base-class

Call derived class method from base class reference

时间秒杀一切 提交于 2019-11-30 09:03:01
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 derived. The method Material::foo() represents a method in Material that is adequate for most materials, but

how to get derived class name from base class

≯℡__Kan透↙ 提交于 2019-11-30 08:08:08
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='i5', label_msgid='label_pub_city')) class Manager(BaseContent): def get_name(self): catalog =

ASP.NET MVC: Ignore custom attribute in a base controller class

≡放荡痞女 提交于 2019-11-30 07:39:49
问题 I have a number of Controllers in my project that all inherit from a controller I've named BaseController. I wrote a custom attribute that I applied to the entire BaseController class, so that each time an action runs in any of my controllers, that attribute will run first. The problem is that I have a couple of controller actions that I'd like to ignore that attribute, but I don't know how to do it. Can anyone help? I'm using MVC 1. Thanks. 回答1: I had a similar need for something like this

C++: Accessing parent methods and variables

安稳与你 提交于 2019-11-30 02:37:46
问题 In which way should I access this parent method and parent variable? class Base { public: std::string mWords; Base() { mWords = "blahblahblah" } }; class Foundation { public: Write( std::string text ) { std::cout << text; } }; class Child : public Base, public Foundation { DoSomething() { this->Write( this->mWords ); // or Foundation::Write( Base::mWords ); } }; Thanks. Edit: And what if there is ambiguity? 回答1: The two syntaxes you use in your code ( this->... and qualified names) are only

Call a C++ base class method automatically

孤街醉人 提交于 2019-11-30 02:37:40
问题 I'm trying to implement the command design pattern, but I'm stumbling accross a conceptual problem. Let's say you have a base class and a few subclasses like in the example below: class Command : public boost::noncopyable { virtual ResultType operator()()=0; //Restores the model state as it was before command's execution. virtual void undo()=0; //Registers this command on the command stack. void register(); }; class SomeCommand : public Command { virtual ResultType operator()(); //

Why use base class pointers for derived classes

▼魔方 西西 提交于 2019-11-29 19:28:00
class base{ ..... virtual void function1(); virtual void function2(); }; class derived::public base{ int function1(); int function2(); }; int main() { derived d; base *b = &d; int k = b->function1() // Why use this instead of the following line? int k = d.function1(); // With this, the need for virtual functions is gone, right? } I am not a CompSci engineer and I would like to know this. Why use virtual functions if we can avoid base class pointers? The power of polymorphism isn't really apparent in your simple example, but if you extend it a bit it might become clearer. class vehicle{ .....

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# protected members accessed via base class variable [duplicate]

自古美人都是妖i 提交于 2019-11-29 13:19:40
This question already has an answer here: Why can't I access C# protected members except like this? 7 answers It may seems rather newbie question, but can you explain why method Der.B() cannot access protected Foo via Base class variable? This looks weird to me: public class Base { protected int Foo; } public class Der : Base { private void B(Base b) { Foo = b.Foo; } // Error: Cannot access protected member private void D(Der d) { Foo = d.Foo; } // OK } Thanks! This is a frequently asked question. To figure out why this is illegal, think about what could go wrong. Suppose you had another

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=

Why does the compiler select the base class constructor inside the template argument list?

北城余情 提交于 2019-11-29 06:00:19
Follow-up question to this one . Basically, in the following code, why does the compiler think that the B inside A<B> in C s constructor refer to the (inaccessible) constructor of the B base class? struct B{}; template <typename T> struct A : private T{}; struct C : public A<B>{ C(A<B>); // ERROR HERE }; Live example on Ideone. Output: prog.cpp:1:9: error: 'struct B B::B' is inaccessible prog.cpp:7:7: error: within this context Note that the same error pops up if you change the constructor argument to A<B*> , A<B&> or even A<const B> . Also note that three of MSVC10, GCC 4.7 and Clang 3.1 ToT