initialization

Are the following two statements semantically same?

Are the following two statements semantically same? #1 person p("Rahul", 20); #2 person const &p = person("Rahul", 20); EDIT: Sorry, I meant to ask whether the following two are semantically same: #1 person const p("Rahul", 20); #2 person const &p = person("Rahul", 20); ...

Is it possible to defer member initialization to the constructor body?

I have a class with an object as a member which doesn't have a default constructor. I'd like to initialize this member in the constructor, but it seems that in C++ I can't do that. Here is the class: #include <boost/asio.hpp> #include <boost/array.hpp> using boost::asio::ip::udp; template<class T> class udp_sock { public: ...

Set attributes from dictionary in python

Is it possible to create an object from a dictionary in python in such a way that each key is an attribute of that object? Something like this: dict = { 'name': 'Oscar', 'lastName': 'Reyes', 'age':32 } e = Employee( dict ) print e.name # Oscar print e.age + 10 # 42 I think it would be pretty much the inverse of this question:...

How can I make `new[]` default-initialize the array of primitive types?

Every now and then I need to call new[] for built-in types (usually char). The result is an array with uninitialized values and I have to use memset() or std::fill() to initialize the elements. How do I make new[] default-initialize the elements? ...

Is new int[10]() valid c++?

While trying to answer this question I found that the code int* p = new int[10](); compiles fine with VC9 compiler and initializes the integers to 0. So my questions are: First of all is this valid C++ or is it a microsoft extension? Is it guaranteed to initialize all the elements of the array? Also, is there any difference if I do new...

Copying pointers in C++

Hello, I have a class A containing two pointers to objects of another class B. I want to initialize one pointer or the other depending on which one is passed to init(), which also takes other parameters. My situation is thus the following: class A { public: A(); init(int parameter, int otherParameter, B* toBeInitialized); prot...

Is Java class initialized by the thread which use it for the first time?

Lets assume following classes definition: public class A { public final static String SOME_VALUE; static { SOME_VALUE = "some.value"; } } public class B { private final String value = A.SOME_VALUE; } Assuming that the class A hasn't been loaded yet, what does happen when object of the class B is instantiated ...

Why a variable is seen in the loop and is not seen outside the loop?

I have the following code: String serviceType; ServiceBrowser tmpBrowser; for (String playerName: players) { serviceType = "_" + playerName + "._tcp"; tmpBrowser = BrowsersGenerator.getBrowser(serviceType); tmpBrowser.browse(); System.out.println(tmpBrowser.getStatus()); } Syste...

What's the effect in Ruby when you initialize with nil: x = String(nil)

What's the effect in Ruby when you pass nil to the constructor as in: s = String(nil) or a = Array(nil) Does this mean that s or a is nil or that s or a is an unpopulated object of type String or type Array? ...

iPhone init method return type

Suppose we are writing a class (let's call it Class) in an iPhone program. In all the samples out there, the init methods are typically declared like this: -(id) initWithFoo: (Foo *) foo My question is: would it be more logical to do the following? Why or why not? -(Class *) initWithFoo: (Foo *) foo ...

Initialization of controllers in a for loop - leaking problem ?

Hey, I am creating a kinda gallery and for each gallery I created a view controller whose view is added to a scrollview (see code below) : GalleryViewController *galViewController; for (NSUInteger i = 0 ; i < [galleries count]; i++) { galViewController = [[GalleryViewController alloc] init]; galViewController.record = [galleri...

What should I use for the filepath if I'm developing from my desktop?

I'm having an extremely difficulty time getting the flowplayer to show up and the worst part is I have no idea what is wrong because I'm not getting any error messages! I have an external javascript file: C:/desktop/mysite/js/jq/plugins.js calling $f() from: C:/desktop/mysite/thirdparty/flowplayer/flowplayer.js the swf files also li...

Ninject - initialise objects

Hi guys, I am new to ninject, I am wondering how I can run custom initizlisation code when constructing the injected objects? ie. I have a Sword class which implements IWeapon, but I want to pass an hit point value to the Sword class constructor, how do I achieve that? Do I need to write my own provider? A minor question, IKernel kerne...

How to use references, avoid header bloat, and delay initialization?

I was browsing for an alternative to using so many shared_ptrs, and found an excellent reply in a comment section: Do you really need shared ownership? If you stop and think for a few minutes, I'm sure you can pinpoint one owner of the object, and a number of users of it, that will only ever use it during the owner's lifeti...

Non initialized variable in C#

Right, Another question from me: I have the following piece of code: class Foo { public Foo() { Bar bar; if (null == bar) { } } } class Bar { } Code gurus will already see that this gives an error. Bar might not be initialized before the if statement. So now I'm wondering: what IS th...

Error occurred during initialization of VM

I'm getting the following error when running an executable I created on a 64-bit machine using C++ code: "Error occurred during initialization of VM Unable to load native library: Can't find dependent libraries" My PATH (shown below) obviously points to jvm.dll since I have that file in both C:\Progra~1\Java\jdk1.6.0_17\jre\bin and C:\...

Initializing a Global Struct in C

What is the best way to accomplish the following in C? #include <stdio.h> struct A { int x; }; struct A createA(int x) { struct A a; a.x = x; return a; } struct A a = createA(42); int main(int argc, char** argv) { printf("%d\n", a.x); return 0; } When I try to compile the above code, the compiler reports th...

Basic C++ Speed (initialization vs adding) and comparison speed

I was curious if anyone knows which of the following executes faster (I know this seems like a weird question but I'm trying to shave as much time and resources as possible off my program.) int i=0; i+=1; or int i; i=1; and I also was curious about which comparison is faster: //given some integer i // X is some constant i < X+...

C++ invalid reference problem

Hi all, I'm writing some callback implementation in C++. I have an abstract callback class, let's say: /** Abstract callback class. */ class callback { public: /** Executes the callback. */ void call() { do_call(); }; protected: /** Callback call implementation specific to derived callback. */ virtual void do_call(...

How to init and malloc array to pointer on C

It looks like I have a memory leak when I try to initializ an array of pointers. This my code: void initLabelTable(){ register int i; hashNode** hp; labelHashTable = (hashNode**) malloc(HASHSIZE*sizeof(hashNode*)); hp = labelHashTable; for(i=0; i<HASHSIZE; i++) { *(hp+i) = NULL; } } Any idea? ...