inheritance

Automatically making Base Constructors available in derived class?

心不动则不痛 提交于 2021-02-01 02:30:45
问题 I have a Base Class with two constructors, requiring a parameter: public abstract class StoreBase { private readonly SomeObject_sobj; protected StoreBase(SomeObject sobj) { _sobj = sobj; } protected StoreBase(OtherObject oobj) { _sobj = new SomeObject(oobj); } } Then I have a derived class: public class MyDerived: StoreBase { } This causes a compilation error as base class doesn't contain parameterless constructor . My understanding is that because MyDerived doesn't contain a constructor, the

Making double and std::vector<double> covariant

强颜欢笑 提交于 2021-01-29 22:32:45
问题 I am trying to make a wrapper for "any" data type such that they have common interface called IValue so it will be possible to call get() on any concrete Value and return the value of concrete data type. In simplest case I just want to be able to call get() on double and std::vector<double> . In my understanding these data types need to be covariant (which it is not at all in my code). The following is my raw imagination of the code: //template<typename T> class IValue { protected: // typedef

Making double and std::vector<double> covariant

泄露秘密 提交于 2021-01-29 22:31:04
问题 I am trying to make a wrapper for "any" data type such that they have common interface called IValue so it will be possible to call get() on any concrete Value and return the value of concrete data type. In simplest case I just want to be able to call get() on double and std::vector<double> . In my understanding these data types need to be covariant (which it is not at all in my code). The following is my raw imagination of the code: //template<typename T> class IValue { protected: // typedef

“Load is not allowed to be called from MonoBehaviour constructor” On script that does not inherit from MonoBehaviour

淺唱寂寞╮ 提交于 2021-01-29 22:20:37
问题 I have two classes; BuildableObject and TempObject . For some reason, Unity is treating TempObject like a MonoBehaviour class in which it throws an error if I use Resources.Load() from the constructor. Why is this, and how do I fix it? public abstract class BuildableObject { public abstract Vector2Int Space { get; set; } public abstract GameObject Body { get; set; } public void Init(GridSpace[,] World, Vector2Int pos) { Vector3 Pos = World[pos.x, pos.y].pos; //TODO: Optimize //TODO: Add

Making double and std::vector<double> covariant

独自空忆成欢 提交于 2021-01-29 21:51:30
问题 I am trying to make a wrapper for "any" data type such that they have common interface called IValue so it will be possible to call get() on any concrete Value and return the value of concrete data type. In simplest case I just want to be able to call get() on double and std::vector<double> . In my understanding these data types need to be covariant (which it is not at all in my code). The following is my raw imagination of the code: //template<typename T> class IValue { protected: // typedef

Why can't I use the property of a superclass in a subclass (Objective-C)

丶灬走出姿态 提交于 2021-01-29 18:09:50
问题 I declared one property in a class: @property double x; Then I made a subclass of that class in which I declared two more properties: @property double a, b; Then I made an init method in the subclass: -(id)initWithAValue:andBValue: And I am allowed to use: _a; // same for b [self setValue a]; // same for b self.a; // same for b and x Now, when I try to use the x property, I can only use [self setValue x] and I can't use _x. Why? 回答1: You are able to get/set the property, you just can't

If 'A' has a member variable 'x' of type 'A*', and 'B' inherits from 'A', how to redefine 'x' as 'B*' inside of 'B'?

有些话、适合烂在心里 提交于 2021-01-29 13:37:14
问题 Edit: a simpler version of the question: class ForwardNode // A { public: char val; ForwardNode* next; // x ForwardNode(char val, ForwardNode* next = nullptr) : val(val), next(next) {} }; class BidirectionalNode : public ForwardNode // B { public: BidirectionalNode* prev; BidirectionalNode(char val, BidirectionalNode* next = nullptr, BidirectionalNode* prev = nullptr) : ForwardNode(val, next), prev(prev) {} }; Here I'm defining a forward node and bidirectional node. Forward node have

Python class inherited singleton inits instance on every call

这一生的挚爱 提交于 2021-01-29 12:26:33
问题 I'm trying to implement class inherited singleton as described here (Method 2). Going over the question and the extensive chosen answer I tried to implement the following: class Singleton(object): _instance = None def __new__(cls, *args, **kwargs): if not isinstance(cls._instance, cls): cls._instance = object.__new__(cls, *args, **kwargs) cls._instance._initialized = False return cls._instance class A(Singleton): def __init__(self): print "Init is called" class B(Singleton): def __init__(self

Is Inheritance in Python canonically the same as in other languages? A comparative example with Java

好久不见. 提交于 2021-01-29 09:47:54
问题 I have been puzzling today, as (simplified) python code seemed to raise an exception. Here's the code in python : class AwinBaseOperator: def __init__(self): self.data = self.get_data(query_url='test') def get_data(self, query_url): print('Parent called with ', repr(query_url)) class AwinTransactionOperator(AwinBaseOperator): def __init__(self): super().__init__() def get_data(self): print('Child called') print(AwinTransactionOperator()) This returns: Child called with 'test.' It means the

Implementation of methods of interface

空扰寡人 提交于 2021-01-29 07:17:31
问题 I want to implement a representation of matrices. for that I have two types of matrices - regular and sparse, which differ in their implementation - one holds a vector, and the second a map of indices and value, both inherit from Matrix class. For that, I'm using the strategy pattern, where I create the base abstract class Matrix , two classes that inherit from Matrix - RegMatrix and SparseMatrix , and MyMatrix that holds a pointer to a Matrix . I want to implement the + operator, which