inheritance

C++ Inheritance & composition used together

≯℡__Kan透↙ 提交于 2019-12-25 10:51:09
问题 I have class A, having methods that update properties of B. So, I need inheritance. class B{ public: int x; }; class A : public B{ public: void update(int y){ x = y; } }; I also want to reach function "update" through class B, so I also need composition. class B{ public: A a; int x; }; class A : public B{ public: void update(int y){ x = y; } }; I want my structure as such, so that I want to track properties of objects-type Bs this way: ... B.a.update(5); int n = B.x; However, I cannot use

Overriding function called by base class?

喜欢而已 提交于 2019-12-25 09:46:56
问题 I have a class that is designed to be general-purpose, used wherever, that looks kinda like this: class FixedByteStream { public: FixedByteStream(const char* source) { size = strlen(source); copy(source); } /* Many more constructors here */ protected: void copy(const char* source) { address = allocate(); //... } /* Plus other functions that call allocate() */ char* FixedByteStream::allocate() { return (char*)malloc(size); } } I have then extended this class, so that it can use a project

Overriding function called by base class?

99封情书 提交于 2019-12-25 09:46:47
问题 I have a class that is designed to be general-purpose, used wherever, that looks kinda like this: class FixedByteStream { public: FixedByteStream(const char* source) { size = strlen(source); copy(source); } /* Many more constructors here */ protected: void copy(const char* source) { address = allocate(); //... } /* Plus other functions that call allocate() */ char* FixedByteStream::allocate() { return (char*)malloc(size); } } I have then extended this class, so that it can use a project

Flask-SQLAlchemy Single Table Inheritance

时光总嘲笑我的痴心妄想 提交于 2019-12-25 09:39:13
问题 SQLAlchemy support single table inheritance. I have structure like: class User(db.Model): __tablename__ = 'tbl_user' type = db.Column(db.String(32)) ... __mapper_args__ = { 'polymorphic_identity': 'user', 'polymorphic_on': type, 'with_polymorphic': '*' } class Tourist(User): __mapper_args__ = { 'polymorphic_identity': 'tourist' } ... class Guide(User): __mapper_args__ = { 'polymorphic_identity': 'guide' } ... When I try to run code, i get error like: sqlalchemy.exc.InvalidRequestError: Table

C# inheritance - save reference to last instance

天大地大妈咪最大 提交于 2019-12-25 09:30:54
问题 I just came up with a really odd problem and I wasn't able to figure out how to solve it. I have 3 classes, the class A is the base for B and C, that is: class A { ... } class B : A { ... } class C : B { ... } Now I would like to have a static property in these classes that stores the last object of each classes created, for example: class A { static public A lastInstance; } class B : A { public B() { lastInstance = this; } } class C : A { public C() { lastInstance = this; } } What I would

Inheriting QSerialPort

早过忘川 提交于 2019-12-25 08:43:11
问题 Perhaps a rather silly and newbie question but I have been struggling with keeping my QSerialPort serial; being used within the entirety of the application I am making. (Ughh this is frustrating to explain) To be more clear I have the aforementioned QSerialPort serial; established in my MainWindow.cpp , but as I transition to another form which has a different class (for example operations.cpp ) I am unsure on how to keep and use my serial.* functions. My mainwindow.cpp form is just a

Refactoring a class which inherited from a std::container

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 08:22:54
问题 I've a medium chunk of code containing 5 classes that they're all inherited from standard containers. For example: class Step : public std::vector<unsigned int> { public: friend std::ostream& operator<<(std::ostream& outStream, const Step& step); Step& operator =(const Step& rhv); static Step fromString(const std::string &input); std::string name; }; I know it's a bad idea to inherit from standard containers, so I'm going to remove all inheritances by adding a subobject of parent datatype:

Inheritence using java generics not working as expected

大兔子大兔子 提交于 2019-12-25 07:50:19
问题 I am trying to use inheritence and generics to create my application, but it doesn't seem to work the way I expect it to. I'll show you what I mean (TL;DR at the bottom): public interface IModel extends Serializable { public int save(); public void update(); public void delete(); } // <T> is a JPA annotated entity/class @SuppressWarnings("serial") public abstract class Model<T> implements IModel { private final Repository<T> _repository; protected T _entity; public Model(T entity, Repository

Not defining Virtual method in derived class as well

十年热恋 提交于 2019-12-25 07:49:59
问题 I have the following class hierarchy. class A { public: virtual bool foo() const; }; class B : public A { // Redeclare foo as virtual here? }; class C : public B { bool foo() const {/*Definition*/ return true;} }; class D : public B { bool foo() const {/*Definition*/ return false;} }; So the foo() method the class C and D wants to implement, B doesn't. How can I achieve that? Do I have to re-declare the foo() as virtual in class B? Note: Ignore minor syntactical error here and there. This is

Design alternative for access to derived class member from base class pointer

拜拜、爱过 提交于 2019-12-25 07:47:45
问题 I'm writing a DAL/ORM library. This library will be accessed mainly from GUIs but also from some "business level" applications. I'm still in the design phase of this library and came to a point where I'm not sure how to solve the following issue nicely. In my current design I have a class, let's call it List for the moment, that has a container of another class, Properties . Properties come in two flavors (A and B), with mostly the same functionality, but some of their functionality is