initialization

DRY'er Object Initialization in Ruby

Hi, Is there a more 'DRY' way to do the following in ruby? #!/usr/bin/env ruby class Volume attr_accessor :name, :size, :type, :owner, :date_created, :date_modified, :iscsi_target, :iscsi_portal SYSTEM = 0 DATA = 1 def initialize(args={:type => SYSTEM}) @name = args[:name] @size = args[:size] @type ...

About c arrays...

I know that you can declare an array in C like this: int nums[5] = {0,1,2,3,4}; However, can you do this? int nums[5]; // more code.... nums = { 0,2,5,1,2}; In other words, can I initialize the array using the bracket notation at any other time than just the declaration? Thanks for your time, Sam ...

Static constructor equivalent in Objective-C?

I'm new to Objective C and I haven't been able to find out if there is the equivalent of a static constructor in the language, that is a static method in a class that will automatically be called before the first instance of such class is instantiated. Or do I need to call the Initialization code myself? Thanks ...

Initialization of an ArrayList in one line.

I am willing to create a list of options to test something. I was doing: ArrayList<String> places = new ArrayList<String>(); places.add("Buenos Aires"); places.add("Córdoba"); places.add("La Plata"); I refactor the code doing: ArrayList<String> places = new ArrayList<String>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata")); I...

What is the default state of variables?

When a C program starts and variables are assigned to memory locations, does the C standard say if that value is initialized? // global variables int a; int b = 0; static int c; In the above code, 'b' will be initialized to 0. What is the initial value of 'a'? Is 'c' any different since it is static to this module? ...

Is environment.rb invoked on every http request?

I'm wondering what file I should use to ensure my initializing code will only be executed once when the application starts up. Is environment.rb the correct file to use or will it be invoked on every http request? ...

environment first or boot first?

environment.rb starts with this: RAILS_GEM_VERSION = '2.3.2' unless defined? RAILS_GEM_VERSION require File.join(File.dirname(__FILE__), 'boot') Rails::Initializer.run do |config| Does this mean environment.rb starts first and calls boot.rb? ...

Unhandled exceptions in field initializations

Does Java have any syntax for managing exceptions that might be thrown when declaring and initializing a class's member variable? public class MyClass { // Doesn't compile because constructor can throw IOException private static MyFileWriter x = new MyFileWriter("foo.txt"); ... } Or do such initializations always have to be mov...

MFC: OnInitialUpdate function of a CFormView-derived class

My CFormView-derived class is structured as follows: class FormViewClass : public CFormView { ... FormViewClass(); void Initialize(); virtual void OnInitialUpdate(); ... }; Ideally, I would like to call the Initialize() function in the body of the constructor as follows: FormViewClass::FormViewClass() { ...

Initializing 2D int array

Hello. I got this code from a c++ book, and I cannot figure out how the initialization works. From what I can see, there is an outer for loop cycling trough the rows, and the inner loop cycling trough the column. But its is the assignment of the values into the array that I do not understand. #include <iostream> using namespace std; i...

Is it a good idea to use IEEE754 floating point NaN for values which are not set?

Is it a good idea to use IEEE754 floating point NaN (not-a-number) for values which are undefined for non-mathematical reasons? In our case they are not yet set because the values have not been received from some other device. The context is an embedded system using IEC1131 REAL32 values. Edit: The programming language is C, so we would...

Pointer initializiation? for a specific function.

Alright, this one's been puzzling me for a bit. the following function encodes a string into base 64 void Base64Enc(const unsigned char *src, int srclen, unsigned char *dest) { static const unsigned char enc[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; unsigned char *cp; int i; cp = dest...

C++ array initialization

Why can’t I initialize a local array with a string ...

Static initializers and thread synchronization (.NET)

Static initializers are supposed to be executed once before the first reference to the class. It means that every time a class is accessed, a check should be performed whether the static initializers for the class are executed. It seems that in multithreaded environment classes with non-trivial static initializers can be a source of cont...

Thread-safe static variables without mutexing?

I remember reading that static variables declared inside methods is not thread-safe. Dog* MyClass::BadMethod() { static Dog dog("Lassie"); return &dog; } My library generates C++ code for end-users to compile as part of their application. The code it generates needs to initialize static variables in a thread-safe cross-platform ma...

Creating a Pop up and setting its view state before it displays

Hi, I am a beginner developer in Flex, and I have been using viewstates lately. I had a couple of custom popup titlewindow components that are initialized using: PopUpManager.createPopUp(this, ContentCreate, true); They both contain view states already, and are very similar so I wanted to combione them into one popup titlewindow and ...

C/C++ initialization of a normal array with one default value

http://www.fredosaurus.com/notes-cpp/arrayptr/array-initialization.html 1: Page above has a nice list over initialization of arrays. So I have a int array[100] = {-1}; expecting it to be full with -1's but its not, only first value is and the rest are 0's mixed with random values. The code int array[100] = {0}; works just fine a...

Mixed mode MFC application not intializing correctly in VS2008

I have converted a mixed mode MFC application from VS2005 to VS2008. It is compiling OK but when starting the application I get an assert in afxwin1.inl because afxCurrentResourceHandle = NULL. This is before MyCWinApp::InitInstance is called. The call stack is mfc90d.dll->AfxWinInit (with HINSTANCE = NULL) myapp.exe!InternalDllMain my...

Creating an NSArray initialized with count N, all of the same object

I want to create an NSArray with objects of the same value (say NSNumber all initialized to 1) but the count is based on another variable. There doesn't seem to be a way to do this with any of the intializers for NSArray except for one that deals with C-style array. Any idea if there is a short way to do this? This is what I am lookin...

Initializing a GUID variable

I am using VC++ 6.0 on Windows XP SP2 platform. I am using GUID structure in my code. typedef struct _GUID { // size is 16 DWORD Data1; WORD Data2; WORD Data3; BYTE Data4[8]; } GUID; How to initialize this structure to zeros while creating an object? Or when I create an object, what is the default value f...