idioms

How to implement the factory method pattern in C++ correctly

你。 提交于 2019-11-26 01:28:50
问题 There\'s this one thing in C++ which has been making me feel uncomfortable for quite a long time, because I honestly don\'t know how to do it, even though it sounds simple: How do I implement Factory Method in C++ correctly? Goal: to make it possible to allow the client to instantiate some object using factory methods instead of the object\'s constructors, without unacceptable consequences and a performance hit. By \"Factory method pattern\", I mean both static factory methods inside an

Can we increase the re-usability of this key-oriented access-protection pattern?

寵の児 提交于 2019-11-26 01:07:26
问题 Can we increase the re-usability for this key-oriented access-protection pattern: class SomeKey { friend class Foo; // more friends... ? SomeKey() {} // possibly non-copyable too }; class Bar { public: void protectedMethod(SomeKey); // only friends of SomeKey have access }; To avoid continued misunderstandings, this pattern is different from the Attorney-Client idiom: It can be more concise than Attorney-Client (as it doesn\'t involve proxying through a third class) It can allow delegation of

How can I loop through a C++ map of maps?

久未见 提交于 2019-11-26 00:51:50
问题 How can I loop through a std::map in C++? My map is defined as: std::map< std::string, std::map<std::string, std::string> > For example, the above container holds data like this: m[\"name1\"][\"value1\"] = \"data1\"; m[\"name1\"][\"value2\"] = \"data2\"; m[\"name2\"][\"value1\"] = \"data1\"; m[\"name2\"][\"value2\"] = \"data2\"; m[\"name3\"][\"value1\"] = \"data1\"; m[\"name3\"][\"value2\"] = \"data2\"; How can I loop through this map and access the various values? 回答1: Old question but the

What is the “Execute Around” idiom?

ε祈祈猫儿з 提交于 2019-11-25 23:45:51
问题 What is this \"Execute Around\" idiom (or similar) I\'ve been hearing about? Why might I use it, and why might I not want to use it? 回答1: Basically it's the pattern where you write a method to do things which are always required, e.g. resource allocation and clean-up, and make the caller pass in "what we want to do with the resource". For example: public interface InputStreamAction { void useStream(InputStream stream) throws IOException; } // Somewhere else public void executeWithFile(String

var functionName = function() {} vs function functionName() {}

点点圈 提交于 2019-11-25 22:09:39
问题 I\'ve recently started maintaining someone else\'s JavaScript code. I\'m fixing bugs, adding features and also trying to tidy up the code and make it more consistent. The previous developer uses two ways of declaring functions and I can\'t work out if there is a reason behind it or not. The two ways are: var functionOne = function() { // Some code }; function functionTwo() { // Some code } What are the reasons for using these two different methods and what are the pros and cons of each? Is

How do I reverse an int array in Java?

笑着哭i 提交于 2019-11-25 21:47:45
问题 I am trying to reverse an int array in Java. This method does not reverse the array. for(int i = 0; i < validData.length; i++) { int temp = validData[i]; validData[i] = validData[validData.length - i - 1]; validData[validData.length - i - 1] = temp; } What is wrong with it? 回答1: To reverse an int array, you swap items up until you reach the midpoint, like this: for(int i = 0; i < validData.length / 2; i++) { int temp = validData[i]; validData[i] = validData[validData.length - i - 1];

What does if __name__ == “__main__”: do?

北城以北 提交于 2019-11-25 21:32:41
问题 What does the if __name__ == \"__main__\": do? # Threading example import time, thread def myfunction(string, sleeptime, lock, *args): while True: lock.acquire() time.sleep(sleeptime) lock.release() time.sleep(sleeptime) if __name__ == \"__main__\": lock = thread.allocate_lock() thread.start_new_thread(myfunction, (\"Thread #: 1\", 2, lock)) thread.start_new_thread(myfunction, (\"Thread #: 2\", 2, lock)) 回答1: Whenever the Python interpreter reads a source file, it does two things: it sets a