inheritance

Avoid Inheriting Super Class Tests in ScalaTest

狂风中的少年 提交于 2019-12-24 05:27:28
问题 I'd like to use test code from another project which has tightly coupled some utility methods along with their tests. I would like to inherit from this class so I'm able to use the utility methods but don't want the accompanying tests. Is there a way to inherit from this other Suite without also registering the tests? I've tried extending from a replica of the class labeled with the @DoNotDiscover annotation but this doesn't seem to have the desired effect. I'd also be interested if there was

Rails Controller Inheritance vs. Concerns and Mixins

假如想象 提交于 2019-12-24 05:17:09
问题 I have a lot of similar resources in my Rails application, and I have currently DRY'd up the code using controller inheritance. I see that there is a directory called concerns under the controller folder, where I could potentially write similar concerns (such as archiving, activate/deactivate etc.) I can probably write mixins too. Is there a preferred approach to DRY up the controller code? Is there any downside in using inheritance, or are there any advantages using other techniques? 回答1: Is

Object's type in a has-a relationship - C++

无人久伴 提交于 2019-12-24 04:59:15
问题 Consider the following code (also available at C++ Shell). Basically, we have an Employee base class with FullTime and PartTime subclasses. The organization class HAS a list of Employees (who could be Fulltime or PartTime). The problem arises when I want to define a getEmployee(int) method in the Organization class. What type of Employee should it return? If it returns a Employee*: first of all, is it safe to return a pointer? I assume that its address is permanent, because it will send the

Limit the number of DIRECT Instances of a class

心不动则不痛 提交于 2019-12-24 04:59:09
问题 To put in other words: How can a class track whether its constructor is called due to instantiating its child class or its instance is directly created? [Please cosider the following sample code]: class Parent { ............. ......... .............. } class Child1 extends Parent { ............. ......... .............. } class Child2 extends Parent { ............. ......... .............. } I want to limit number of direct instances of Parent class created by calling new Parent(...) , and

How to inherit and implement a pure virtual method with the abstract class as a parameter?

白昼怎懂夜的黑 提交于 2019-12-24 04:53:08
问题 I have an abstract class Node which contains a pure virtual method stub matches , requiring another instance of a Node (i.e. instance of something that subclasses Node ) as a parameter. class Node; // forward declaration class Node { public: Node() : parentNode(this) {} virtual ~Node() {} Node* parentNode; virtual bool matches(const Node& node) const = 0; }; How can I implement matches in a subclass such that the parameter can be of the subclasses type as opposed to Node ? E.g. I want

Virtual inheritance: Why does it work when only one base class has “virtual” keyword? Is there a better way?

半世苍凉 提交于 2019-12-24 04:47:11
问题 I want to implement a class hierarchy in C++: I need interfaces so I can provide multiple implementations. I need common methods in all the classes. But I need to be able to override specific methods. Constructors all take at least one parameter. Simplified I have this code: #include <iostream> class IClass { public: virtual int commonMethod() const = 0; }; class Class : public virtual IClass { protected: int commonValue; public: Class(int commonValue) : commonValue(commonValue) {} virtual

Restrict classes that can implement interface in Java

感情迁移 提交于 2019-12-24 04:45:10
问题 Before we start: I'm aware of this question and in my case that answer will not work. So let's say I have: interface Iface class SuperClass (this one is from external library and can't be changed) class SubClass extends SuperClass class OtherClass Is there a way to make it so that Iface can only be implemented by SuperClass and its subclasses? So in this case: class SuperClass implements Iface - would be good if I could edit that class class SubClass implements Iface - good class OtherClass

Virtual function invocation from constructor

这一生的挚爱 提交于 2019-12-24 04:40:54
问题 Maybe I am wrong, but this seems to be a very basic question. Suddenly my inheritance chain stopped working. Writing a small basic test application proved that it was me that was wrong (so I can't blame the compiler). I have a base class, with the default behavior in a virtual function. A child class derives from that and changes the behavior. #include <iostream> class Base { public: Base() { print(); } ~Base() {} protected: virtual void print() { std::cout << "base\n"; } }; class Child :

Java inheritance and super() isn't working as expected

北城余情 提交于 2019-12-24 04:15:13
问题 For a homework assignment, I'm working with the following. It's an assigned class structure, I know it's not the best design by a long shot. Class | Extends | Variables -------------------------------------------------------- Person | None | firstName, lastName, streetAddress, zipCode, phone CollegeEmployee | Person | ssn, salary,deptName Faculty | CollegeEmployee | tenure(boolean) Student | person | GPA,major So in the Faculty class... public class Faculty extends CollegeEmployee { protected

Confusion with virtual pointer of derived class

和自甴很熟 提交于 2019-12-24 03:42:35
问题 Base having a virtual function and Derived also having one virtual function like this, class Base { private: int i; public: Base(int data = 9):i(data) { cout << "In Base class constructor" << endl; } void display() { cout << "In Base class" << endl; cout << "i = " << i << endl; } virtual ~Base() { cout << "In Base class destructor" << endl; } }; class Derived: public Base { private: int j; public: Derived(int data = 10):Base(11),j(data) { cout << "In Derived class constructor" << endl; }