class-design

In .NET, Why Can I Access Private Members of a Class Instance within the Class?

陌路散爱 提交于 2019-12-05 05:11:00
While cleaning some code today written by someone else, I changed the access modifier from Public to Private on a class variable/member/field. I expected a long list of compiler errors that I use to "refactor/rework/review" the code that used this variable. Imagine my surprise when I didn't get any errors. After reviewing, it turns out that another instance of the Class can access the private members of another instance declared within the Class. Totally unexcepted. Is this normal? I been coding in .NET since the beginning and never ran into this issue, nor read about it. I may have stumbled

Any tool which generate C++ code against a design

試著忘記壹切 提交于 2019-12-04 22:23:26
I have a class diagram , I want that to draw it in some tool which can generate native C++ out of it , I wish to have design first approach to test the various data structures I am designing for my application A lot of tools can make C++ code out of UML diagrams. Try Software Ideas Modeller for example, it's a great UML tool, and it's free for non-commercial use. Also, wiki has a list of UML tools. You can sort them by "languages generated" column to check what they do support and chose the one that you will like. You can also try UMLET for your work. You can seach mofre on google by typing

Why put private fields and methods at the top of class?

亡梦爱人 提交于 2019-12-04 22:17:18
I've seen this de facto standard in many places in many languages, but I've never understood it - why put your private fields and methods at the top of a class declaration? Metaphorically it seems like private things should be located at the bottom (hidden) and everything public should be at the top, so that when you read through the class top to bottom you first see the public interface then the inner workings. What is the reasoning behind this? EDIT: Just to clarify, I don't mean the practice of declaring all members at the top of the class, but of putting private members/methods at the top

What is a good design pattern in C# for classes that need to reference other classes?

非 Y 不嫁゛ 提交于 2019-12-04 21:45:09
问题 I am working on a business problem in C#.NET. I have two classes, named C and W that will be instantiated independently at different times. An object of class C needs to contain references to 0 ... n objects of class W, i.e. a C object can contain up to n W objects. Each W object needs to contain a reference to exactly 1 object of class C, i.e. a W object is contained in one C object. An object of class C is usually instantiated first. At a later point, its W contents are discovered, and

python 3: class “template” (function that returns a parameterized class)

空扰寡人 提交于 2019-12-04 17:14:16
问题 I am trying to create a function that is passed a parameter x and returns a new class C . C should be a subclass of a fixed base class A , with only one addition: a certain class attribute is added and is set to equal x . In other words: class C(A): C.p = x # x is the parameter passed to the factory function Is this easy to do? Are there any issues I should be aware of? 回答1: First off, note that the term "class factory" is somewhat obsolete in Python. It's used in languages like C++, for a

How should I split large and bloated classes into smaller ones?

对着背影说爱祢 提交于 2019-12-04 14:08:33
问题 I have a large 'Manager' class which I think is doing too much but I am unsure on how to divide it into more logical units. Generally speaking the class basically consists of the following methods: class FooBarManager { GetFooEntities(); AddFooEntity(..); UpdateFooEntity(..); SubmitFooEntity(..); GetFooTypes(); GetBarEntities(); } The Manager class is part of my business logic and constains an instance of another "Manager" class on the data access level which contains all CRUD operations for

Cleaning up in Shutdown() method instead of destructor

£可爱£侵袭症+ 提交于 2019-12-04 10:41:01
In Rastertek DirectX tutorials they have empty constructors and destructors and instead use initialize() and shutdown() functions for objects initialization and cleaning up. After using this design for a while I can somewhat understand the benefits of having an initialize() method, but I can't see how using a shutdown() method is any better than putting all the clean-up code in the destructor. The reason they provide is the following: You will also notice I don't do any object clean up in the class destructor. I instead do all my object clean up in the Shutdown function you will see further

How to force only smart pointers instance for a class?

醉酒当歌 提交于 2019-12-04 09:16:14
问题 I've been working on a way to prevent user of using a class without smart pointers. Thus, forcing them to have the object being heap allocated and managed by smart pointers. In order to get such a result, I've tried the following : #include <memory> class A { private : ~A {} // To force use of A only with std::unique_ptr friend std::default_delete<A>; }; This work pretty well if you only want your class users being capable of manipulating instance of your class through std::unique_ptr . But

Copying Methods from Member

爱⌒轻易说出口 提交于 2019-12-04 07:39:33
I have a simple, low-level container class that is used by a more high-level file class. Basically, the file class uses the container to store modifications locally before saving a final version to an actual file. Some of the methods, therefore, carry directly over from the container class to the file class. (For example, Resize() .) I've just been defining the methods in the file class to call their container class variants. For example: void FileClass::Foo() { ContainerMember.Foo(); } This is, however, growing to be a nuisance. Is there a better way to do this? Here's a simplified example:

Should entities implement interfaces?

喜你入骨 提交于 2019-12-04 07:21:43
I personally don't have my entities implement interfaces. For a Task class I wouldn't have ITask that just had the same properties defined on it. I've seen it done a few times though, so I'm wondering where that advice comes from, and what benefits you get from it. If you're using an ORM then the argument that says "I can change my data access" is irrelevent, so what other reason is there for doing this? UPDATE: A good point was made in the comments about INotifyPropertyChanged . That wasn't my point though - I'm talking about having something like this: public interface ITask { int Id { get;