derived-class

How do I call a derived class method from the base class?

佐手、 提交于 2019-12-01 02:22:01
I have read several similar questions about this but none seem to solve the problem I am facing. The typical answer is to cast as the derived class but I cannot since I do not know the derived class type. Here is my example: class WireLessDevice { // base class void websocket.parsemessage(); // inserts data into the wireless device object } class WiFi : WireLessDevice { // derived class GPSLoc Loc; } Wireless Device can also be derived to make Bluetooth, Wi-Max, Cellular, etc. devices and thus I do not know which type of wirelessdevice will be receiving the data. When a GPS packet is received

Does a derived class object contain a base class object?

杀马特。学长 韩版系。学妹 提交于 2019-12-01 01:25:56
Consider the following sample code below: #include <iostream> using namespace std; class base { public: base() { cout << "ctor in base class\n"; } }; class derived1 : public base { public: derived1() { cout <<"ctor in derived class\n"; } }; int main() { derived1 d1obj; return 0; } Questions When d1obj is created, the constructors are invoked in the order of derivation : base class constructor is called first and then the derived class constructor. Is this done because of the following reason : In-order to construct the derived class object the base class object needs to be constructed first ?

Avoid dynamic_cast with derived classes (Cast Derived class)

旧时模样 提交于 2019-12-01 00:39:33
I am new to C++ and came to a point, where I generate an overhead with classes. I have a QTcpSocket and read messages from it and create objects, for example MessageJoin, MessagePart, MessageUserData etc. I send these objects to my client and display them (+ do some UI updating). Now here comes my problem. I tested a few design techniques but all of them are not that nice: Pass each parameter of a message object in a signal/slot connection to the client - small overhead but not that good-looking Create a method for each Message-Type (messageJoinReceived, messageNoticeReceived etc.) Create one

How do I call a derived class method from the base class?

和自甴很熟 提交于 2019-11-30 21:54:34
问题 I have read several similar questions about this but none seem to solve the problem I am facing. The typical answer is to cast as the derived class but I cannot since I do not know the derived class type. Here is my example: class WireLessDevice { // base class void websocket.parsemessage(); // inserts data into the wireless device object } class WiFi : WireLessDevice { // derived class GPSLoc Loc; } Wireless Device can also be derived to make Bluetooth, Wi-Max, Cellular, etc. devices and

Avoid dynamic_cast with derived classes (Cast Derived class)

北慕城南 提交于 2019-11-30 20:05:07
问题 I am new to C++ and came to a point, where I generate an overhead with classes. I have a QTcpSocket and read messages from it and create objects, for example MessageJoin, MessagePart, MessageUserData etc. I send these objects to my client and display them (+ do some UI updating). Now here comes my problem. I tested a few design techniques but all of them are not that nice: Pass each parameter of a message object in a signal/slot connection to the client - small overhead but not that good

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

梦想的初衷 提交于 2019-11-30 12:39:02
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 approximation of the above, but just for comparison: >>> class MyUni(object): ... def __new__(cls, s): ... return

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 =

How to partially specialize a class template for all derived types?

a 夏天 提交于 2019-11-30 03:12:02
问题 I want to partially specialize an existing template that I cannot change ( std::tr1::hash ) for a base class and all derived classes. The reason is that I'm using the curiously-recurring template pattern for polymorphism, and the hash function is implemented in the CRTP base class. If I only want to partially specialize for a the CRTP base class, then it's easy, I can just write: namespace std { namespace tr1 { template <typename Derived> struct hash<CRTPBase<Derived> > { size_t operator()

Virtual Table C++

为君一笑 提交于 2019-11-29 22:25:06
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 derived then the output would not have been "derived print". So IMO there exists a vtable for any class