How would you call the constructor of the following class in these three situations: Global objects, arrays of objects, and objects contained in another class/struct?
The class with the constructor (used in all three examples):
class Foo {
public:
Foo(int a) { b = a; }
private:
int b;
};
And here are my attem...
Is it better to initialize class member variables on declaration
private List<Thing> _things = new List<Thing>();
private int _arb = 99;
or in the default constructor?
private List<Thing> _things;
private int _arb;
public TheClass()
{
_things = new List<Thing>();
_arb = 99;
}
Is it simply a matter of style or are there perform...
Hi *,
As an c# developer I'm used to run through constructors:
class Test {
public Test() {
DoSomething();
}
public Test(int count) : this() {
DoSomethingWithCount(count);
}
public Test(int count, string name) : this(count) {
DoSomethingWithName(name);
}
}
Is there a way to do this in...
One of the tenants of DDD is to not allow your objects to enter an invalid state. To me this means there shouldn't be a public parameterless constructor because that's going to be an object in an invalid state 99% of the time.
Is this a good way to move forward? It becomes a huge PITA when you just want to new-up a class real quick.
...
After creating a instance of a class, can we invoke the constructor explicitly?
For example
class A{
A(int a)
{
}
}
A instance;
instance.A(2);
Can we do this?
...
I am working on an object factory to keep track of a small collection of objects. The objects can be of different types, but they will all respond to createInstance and reset. The objects can not be derived from a common base class because some of them will have to derive from built-in cocoa classes like NSView and NSWindowController.
I...
I have a structure which I create a custom constructor to initialize the members to 0's. I've seen in older compilers that when in release mode, without doing a memset to 0, the values are not initialized.
I now want to use this structure in a union, but get errors because it has a non-trivial constructor.
So, question 1. Does the de...
I have a BasePage class which all other pages derive from:
public class BasePage
This BasePage has a constructor which contains code which must always run:
public BasePage()
{
// Important code here
}
I want to force derived classes to call the base constructor, like so:
public MyPage
: base()
{
// Page specific code h...
I may be going about this backwards...
I have a class which is like a document and another class which is like a template. They both inherit from the same base class and I have a method to create a new document from a template (or from another document, the method it is in the base class).
So, if I want to create a new document from a t...
I just stumbled across this bug in some legacy code:
class MyAPIHandler
{
private:
int handle;
public:
void MyApiHandler() // default constructor
{
handle = 42;
};
};
It compiles fine, with no warnings - but the behaviour wasn't what I intended, because the constructor name is misspelt. This by itself wo...
This question regards unit testing in Visual Studio using MSTest (this is important, because of MSTest's execution order). Both the method marked [TestInitialize] and the test class constructor will run before each test method.
So, the question is, what do you tend to do in each of these areas? Do you avoid performing certain activities...
I have two classes, Foo and Bar, that have constructors like this:
class Foo
{
Foo()
{
// do some stuff
}
Foo(int arg)
{
// do some other stuff
}
}
class Bar : Foo
{
Bar() : base()
{
// some third thing
}
}
Now I want to introduce a constructor for Bar that takes an int, but I wa...
class C {
public
T x;
};
Is there an elegant way for the constructor of x to know implicitly in what instance of C it is constructing?
I've implemented such behavior with some dirty inelegant machinery. I need this for my sqlite3 wrapper. I don't like all wrappers I've seen, their API IMO ugly and inconvenient. I wa...
Every now and then, I bump into syntax that I've seen before, but never used. This is one of those times.
Can someone explain the purpose of ":this" or ":base" following a C# constructor method?
For example:
public MyClass(SomeArg arg) : this(new SomethingElse(), arg)
{
}
My gut feeling is that it is used to map a default argument o...
I have two constructor :
function clsUsagerEmailUserName($nickName, $email)
{
$this->nickName = $nickName;
$this->email = $email;
}
function clsUsagerEmailUserName($email)
{
$this->email = $email;
}
But this is not working? What's wrong, isn't supposed to be OO at this version of ...
All the member variables and member functions in my class ClassA are static.
If a user is trying (by mistake) to create an object of this class, he receives a warning: "ClassA, local variable never referenced", because all the functions are static, so this object is never referenced. So, I want to prevent the user from trying to create...
Why must class members declared as const be initialized in the constructor initializer list rather than in the constructor body?
What is the difference between the two?
...
Do you need to unit test constructors, say I have a ctor like this,
IMapinfoWrapper wrapper;
public SystemInfo(IMapinfoWrapper mapinfoWrapper)
{
this.wrapper = mapinfoWrapper;
}
Do I need to write a unit test for this ctor? I don't have any getters for the wrapper variable, so I don't need t...
I have a class hierarchy, this one:
type
TMatrix = class
protected
//...
public
constructor Create(Rows, Cols: Byte);
//...
type
TMinMatrix = class(TMatrix)
private
procedure Allocate;
procedure DeAllocate;
public
constructor Create(Rows, Cols: Byte);
constructor CreateCopy(var t...