constructor

Can I pass a delegate into a constructor of an abstract class from a descendant's constructor?

I have an abstract class which requires a delegate to function. I pass the delegate into the constructor. Now that I have a non default constructor I need to call the abstract class's constructors from the concrete class which means that I need to use MyBase.New(...). I have included a quick example below. Public MustInherit Class BaseC...

Does a StringBuilder initialized with a string contain exactly (only) enough space for that string?

I'm wondering if this code ... StringBuilder sb = new StringBuilder("Please read the following messages."); ... initializes sb with a buffer exactly as large as the string passed to the constructor. On the one hand, this would seem the most logical thing. On the other hand, it seems to kind of defeat the purpose of the StringBuilder c...

Mass assignment on construction from within ruby.

Possible Duplicate: Idiomatic object creation in ruby Sometimes it's useful to assign numerous of a constructed arguments to instance variables on construction. Other than the obvious method: def initialize(arg1, arg2, arg3) @arg1, @arg2, @arg3 = arg1, arg2, arg3 end Is there a more concise idiom for achieving the same re...

C++ g++ thread-safety constructors

Given: class Foo { Foo() {}; }; class Bar { static int counter; Bar() { ++counter; } } It's clear that Foo::Foo is thread safe whereas Bar::bar is not. Furthermore, it's clear that if a function is written in such a way so that it's not thread-safe, then clearly putting it in a constructor makes that constructor not thread saf...

If changing a const object is undefined behavior then how do constructors and destructors operate with write access?

C++ standard says that modifying an object originally declared const is undefined behavior. But then how do constructors and destructors operate? class Class { public: Class() { Change(); } ~Class() { Change(); } void Change() { data = 0; } private: int data; }; //later: const Class object; //object.Change(); - won't co...

How to create a C-style array without calling default constructors?

I am writing a memory-managing template class in which I want to create a C-style array of fixed size, to serve as a heap. I keep the objects stored in an array like this: T v[SIZE]; As this only serves the role as a heap that can hold T objects, I don't want the T default constructor to get automatically called for every object in th...

Try/catch blocks inside constructors

Hi, Is it a bad programming practice to have try/catch blocks inside constructors? Or does it make no difference as long as our programs handle typeinitializer exceptions gracefully. In C# if there are any exceptions inside a constructor the framework always throws typeinitilizer exceptions. Thanks, Shamika ...

Pass current object type into base constructor call

How do I grab the Type of the inherited class and pass it into the base constructor of the class also inherited? See the code sample below: // VeryBaseClass is in an external assembly public abstract class VeryBaseClass { public VeryBaseClass(string className, MyObject myObject) { } } // BaseClass and InheritedClass are in...

Initialize parent's protected members with initialization list (C++)

Is it possible to use the initialization list of a child class' constructor to initialize data members declared as protected in the parent class? I can't get it to work. I can work around it, but it would be nice if I didn't have to. Some sample code: class Parent { protected: std::string something; } class Child : public Parent...

Calling a method in a Javascript Constructor and Accessing Its Variables

I am trying to call a method from the constructor of my javascript constructor, is this possible and if so, I can't seem to get it working, any insight would be great! Thanks! function ValidateFields(pFormID){ var aForm = document.getElementById(pFormID); this.errArray = new Array();//error tracker this.CreateErrorList(); } ...

Preventing implicit cast of numerical types in constructor in C++

I have a constructor of the form: MyClass(int a, int b, int c); and it gets called with code like this: MyClass my_object(4.0, 3.14, 0.002); I would like to prevent this automatic conversion from double to int, or at least get warnings at compile time. It seems that the "explicit" keyword does not work in these case, right? ...

Declaring/passing structs vs. declaring/passing individual values

I have two questions, actually. But they are very closely related. Suppose I have a simple struct like this: public struct TradePrice { public float Price; public int Quantity; public TradePrice(float price, int quantity) { Price = price; Quantity = quantity; } } Question 1 Now if I have code somewhe...

Abstract constructor in C#

Why I can't declare abstract an constructor of my class like this: public abstract class MyClass { public abstract MyClass(int param); } ...

How to instantiate a Socket class in Java?

I know that the Socket class can be instantiated in this way (as an example): new Socket("taranis", 7); where "taranis" is the name of the server in a local network. In this respect I have two questions: 1. If my computers do not form a local network but are connected to the Internet, can I use the IP addresses of computers to instan...

Multiple Constructors for a Class Problem

I am used to doing this in C++. Is this not allowed in C#? BasicCtor(int a) { return BasicCtor(a, "defaultStringValue"); } BasicCtor(int a, string b) { //blah blah } In C# I can neither return a calling of the constructor or call it w/o a return. Does C# allow what I want to do? :P ...

Why and when should use a Copy Constructor in C++? Why shallow copy is discouraged?

Lets say we create an object of class Student, we create two instances/objects class Student(i.e. StudentA and StudentB). We do shallow copy to initialize data members of B with that of A as follows: Student StudentB = StudentA; And then we destroy StudentB. Are we facing a situation of Dangling Pointer here? Why and how? Please explai...

Constructor in class with inheritance

I'm having some problems with inheritance and constructors in C++. What I've got is a class VirtualMotor which inherits Motor (is that the correct way to say it?). The class VirtualMotor should have it's own constructor, but I'm doing something wrong when I create it and the compiler gives me an error (se below). My source code is like t...

Proper usage of "this." keyword in C#?

Hi all, I'm working through the book Head First C# (and it's going well so far), but I'm having a lot of trouble wrapping my head around the syntax involved with using the "this." keyword. Conceptually, I get that I'm supposed to use it to avoid having a parameter mask a field of the same name, but I'm having trouble actually tracking ...

Different ways of constructing an object in C++.

I want to construct an object in the stack, using C++. Do you know what is the difference between these to ways of calling the constructor (with and without parenthesis): a) MyClass object ; b) MyClass object() ; I am using MFC and when constructing the global variable for the main app, if I use the latter way, I get an exception, I...

Some basic questions on constructors (and multiple-inheritance) in C++?

(I’m sorry if this has been asked before; the search feature seems to be broken: the results area is completely blank, even though it says there are a few pages of results… in Chrome, FireFox, and Safari) So, I’m just learning C++… and the book I’m moving through is doing a really bad job of explaining constructors in a way that I can g...