class-design

static const Member Value vs. Member enum : Which Method is Better & Why?

回眸只為那壹抹淺笑 提交于 2019-11-28 03:40:01
If you want to associate some constant value with a class, here are two ways to accomplish the same goal: class Foo { public: static const size_t Life = 42; }; class Bar { public: enum {Life = 42}; }; Syntactically and semantically they appear to be identical from the client's point of view: size_t fooLife = Foo::Life; size_t barLife = Bar::Life; Is there any reason other than just pure style concerns why one would be preferable to another? The enum hack used to be necessary because many compilers didn't support in-place initialization of the value. Since this is no longer an issue, go for the

python circular imports once again (aka what's wrong with this design)

心已入冬 提交于 2019-11-28 03:33:48
Let's consider python (3.x) scripts: main.py: from test.team import team from test.user import user if __name__ == '__main__': u = user() t = team() u.setTeam(t) t.setLeader(u) test/user.py: from test.team import team class user: def setTeam(self, t): if issubclass(t, team.__class__): self.team = t test/team.py: from test.user import user class team: def setLeader(self, u): if issubclass(u, user.__class__): self.leader = u Now, of course, i've got circular import and splendid ImportError. So, not being pythonista, I have three questions. First of all: i. How can I make this thing work ? And,

Class members that are objects - Pointers or not? C++

醉酒当歌 提交于 2019-11-28 03:23:34
If I create a class MyClass and it has some private member say MyOtherClass, is it better to make MyOtherClass a pointer or not? What does it mean also to have it as not a pointer in terms of where it is stored in memory? Will the object be created when the class is created? I noticed that the examples in QT usually declare class members as pointers when they are classes. Regards Mark If I create a class MyClass and it has some private member say MyOtherClass, is it better to make MyOtherClass a pointer or not? you should generally declare it as a value in your class. it will be local, there

How many methods can a C# class have

☆樱花仙子☆ 提交于 2019-11-28 02:39:02
问题 Is there a limitation on number of properties, methods a C# class can have? I do a quick skim at Standard ECMA-334 and did not find any information on it. Before jumping into why a class with many methods are bad design, I want to be more clear on the intention. Of course I will not be writing a class with large number of methods manually. The reason I am asking this is I need to generate a large number of execution units by code. I am debate between have multiple classes with single method

Calling method that exists in child classes but not in parent class

久未见 提交于 2019-11-28 01:58:20
public class Parent { .... } public class Child1 extends Parent { .... public void foo() { .... } } public class Child2 extends Parent { .... public void foo() { .... } } Here method foo() only exists in the Child classes and CAN NOT be added to the Parent class (not even abstract method). In this situation when I want to call the foo() method on obj which is Parent class's reference then I need to use intanceof with multiple if..else which I want to avoid. Parent obj = ...// Object of one of the child classes obj.foo(); EDIT: I Need to use type of obj as Parent only. Else I will not be able

Delegates, can't get my head around them

不羁岁月 提交于 2019-11-27 20:49:13
问题 Hey, I'm looking for useful resources about Delegates. I understand that the delegate sits in the background and receives messages when certain things happen - e.g. a table cell is selected, or data from a connection over the web is retrieved. What I'd like to know in particular is how to use delegates with multiple objects. As far as I know, specifying the same delegate for an object (e.g. table cell) would cause the same events to be called for both the cells at the same time. Is there

Improve this PHP bitfield class for settings/permissions?

烈酒焚心 提交于 2019-11-27 19:30:15
I have been trying to figure out the best way to use bitmask or bitfields in PHP for a long time now for different areas of my application for different user settings and permissions. The farthest I have come so far is from a class contributed by svens in the Stack Overflow post Bitmask in PHP for settings? . I have slightly modified it below, changing it to use class constants instead of DEFINE and making sure the get method is passed an int only. I also have some sample code to test the class's functionality below. I am looking for any suggestions/code to improve this class even more so it

Nested Java enum definition - does declaring as static make a difference? [duplicate]

吃可爱长大的小学妹 提交于 2019-11-27 18:27:38
This question already has an answer here: In Java, are enum types inside a class static? 2 answers I have an interface - here's a nicely contrived version as an example: public interface Particle { enum Charge { POSITIVE, NEGATIVE } Charge getCharge(); double getMass(); etc... } Is there any difference in how implementations of this would behave if I defined the Charge enum as static - i.e. does this have any effect: public interface Particle { static enum Charge { POSITIVE, NEGATIVE } Charge getCharge(); double getMass(); etc... } No, it makes no difference. However the reason is not because

How do you design object oriented projects? [closed]

自古美人都是妖i 提交于 2019-11-27 16:33:07
I'm working on a large project (for me) which will have many classes and will need to be extensible, but I'm not sure how to plan out my program and how the classes need to interact. I took an OOD course a few semesters back and learned a lot from it; like writing UML, and translating requirements documents into objects and classes. We learned sequence diagrams too but somehow I missed the lecture or something, they didn't really stick with me. With previous projects I've tried using methods I learned from the course but usually end up with code that as soon as I can say "yeah that looks

Non-public top-level class vs static nested class

女生的网名这么多〃 提交于 2019-11-27 12:59:01
问题 It seems to me that non-public top-level classes and static nested classes essentially perform the same tasks when creating a helper class. A.java public class A { public static main (String[] args) { AHelper helper = new AHelper(); } } class AHelper {} A.java public class A { public static main (String[] args) { A.AHelper helper = new A.AHelper(); } static class AHelper {} } Aside from how they are referenced, there seems to me very little difference between the two ways of creating a helper