Questions tagged [variable-initialization]

In computer programming, initialization is the assignment of an initial value for a data object or variable.

In computer programming, initialization is the assignment of an initial value for a data object or variable. The manner in which initialization is performed depends on programming language, as well as type, storage class, etc., of an object to be initialized. Programming constructs which perform initialization are typically called initializers and initializer lists. Initialization is distinct from (and preceded by) declaration, although the two can sometimes be conflated in practice. The complement of initialization is finalization, which is primarily used for objects, but not variables.

119 questions
98
votes
11 answers

Declare and assign multiple string variables at the same time

I'm declaring some strings that are empty, so it won't throw errors later on. I've read that this was the proper way: string Camnr = Klantnr = Ordernr = Bonnr = Volgnr = Omschrijving = Startdatum = Bonprioriteit = Matsoort = Dikte = Draaibaarheid =…
Mathlight
  • 6,436
  • 17
  • 62
  • 107
71
votes
2 answers

What are the differences between C-like, constructor, and uniform initialization?

To the best of my knowledge, there are three ways to initialize a variable in C++. int x = 0; // C-like initialization int x (0); // Constructor initialization int x {0}; // Uniform initialization The uniform initialization was brought on…
dayuloli
  • 16,205
  • 16
  • 71
  • 126
45
votes
3 answers

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

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) …
nakiya
  • 14,063
  • 21
  • 79
  • 118
22
votes
5 answers

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

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; …
22
votes
4 answers

C: Cannot initialize variable with an rvalue of type void*

I have the following code: int *numberArray = calloc(n, sizeof(int)); And I am unable to understand why I receive the following error. Cannot initialize a variable of type 'int *' with an rvalue of type 'void *'`. Thank you.
user3662185
  • 291
  • 1
  • 2
  • 5
21
votes
9 answers

Declaring vs Initializing a variable?

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…
17
votes
5 answers

How is this possible to use in c++?

To my surprise, I found that the name of a c++ object can be the same as class name. Can someone explain to me the reason why? When I declare an object of class a as a a1(), it does not raise an error, but doesn't call the constructor. Why is this…
16
votes
5 answers

Which is the better approach to initialize php properties?

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…
Starx
  • 77,474
  • 47
  • 185
  • 261
11
votes
4 answers

Should variable be initialized before call to function?

When I call Functions to get a value, I usually initialize varible, in case function fails or doesn't return anything and I want to avoid dealing with uninitialized variable. I do the same for string, integer or any other type. Example for integer…
Mike Torrettinni
  • 1,816
  • 2
  • 17
  • 47
11
votes
7 answers

Java String initialization

Which do you prefer and why" String myString = null; if(someCondition) myString = "something"; else myString = "something else"; OR String myString = ""; if(someCondition) myString = "something"; else myString = "something else"; I…
10
votes
2 answers

How to initialize to zero/NULL in a template

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
user5709504
8
votes
5 answers

C# - Initialize a variable without knowing what its going to be

I have two different tables in my database, and each are displayed to the user based on their "SortOrder". I have written two functions that take a row (or entity) and swaps its sort order with the one nearest it (up or down, depending on which…
Jordan Foreman
  • 3,848
  • 7
  • 40
  • 65
7
votes
4 answers

Why is it necessary that variables should be initialized to default values in Java

In an article about how objects are initialized in Java there was a paragraph which is given below: At the beginning of an object's life, the Java virtual machine (JVM) allocates enough memory on the heap to accommodate the object's instance…
swdeveloper
  • 908
  • 1
  • 11
  • 33
6
votes
3 answers

Can I Initialize a char[] with a Ternary?

I asked a question about it and didn't get a really clear answer, but after reading this article I started preferring const char[] to const char*. I've come upon a difficulty when initializing with a ternary. Given const bool bar, I've tried: const…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
6
votes
5 answers

Is it bad practice to initialize a variable to a dummy value?

This question is a result of the answers to this question that I just asked. It was claimed that this code is "ugly" because it initializes a variable to a value that will never be read: String tempName = null; try{ tempName =…
froadie
  • 79,995
  • 75
  • 166
  • 235
1
2 3 4 5 6 7 8