inheritance

Static const variable is not constant in child class

早过忘川 提交于 2021-02-10 09:34:16
问题 I am using Visual Studio 2008 and have two classes Parent and Child. Parent declares some static const variables in the header, which are then defined in the cpp file. When I try to use the defines as cases in a switch statement in a child class I get the error: C2051: case expression not constant . So I've done some testing and the behavior I'm seeing is somewhat inconsistent. // Parent.h class Parent { public: Parent(); ~Parent(void) { } static const unsigned long A = 1; static const

Dynamically add new subtypes after the original supertype has already been configured in jackson

為{幸葍}努か 提交于 2021-02-10 09:28:12
问题 I have one super class Field and there are two other classes that inherit the super class Field . I want to dynamically add subclass without affecting super class changes public class TestConfiguration { private List<Field> fields; } I want to use the mapping in this way when fields is instance of same class Field then without className property "Field" used for mapping { "fields" : [ { "name" : "First_name", "type" : { "fieldType" : { "name" : "string" } }, "required" : true }] } I want to

C++ Using inheritance to tweak an algorithm

穿精又带淫゛_ 提交于 2021-02-10 07:25:09
问题 Since Dijkstra's algorithm and Prim's algorithm are so similar, I'd like to make a base algorithm class (I'll call it "Greedy" or something) and then I want to inherit from Greedy and tweak the algorithm based on the class. I think it boils down to this. I'd like to reuse a large portion of an algorithm but tweak an operation or two. class BaseAlg { public: BaseAlg(std::vector<int> data) //constructor sums a vector and stores result { int accum = 0; for (unsigned int i = 0; i < data.size(); +

Inheritance with Lo-Dash

[亡魂溺海] 提交于 2021-02-10 03:28:10
问题 I am trying to emulate inheritance in javascript using a the Lo-Dash javascript library. I am simply using _.extend to do so: function Parent() { var self = this; self.hello = function () { console.log('Hello from parent'); }; } function Child() { var self = this; _.extend(self, Parent); } Child.hello(); // Doesn't exist I thought this would work since all javascript functions are objects but obviously I am wrong. Why doesn't this work and how would I properly emulate inheritance using Lo

Inheritance with Lo-Dash

断了今生、忘了曾经 提交于 2021-02-10 03:27:39
问题 I am trying to emulate inheritance in javascript using a the Lo-Dash javascript library. I am simply using _.extend to do so: function Parent() { var self = this; self.hello = function () { console.log('Hello from parent'); }; } function Child() { var self = this; _.extend(self, Parent); } Child.hello(); // Doesn't exist I thought this would work since all javascript functions are objects but obviously I am wrong. Why doesn't this work and how would I properly emulate inheritance using Lo

Java inheritance

依然范特西╮ 提交于 2021-02-09 06:58:07
问题 So I've been trying to find the proper way to get what should be pretty straightforward inheritance to function (the way I want ;)) and I'm failing miserably. Consider this: class Parent { public String name = "Parent"; public Parent() {}; public void doStuff() { System.out.println(name); } } class Child extends Parent { public String name = "Child"; public Child() { doStuff(); } } I understand that Java doesn't do what I'd expect in this case as for some reason member variables aren't

Java inheritance

二次信任 提交于 2021-02-09 06:57:17
问题 So I've been trying to find the proper way to get what should be pretty straightforward inheritance to function (the way I want ;)) and I'm failing miserably. Consider this: class Parent { public String name = "Parent"; public Parent() {}; public void doStuff() { System.out.println(name); } } class Child extends Parent { public String name = "Child"; public Child() { doStuff(); } } I understand that Java doesn't do what I'd expect in this case as for some reason member variables aren't

accessing base class public member from derived class

寵の児 提交于 2021-02-08 21:23:07
问题 Is it possible to access base class public member from instance of derived class in some other locations in the program. class base { public: int x; base(int xx){ x = xx; } }; class derived : base { public: derived(int xx) : base(xx){ } }; class main { public: derived * myDerived; void m1(){ myDerived = new derived(5); m2(myDerived); } void m2(derived * myDerived){ printf("%i", myDerived->x); } }; After above code, I got following error. `error: 'int base::x' is inaccessible` 回答1: You inherit

accessing base class public member from derived class

喜欢而已 提交于 2021-02-08 21:20:31
问题 Is it possible to access base class public member from instance of derived class in some other locations in the program. class base { public: int x; base(int xx){ x = xx; } }; class derived : base { public: derived(int xx) : base(xx){ } }; class main { public: derived * myDerived; void m1(){ myDerived = new derived(5); m2(myDerived); } void m2(derived * myDerived){ printf("%i", myDerived->x); } }; After above code, I got following error. `error: 'int base::x' is inaccessible` 回答1: You inherit

DLL export and inheritance in C++

删除回忆录丶 提交于 2021-02-08 15:27:19
问题 I'm trying to export a class and its base class from a DLL like this: #ifdef MY_EXPORTS #define DECLSPEC_TEST __declspec(dllexport) #else #define DECLSPEC_TEST __declspec(dllimport) #endif class DECLSPEC_TEST BaseClass { // stuff. }; class DECLSPEC_TEST DerivedClass : public BaseClass { // This class only has a constructor which initializes the class differently. }; But I try to use this class in another DLL, I keep getting an error: error LNK2019: unresolved external symbol "__declspec