variable-initialization

Which is the better approach to initialize php properties?

核能气质少年 提交于 2019-12-05 13:24:42
问题 Here are two way to initialize class variables. 1st Method class Test { private $var1; private $var2; public function Test($var1,$var1) { $this->var1 = $var1; $this->var2 = $var2; } } $objTest = new Test("value1","value2"); 2nd Method class Test { private $var1; private $var2; public function _set($var, $value) { $this->$$var = $value } } $objTest = new Test(); $objTest->_set('var1','value1'); $objTest->_set('var2','value2'); Now, these both methods are valid, but I would like to know which

Which is the better approach to initialize php properties?

早过忘川 提交于 2019-12-04 00:01:12
Here are two way to initialize class variables. 1st Method class Test { private $var1; private $var2; public function Test($var1,$var1) { $this->var1 = $var1; $this->var2 = $var2; } } $objTest = new Test("value1","value2"); 2nd Method class Test { private $var1; private $var2; public function _set($var, $value) { $this->$$var = $value } } $objTest = new Test(); $objTest->_set('var1','value1'); $objTest->_set('var2','value2'); Now, these both methods are valid, but I would like to know which one in better in what conditions? What are pros and cons of sticking with one method only? In your

Why can't I access a member of this class? [duplicate]

拈花ヽ惹草 提交于 2019-12-01 23:30:35
This question already has an answer here: My attempt at value initialization is interpreted as a function declaration, and why doesn't A a(()); solve it? 5 answers I have the following three class definitions: class String { public: String() {} String(const char *) {} }; class ClassA { public: ClassA(const String &) {} }; class ClassB { public: ClassB(const ClassA &, const String & = String()) {} void method() {} }; Now suppose I want to create an instance of ClassB : String name("test"); ClassA item(ClassB(name)); This doesn't work: error: request for member 'method' in 'item', which is of

Is it ok to call a function in constructor initializer list?

做~自己de王妃 提交于 2019-11-30 04:18:36
My gut feeling is it is not. I am in the following situation: class PluginLoader { public: Builder* const p_Builder; Logger* const p_Logger; //Others }; PluginLoader::PluginLoader(Builder* const pBuilder) :p_Builder(pBuilder), p_Logger(pBuilder->GetLogger()) { //Stuff } Or should I change the constructor and pass a Logger* const from where PluginLoader is constructed? That's perfectly fine and normal. p_Builder was initialized before it. Benjamin Lindley What you have is fine. However, I just want to warn you to be careful not to do this : (GMan alluded to this, I just wanted to make it

Does an int in Objective-C have a default value of 1?

坚强是说给别人听的谎言 提交于 2019-11-29 11:04:50
I have this simple line of code: int x; x automatically has the value of 1. I don't set it to anything but when I debug, it shows that x is 1. Does an int have a default value of 1?! No. int has an undefined default value. It just happens to be 1 in this case. It could just as easily be -18382 or 22 or 0xBAADF00D . Always initialize your variables in C. The initial value is undefined, and in this case will be whatever happened to be in that memory location before x started using it. (Depending on the surrounding code, you might find that in your specific case it's always 1 , but you can't be

Is it ok to call a function in constructor initializer list?

一曲冷凌霜 提交于 2019-11-29 01:38:06
问题 My gut feeling is it is not. I am in the following situation: class PluginLoader { public: Builder* const p_Builder; Logger* const p_Logger; //Others }; PluginLoader::PluginLoader(Builder* const pBuilder) :p_Builder(pBuilder), p_Logger(pBuilder->GetLogger()) { //Stuff } Or should I change the constructor and pass a Logger* const from where PluginLoader is constructed? 回答1: That's perfectly fine and normal. p_Builder was initialized before it. 回答2: What you have is fine. However, I just want

Declaring vs Initializing a variable?

怎甘沉沦 提交于 2019-11-28 11:12:28
I'm curious to know the difference between declaring a variable, and initializing a variable. e.g. var example; // this is declaring var example = "hi" // initializing? Or just "adding a value"? I don't think I'm right there, but what exactly is the definition of each? Or do they basically mean the same thing? Edit: @ThisClark said something in the comments, and I went to prove him wrong, and upon reading the spec some more I learned something: Here's an informative except from the specification : A var statement declares variables that are scoped to the running execution context’s

How to initialize to zero/NULL in a template

戏子无情 提交于 2019-11-28 07:54:54
问题 While writing a template, I want to initialize my variable to a value that serves as zero or null the the data type. If I set it to 0x00 is it going to serve as zero/NULL for any type ? for example This is template declaration template <class T> ... T A=0x00; Now if I define an instance of type T => std::string the above statement serves as NULL ? What about " int " and " unsigned int ". For both of the it serves as "0" ? 回答1: Use Value Initialization: T A = T(); // before C++11 T A{}; // C+

Does an int in Objective-C have a default value of 1?

我的未来我决定 提交于 2019-11-28 04:16:29
问题 I have this simple line of code: int x; x automatically has the value of 1. I don't set it to anything but when I debug, it shows that x is 1. Does an int have a default value of 1?! 回答1: No. int has an undefined default value. It just happens to be 1 in this case. It could just as easily be -18382 or 22 or 0xBAADF00D . Always initialize your variables in C. 回答2: The initial value is undefined, and in this case will be whatever happened to be in that memory location before x started using it.

Stop a periodic task from within the task itself running in a ScheduledExecutorService

送分小仙女□ 提交于 2019-11-27 21:13:15
Is there a nice way to stop the repetition of task from within the task itself when running in a ScheduledExecutorService? Lets say, I have the following task: Future<?> f = scheduledExecutor.scheduleAtFixedRate(new Runnable() { int count = 0; public void run() { System.out.println(count++); if (count == 10) { // ??? cancel self } } }, 1, 1, TimeUnit.SECONDS); From outside, it is easy to cancel via f.cancel(), but how can I stop the repetition at the specified place? (Passing the Future through an AtomicReference is not safe, because there is a potential window when the scheduleAtFixedRate