nested-class

“Public” nested classes or not

隐身守侯 提交于 2019-11-30 01:55:05
Suppose I have a class 'Application'. In order to be initialised it takes certain settings in the constructor. Let's also assume that the number of settings is so many that it's compelling to place them in a class of their own. Compare the following two implementations of this scenario. Implementation 1: class Application { Application(ApplicationSettings settings) { //Do initialisation here } } class ApplicationSettings { //Settings related methods and properties here } Implementation 2: class Application { Application(Application.Settings settings) { //Do initialisation here } class Settings

Nested class as a template parameter of parent class in C++

巧了我就是萌 提交于 2019-11-29 23:23:41
问题 I want to implement an algorithm as a class deriving from a pure virtual class representing the kind of problem the particular algorithm solves. The general interface would look like this: template<typename A, typename B> class ISolutionToProblem { public: virtual void Init(const A & input, const B & param) = 0; virtual const B & ComputeSolution() = 0; virtual ~ISolutionToProblem() {} }; And the implementation would be for example: template<typename T> class MyAlgorithm: public

Putting Nested Classes In Separate Files

不打扰是莪最后的温柔 提交于 2019-11-29 21:39:03
问题 I have a file with nested classes, but it's becoming long enough to be unreadable. Is there a way I can break out the nested classes into separate files? 回答1: Use the right tools. 回答2: I'm afraid not. As an alternative, you could consider converting some of them to plain Java classes: although nested classes increase encapsulation, you can certainly create good OO design without them. My two cents. 回答3: Write unit-tests to cover the code you are going to refactor. Then use refactoring tools,

Nested Python class needs to access variable in enclosing class

不打扰是莪最后的温柔 提交于 2019-11-29 13:19:31
I've seen a few "solutions" to this, but the solution every time seems to be "Don't use nested classes, define the classes outside and then use them normally". I don't like that answer, because it ignores the primary reason I chose nested classes, which is, to have a pool of constants (associated with the base class) accessible to all sub-class instances which are created. Here is example code: class ParentClass: constant_pool = [] children = [] def __init__(self, stream): self.constant_pool = ConstantPool(stream) child_count = stream.read_ui16() for i in range(0, child_count): children.append

Access base class attribute in derived class - in “class scope”

风流意气都作罢 提交于 2019-11-29 11:52:37
class Outer(object): class InnerBase(object): _var = {'foo', 'bar'} class Derived(InnerBase): _var = _var | {'baz'} # NameError: name '_var' is not defined _var = InnerBase._var | {'baz'} # name 'InnerBase' is not defined _var = Outer.InnerBase._var | {'baz'} # free variable 'Outer' # referenced before assignment in enclosing scope Moving _var in Outer does not help - moving it in module scope would work but defeats the purpose of having classes. So how to go about that ? EDIT: coming from Java so the scoping rules of classes are a head scratcher for me - a briefing would be appreciated. This

Nested class: Cannot access non-static field in static context

前提是你 提交于 2019-11-29 10:07:58
I have a class C with some internal variables. It has a nested class N that wants to access the variables in C. Neither C nor N are static, although C has some static methods and variables. When I try to access a non-static variable in C from N I get the squiggly underline and the message "Cannot access non-static field [fieldname] in static context". This seems to have something to do with the nested class, since I can access the variable fine from the enclosing class itself. ReSharper suggests I make _t static but that isn't an option. How do I deal with this? public sealed partial class C {

Private nested Java class in UML diagram

。_饼干妹妹 提交于 2019-11-29 09:16:50
I have a question regarding UML. I have a class which simply contains an inner class with the private access modifier - cannot be accessed from anywhere else... Normally in order to present an inner class relation I can use a (+) relation like here ( InnerOddIterator ): (taken from http://www.uml-diagrams.org/nested-classifier.html ) I have not found anywhere any information about how can clearly emphasize that this class is private. Do you know if such a method exist at all? If yes I'll be grateful you give me some link here or something? Just to keep things clear, a sample code: public class

Nested class is not defined in itself

不想你离开。 提交于 2019-11-29 07:49:36
The following code successfully prints OK : class B(object): def __init__(self): super(B, self).__init__() print 'OK' class A(object): def __init__(self): self.B() B = B A() but the following which should work just as same as above one raises NameError: global name 'B' is not defined class A(object): def __init__(self): self.B() class B(object): def __init__(self): super(B, self).__init__() print 'OK' A() why? B is available in the scope of A class - use A.B : class A(object): def __init__(self): self.B() class B(object): def __init__(self): super(A.B, self).__init__() print 'OK' A() See

Inheritance + NestedClasses in C#

浪子不回头ぞ 提交于 2019-11-29 06:42:58
We can have nested classes in C#. These nested classes can inherit the OuterClass as well. For ex: public class OuterClass { // code here public class NestedClass : OuterClass { // code here } } is completely acceptable. We can also achieve this without making NestedClass as nested class to OuterClass as below: public class OuterClass { // code here } public class NestedClass : OuterClass { // code here } I am wondering, what is the difference between above two scenarioes? What is achievable in scenario I which can't be achievable in scenario II? Is there anything that we get more by making

Nested Class member function can't access function of enclosing class. Why?

僤鯓⒐⒋嵵緔 提交于 2019-11-29 03:08:17
Please see the example code below: class A { private: class B { public: foobar(); }; public: foo(); bar(); }; Within class A & B implementation: A::foo() { //do something } A::bar() { //some code foo(); //more code } A::B::foobar() { //some code foo(); //<<compiler doesn't like this } The compiler flags the call to foo() within the method foobar(). Earlier, I had foo() as private member function of class A but changed to public assuming that B's function can't see it. Of course, it didn't help. I am trying to re-use the functionality provided by A's method. Why doesn't the compiler allow this