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...
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...
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...
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...
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...
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...
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
...
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...
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...
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();
}
...
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?
...
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...
Why I can't declare abstract an constructor of my class like this:
public abstract class MyClass {
public abstract MyClass(int param);
}
...
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...
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
...
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...
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...
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 ...
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...
(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...