abstract

ptr_map and pointer

扶醉桌前 提交于 2019-12-01 06:51:53
问题 I'm using ptr_map from boost for storing objects derived from some base abstract type. class Entity { virtual void foo() = 0; }; class Entity1 : public Entity {}; class Entity2 : public Entity {}; boost::ptr_map<string, Entity> someMap; // We could store pointers for abstract type Inserting works great: someMap.insert("someKey", new Entity1()); someMap.insert("someKey", new Entity2()); But not returning from map: template<typename EntityType> EntityType *GetEntity(const string &entityName) {

C : send different structures for one function argument

▼魔方 西西 提交于 2019-12-01 05:05:38
问题 I have a function that draws a circle using OpenGL, I would like to pass it a structure containing the x and y coordinates and the radius. The problem is this same function has to be used with 3 different structures all containing the coordinates, radius and some other things that the draw function doesn't use. Is there some way to only have one argument for 3 different structures (only one is sent at a time). I hope I've been enough precise. PS : the functions have to be "abstract". 回答1: Yes

Passing parameters to the base class constructor

╄→尐↘猪︶ㄣ 提交于 2019-12-01 03:12:14
If the base class and derived class both have their constructors with parameters then where we pass the parameters to the base class constructors? Like this: public class DerivedClass : BaseClass { public DerivedClass(int derivedParam, String baseParam):base(baseParam) { } } The base keyword here calls the base class constructor that matches the provided parameter overload. 来源: https://stackoverflow.com/questions/23481456/passing-parameters-to-the-base-class-constructor

opaque (abstract) data types in C

瘦欲@ 提交于 2019-12-01 01:42:30
File api.h #include <stdio.h> #ifndef API #define API struct trytag; typedef struct trytag try; void trial (try *); #endif File core.h #ifndef CORE #define CORE struct trytag { int a; int b; }; #endif File func.c #include "api.h" #include "core.h" void trial (try *tryvar) { tryvar->a = 1; tryvar->b = 2; } File main.c #include "api.h" int main () { try s_tryvar; trial(&s_tryvar); printf("a = %d\nb = %d\n", s_tryvar.a, s_tryvar.b); } When I compile, I get: main.c:5: error: storage size of ‘s_tryvar’ isn’t known If I include core.h in main.c this error doesn't come as try is defined in core.h .

Passing parameters to the base class constructor

邮差的信 提交于 2019-12-01 00:05:13
问题 If the base class and derived class both have their constructors with parameters then where we pass the parameters to the base class constructors? 回答1: Like this: public class DerivedClass : BaseClass { public DerivedClass(int derivedParam, String baseParam):base(baseParam) { } } The base keyword here calls the base class constructor that matches the provided parameter overload. 来源: https://stackoverflow.com/questions/23481456/passing-parameters-to-the-base-class-constructor

How do i make an abstract class work with JAXB

一曲冷凌霜 提交于 2019-11-30 20:40:51
Dear fellow java coders, I have used an example from http://www.vogella.com/articles/JAXB/article.html for JAXB XML usage for my 3 classes, UserStorage, User, and UserTest it works fine, but it's just the unmarchialing of JAXBContext context = JAXBContext.newInstance(UserStorage.class); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); the User class is ABSTRACT!, so it throws an Exception in thread "main" javax.xml.bind.UnmarshalException: Unable to create an instance of platform.User - with linked exception: [java.lang

Flatten a list of lists

限于喜欢 提交于 2019-11-30 13:07:34
I have to write a function that flattens a list of lists. For example flatten [] = [] or flatten [1,2,3,4] = [1,2,3,4] or flatten [[1,2],[3],4,5]] = [1,2,3,4,5] I'm having trouble with the being able to match the type depending on what is given to the flatten function. Here's what I have: data A a = B a | C [a] deriving (Show, Eq, Ord) flatten::(Show a, Eq a, Ord a)=>A a -> A a flatten (C []) = (C []) flatten (C (x:xs) ) = (C flatten x) ++ (C flatten xs) flatten (B a) = (C [a]) From what I can tell the issue is that the ++ operator is expecting a list for both of its arguments and I'm trying

protected data in abstract class

别等时光非礼了梦想. 提交于 2019-11-30 11:48:40
My question involves specifically Java, abstract classes, and the use of protected data. I am being told that all the data should be private, and protected getters/setters used only. Now, I understand we want to shield data from direct manipulation by casual users of the class, and that public data members in general are a questionable practice. I have looked at "Java protected fields vs public getters" ( Java protected fields vs public getters ), but I still am dubious that: protected int i; is worse in an abstract class than: private int i; protected int geti(); protected void seti(int j); I

Best way to declare an interface in C++11

我与影子孤独终老i 提交于 2019-11-30 10:21:34
问题 As we all know, some languages have the notion of interfaces. This is Java: public interface Testable { void test(); } How can I achieve this in C++ (or C++11) in most compact way and with little code noise? I'd appreciate a solution that wouldn't need a separate definition (let the header be sufficient). This is a very simple approach that even I find buggy ;-) class Testable { public: virtual void test() = 0; protected: Testable(); Testable(const Testable& that); Testable& operator= (const

Abstraction and abstract in java

£可爱£侵袭症+ 提交于 2019-11-30 08:42:56
问题 I am a java developer with good understanding of Object orientation concepts( or maybe, I think like that ). And right now I am learning design patterns (From Head first design patterns). I have been reading about OOPS concept abstraction to understand it briefly, and reading more about it has made me more confusing than I earlier was. As I understand, abstraction refers to hiding the internal details of the program while exposing the interface to other programmers without worries of internal