inheritance

ruby / ability to share a single variable between multiple classes

丶灬走出姿态 提交于 2021-02-11 13:22:23
问题 For general classes manipulation understanding; given the following use case : class Child1 def process var 'child1' + var end end class Child2 def process var 'child1' + var end end class Child3 def process var 'child3' + var end end ... class Master attr_reader :var def initialize(var) @var = var end def process [ Child1.new.process(var), Child2.new.process(var), Child3.new.process(var) ] end end by some kind of inheritance or structure, would there be a way to have var available to all

ruby / ability to share a single variable between multiple classes

时间秒杀一切 提交于 2021-02-11 13:22:22
问题 For general classes manipulation understanding; given the following use case : class Child1 def process var 'child1' + var end end class Child2 def process var 'child1' + var end end class Child3 def process var 'child3' + var end end ... class Master attr_reader :var def initialize(var) @var = var end def process [ Child1.new.process(var), Child2.new.process(var), Child3.new.process(var) ] end end by some kind of inheritance or structure, would there be a way to have var available to all

ruby / ability to share a single variable between multiple classes

感情迁移 提交于 2021-02-11 13:22:09
问题 For general classes manipulation understanding; given the following use case : class Child1 def process var 'child1' + var end end class Child2 def process var 'child1' + var end end class Child3 def process var 'child3' + var end end ... class Master attr_reader :var def initialize(var) @var = var end def process [ Child1.new.process(var), Child2.new.process(var), Child3.new.process(var) ] end end by some kind of inheritance or structure, would there be a way to have var available to all

Inheriting from a template class set of operators using CRTP

社会主义新天地 提交于 2021-02-11 12:40:35
问题 This is a follow-up from this Q/A. I'm in the process of taking user Jarod42's advice by using template <template<typename> class C, typename T> instead of the example that I have shown as my answer to the question. My code currently looks like this: template<template<typename> class C, typename T> struct single_member_ops { friend auto operator+(const C<T>& lhs, const C<T>& rhs) { return lhs.value + rhs.value; } friend auto operator+(const C<T>& lhs, const T& rhs) { return lhs.value + rhs;}

Inheriting from a template class set of operators using CRTP

家住魔仙堡 提交于 2021-02-11 12:39:12
问题 This is a follow-up from this Q/A. I'm in the process of taking user Jarod42's advice by using template <template<typename> class C, typename T> instead of the example that I have shown as my answer to the question. My code currently looks like this: template<template<typename> class C, typename T> struct single_member_ops { friend auto operator+(const C<T>& lhs, const C<T>& rhs) { return lhs.value + rhs.value; } friend auto operator+(const C<T>& lhs, const T& rhs) { return lhs.value + rhs;}

How to pass subclass in function with base class typehint [duplicate]

血红的双手。 提交于 2021-02-11 12:19:46
问题 This question already has an answer here : Type hints with user defined classes (1 answer) Closed 10 months ago . I have a base class A from what I inherit in sub class B . I have a function where an argument has this base class A as typehint. Now I want to pass to this function sub class B . This works, but I get a warning in my IDE "Expected type A, got B instead". How to do that correctly? class A: pass class B(A): pass def test(a: A): pass test(B()) # <- expected type A, got B instead

How to pass subclass in function with base class typehint [duplicate]

狂风中的少年 提交于 2021-02-11 12:18:23
问题 This question already has an answer here : Type hints with user defined classes (1 answer) Closed 10 months ago . I have a base class A from what I inherit in sub class B . I have a function where an argument has this base class A as typehint. Now I want to pass to this function sub class B . This works, but I get a warning in my IDE "Expected type A, got B instead". How to do that correctly? class A: pass class B(A): pass def test(a: A): pass test(B()) # <- expected type A, got B instead

Avoiding repetitive sub-class definitions in C++

感情迁移 提交于 2021-02-11 08:58:30
问题 I am new to C++ classes, and I have a question about defining multiple sub-classes of an abstract type/interface which would have identical definitions. Take the following example which might appear in a header file with 3 sub-classes: class Animal { private: int a; int b; public: explicit Animal(int a) {} virtual Animal* getFriend() = 0; virtual bool walk() = 0; virtual bool talk() = 0; virtual bool someFunction() = 0; virtual bool someOtherFunction() = 0; // ... many more functions } class

Deriving class from virtual base with no default constructor

血红的双手。 提交于 2021-02-11 07:24:13
问题 I'm writing a small hierarchy of exception classes for a C++ application I'm developing, and I'm having trouble deriving indirectly from std::runtime_error . Here is code analogous to what I've written so far: class RuntimeException : public virtual boost::exception, public virtual std::runtime_error { public: virtual ~RuntimeException() {} RuntimeException() : runtime_error("A RuntimeException occurred.") {} RuntimeException(const std::string& what) : runtime_error(what) {} }; class

Why is this invalid Scala?

孤人 提交于 2021-02-11 06:46:26
问题 I'm working with abstract types, and I'm wondering why this is invalid: class A {} class B extends A {} class X {type T = A} class Y extends X {override type T = B} Seeing as B <: A, why can't I assign B to T? I get this error: overriding type T in class X, which equals A; type T has incompatible type class Y extends X {override type T = B} Any help would be appreciated. 回答1: When you say this: class X {type T = A} you say: T is exactly A or T is an alias for A . It can't be anything else,