initialization

Java: “Local variable may not have been initialized” not intelligent enough?

我的未来我决定 提交于 2020-01-10 02:07:05
问题 Consider the following method: void a () { int x; boolean b = false; if (Math.random() < 0.5) { x = 0; b = true; } if (b) x++; } On x++ I get the "Local variable may not have been initialized" error. Clearly x will never be used uninitialized. Is there any way to suppress the warning except by initializing x? Thanks. 回答1: No, there is no way Java can examine all possible code paths for a program to determine if a variable has been initialized or not, so it takes the safe route and warns you.

C++ global variable not initialized when linked through static libraries, but OK when compiled with source

被刻印的时光 ゝ 提交于 2020-01-09 12:52:11
问题 I have created a system that automatically registers function objects (functors) into a map based on the constructor of an global instance. In each cpp file that defines the functor, there's a global instance of the registrar class instance to register the functor to a singleton std::map<int, std::function<...> > object. This is the definition of registrar class: template < typename map_type, typename handler_type > struct registrar { registrar ( map_type& map_object, boost::uint16_t cmd_code

Where can we use list initialization?

≯℡__Kan透↙ 提交于 2020-01-09 09:49:28
问题 This question already covers what PODs and aggregates are, and provides some examples on aggregate initialization. The question here is where can you use list initialization? Also where can you use (in lack of a better term) list assignment? An answer should deal with both C++03 and C++11, highlighting the differences between them. 回答1: C++03 List initialization In C++03 you can only use list-initialization for aggregates (C++03 [dcl.init.aggr]) and scalar (C++03 [dcl.init]/13) types: int i =

Why use required Initializers in Swift classes?

a 夏天 提交于 2020-01-09 08:37:34
问题 I am trying to understand the use of the required keyword in Swift classes. class SomeClass { required init() { // initializer implementation goes here } } required doesn't force me to implement the method in my child-class. If I want to override the required designated initializer of my parent class I need to write required and not override . I know how it works but can not understand why I should do this. What is the benefit of required ? As far as I can tell, languages like C# don't have

Why use required Initializers in Swift classes?

无人久伴 提交于 2020-01-09 08:36:32
问题 I am trying to understand the use of the required keyword in Swift classes. class SomeClass { required init() { // initializer implementation goes here } } required doesn't force me to implement the method in my child-class. If I want to override the required designated initializer of my parent class I need to write required and not override . I know how it works but can not understand why I should do this. What is the benefit of required ? As far as I can tell, languages like C# don't have

Value of type char* cannot be used to initialize an entity of type “char”

橙三吉。 提交于 2020-01-07 06:46:10
问题 I'm new to C++ and I'd like to do simple stuff such as writting to disk the content of a char[] I'm having hard times doing it. Here's my code: char x[256],y[256],z[256]; sprintf( x, "%.2f", pCommandHandling->m_dtHandleInformation[i].Xfrms.translation.x ); //pCommandHandling->m_dtHandleInformation[i].Xfrms.translation.x is a float struct sprintf( y, "%.2f", pCommandHandling->m_dtHandleInformation[i].Xfrms.translation.y ); //pCommandHandling->m_dtHandleInformation[i].Xfrms.translation.y is a

Changing Object initializer/constructor depending on method input

大憨熊 提交于 2020-01-07 03:42:10
问题 So I am trying to avoid using duplicate code. At the moment I have several lists which contain Strings; one list is called "images" and the other "videos" etc. These lists contain the properties of the content and they are in a linked list because that information was read from a text file. I am trying to go through these lists and create image/video objects to place in another object (later on). At the moment I have a method private void loadContent(List<String> contentType) inside of it how

Speed up first UIImageView animation (force cache the images)

蹲街弑〆低调 提交于 2020-01-07 02:50:47
问题 My app consists of an animation of about 25 frames. The animations is triggered by a button, and all is working. However, upon first invoking the animation, there is a few second delay. I am aware that this is because the images need to be cached upon their first run in an animation, and I have ~2mb to be cached. I wish Apple would implement an [ animation prepareToPlay] method for this reason. In the meantime, I'm running the animation within viewDidLoad. This caches the images during the

comparison of two auto variables

自古美人都是妖i 提交于 2020-01-06 23:46:50
问题 #include <iostream> using namespace std; int main() { auto a{1}; auto b{1}; if (a==b) { cout << "equal"; } return 0; } Why does the above C++ code return an error in g++ compiler with c++11 standard, instead of printing "equal" as output? test.cpp:9:14: error: no match for ‘operator==’ (operand types are ‘std::initializer_list’ and ‘std::initializer_list’) if (a==b) ^ 回答1: What do you think: auto a{1}; is to compiler? If you think it's supposed to be integer you are wrong. Compilers are lazy

vector of class pointers initialization

放肆的年华 提交于 2020-01-06 20:23:51
问题 class Comp { //... }; class MyClass { private: vector<Comp*>vec; //... }; I need to initialize a vector of class type pointers to objects. how can I initialize it? 回答1: You can set an initial size (e.g. 10, as shown below), filled with all NULL values with the constructor: vector<Comp*> vec(10, NULL); You can also insert elements in various ways, using the push_back(), push_front() , and insert() methods. 回答2: Use vec.push_back(new Comp()) but remember to delete all items using delete vec[