abstract

C++ and inheritance in abstract classes

馋奶兔 提交于 2019-12-06 03:11:41
i have a problem in properly handling method overriding where an abstract class is present inside my classes hierarchy. I'll try to explain: class AbstractClass{ public: virtual void anyMethod() = 0; }; class A : public AbstractClass { void anyMethod() { // A implementation of anyMethod cout << "A"; } }; class B : public AbstractClass { void anyMethod() { // B implementation of anyMethod cout << "B"; } }; AbstractClass *ptrA, *ptrB; ptrA = new A(); ptrB = new B(); ptrA->anyMethod(); //prints A ptrB->anyMethod(); //prints B Ok..previous example work fine .. the concrete implementation of the

How to force an implementation of a protected static function

◇◆丶佛笑我妖孽 提交于 2019-12-05 22:01:25
I'm trying to write an abstract class (or interface) which forces the extending class to implement a protected static function. But this is neither possible with an abstract class nor an interface. Errors: static functions should not be abstract access type for interface members must be omitted Any ideas how to accomplish that? UPDATE The purpose is basically to call the public function statically. This way the class does not need to be instanciated. It is also not necessary to make _doSpecificStuff() callable from class-external code. abstract class Foo { public static function doStuff() {

How to use clone() in C++ with multiple inheritance of abstract classes?

廉价感情. 提交于 2019-12-05 21:51:36
I am working on a C++ program, but I am having problem with multiple inheritance when using cloning. The problem (in a simplified form) is the following. I want to be able to clone all objects derived from the class Base. class Base{ public: virtual Base* clone()const=0; }; I want to define two other classes derived from Base, which are both abstract, i.e. I cannot define the clone function, but I have to declare them in some way (I want to make sure, that if I clone Derived*, I will get back Derived* and not Base*, i.e. I want to avoid casting at the point of application) class Derived1:

abstract type in scala

烈酒焚心 提交于 2019-12-05 17:21:35
I am just going through abstract type in Scala and I got an error The example I was trying: scala> class Food abstract class Animal { type SuitableFood <: Food def eat(food: SuitableFood) } defined class Food defined class Animal scala> class Grass extends Food class Cow extends Animal { type SuitableFood = Grass override def eat(food: Grass) {} } defined class Grass defined class Cow scala> class Fish extends Food defined class Fish scala> val bessy: Animal = new Cow bessy: Animal = Cow@5c404da8 scala> bessy.eat(new bessy.SuitableFood) <console>:13: error: class type required but bessy

What is the difference between using <? extends SomeAbstract> vs. SomeAbstract in java generics

自古美人都是妖i 提交于 2019-12-05 17:12:50
I'm moving over to java from DotNet and this idea of extends is new. I've seen some posts that fully explain using List<? extends SomeAbstract> vs. List<? super SomeAbstract> vs. List<SomeAbstract> , but I'm guessing that there is no difference between using, and not using, extends in generics. Is that true? Would the answer change if using an abstract class as the parent? class My_AbstractExtends<T extends SomeAbstract> vs. class My_Abstract<SomeAbstract> EDIT Creates child classes as follows class My_ChildExtends extends My_AbstractExtends<ConcreteChildOfSomeAbstract> vs. class My_Child

Django OneToOneField to multiple models

天大地大妈咪最大 提交于 2019-12-05 16:55:37
Suppose we have a base model: class BaseModel(models.Model): pass with some subclasses: class Submodel1(BaseModel): some_field = models.TextField() ... class Submodel9(BaseModel): another_field = models.TextField() Each submodel is defined in its own Django app. New apps with new submodels can appear. We also have another model, let's call it RelatedModel , which should have a one-to-one relation to BaseModel : class RelatedModel(models.Model): the_thing = models.OneToOneField(BaseModel, null=True, blank=True) Is it possible to do define such a relation if BaseModel.Meta.abstract == True ? Or

How to get the current class name at runtime? [duplicate]

半世苍凉 提交于 2019-12-05 16:18:38
问题 This question already has answers here : C# getting its own class name (9 answers) Closed 6 years ago . I'm trying to get a current class name into a string. For example: public class Marker : Mark { string currentclass = ???; } public abstract class MiniMarker : Mark { } I'd like to get the string from Marker class so I do not have to put it inside each abstract class I make from it. I want the string to be MiniMarker , or what ever the abstract class is named. I tried MethodBase

C++: any way to prevent any instantiation of an abstract base class?

[亡魂溺海] 提交于 2019-12-05 14:50:55
问题 Aside from having a pure virtual function, is there a way to prevent an instantiation of an abstract base class? I can do this: class BaseFoo { virtual void blah() = 0; }; class Foo : public BaseFoo { virtual void blah() {} }; but I'd like to avoid a vtable. (as per my other question about virtual destructors) Microsoft ATL has ATL_NO_VTABLE to accomplish this (or at least I think that's what it does...) 回答1: A really obvious way is to declare a protected constructor, and to declare public

How do I mock a method inherited from an abstract class with EasyMock?

淺唱寂寞╮ 提交于 2019-12-05 10:10:26
I'm struggling with EasyMock. I've written two small classes to illustrate my problem: public abstract class A { private AtomicReference<Integer> id = new AtomicReference<Integer>(null); public final int getId() { return id.get(); } public final boolean setId(int id) { return this.id.compareAndSet(null, id); } } public class B extends A { } Then I proceed to write a test method as follows: public class EasyMockTester extends EasyMockSupport { @Test public void test() { B b = EasyMock.createStrictMock(B.class); EasyMock.expect(b.getId()).andReturn(100); replayAll(); int id = b.getId(); System

Does an abstract property create a private backing field?

最后都变了- 提交于 2019-12-05 10:02:48
Simple question: does an abstract property create a private backing field? Example: public abstract Name { get; set; } Will this create a private backing field? I want to force any class that derives this property to use their own backing field, not one that's created by the compiler. No it doesn't. I just tested with the following class: public abstract class Class1 { public abstract string TestStringAbstract { get; set; } public string TestString { get; set; } } and decompiled it in Reflector . This was the generated code: public abstract class Class1 { // Fields [CompilerGenerated] private