constructor

Implementing a strategy for the constructor

What is the best way to implement a strategy for the constructor of a template/abstract class in C#? I have several classes which are all based on parsing a string inside the constructor. The parsing is done in a static method which creates list of key value pairs and is common for all classes, but some fields are also common for all cla...

Acceptable answers for an interview question

What kind of answers would you accept to the following question "Describe the process and/or pitfall of throwing exceptions from constructor and destructors" (C++/C#/java) What amount of knowledge about this would you consider essential, for a candidate claiming to have several years of experience in any of these languages (if he misse...

c++ compiling error related to constructor/destructor definition.

Hello, I'm trying to define the constructor and destructor of my class but I keep getting the error: definition of implicitly-declared 'x::x()'. What does it mean? BestWishes! Part of the code: ///Constructor StackInt::StackInt(){ t = (-1); stackArray = new int[20]; }; ///Destructor StackInt::~StackInt(){ delete[] stackArr...

How to instantiate an object with a private constructor in C#?

I definitely remember seeing somewhere an example of doing so using reflection or something. It was something that had to do with SqlParameterCollection which is not creatable by a user (if I'm not mistaken). Unfortunately cannot find it any longer. Can anyone please share this trick here? Not that I consider it a valid approach in deve...

Should you check for wrong parameter values in the constructor?

Do you check for data validity in every constructor, or do you just assume the data is correct and throw exceptions in the specific function that has a problem with the parameter? ...

"No appropriate default constructor available" error in Visual C++

I don't get it. I've been staring at the code the code for three hours and I can't see the problem. The class I'm creating, called TwoDayPackage is derived from a class called Package. This is how I defined the constructor: TwoDayPackage(string, string, string, string, int, string, string, string, string, int, float, float, float)...

Will the initialization list always be processed before the constructor code?

Will the initialization list always be processed before the constructor code? In other words, will the following code always print <unknown>, and the constructed class will have "known" as value for source_ (if the global variable something is true)? class Foo { std::string source_; public: Foo() : source_("<unknown>") { std::c...

Spring.NET & Constructor Interceptors

I'm trying to do some AOP over objects at construction time, and found IConstructorInterceptor, which would be perfect for what I want but it doesn't appear to work (in version 1.2 at least). I've also looked at both the IObjectPostProcessor & the IInstantiationAwareObjectPostProcessor, but I can't find any way to do processing on an ob...

pass a reference to 'this' in the constructor

I know I have done this before but I am getting my constructor order of execution in a twist I think.... public class Class1 { Class2 _class2; public Class1() { _class2 = new Class2(this); } } public class Class2 { Class1 _parent; //corrected typo public Class2(Class1 parent) { _parent = p...

Execute a derived constructor before the base constructor in C#

My problem here is that I would like to pass an object to a derived class, but it must be done before the base class constructor, since the base class will immediately call the derived class's Start() method that uses the object. Here's an excerpt from the base class, (renamed from BarcodeScanner for convenience). public abstract class...

Multiple assertions when unit testing constructor?

I'm trying to unit test a class with 2 constructors. Each constructor has multiple parameters that set public properties. My question is, should I have only 2 unit tests with multiple asserts to check that each property was set OR a test for each parameter for each constructor? Public Person(string name, string phone, string birthday)...

What's the best technique for exiting from a constructor on an error condition in C++

What's the best technique for exiting from a constructor on an error condition in C++? In particular, this is an error opening a file. Thanks for the responses. I'm throwing an exception. Here's the code (don't know if it's the best way to do it, but it's simple) // Test to see if file is now open; die otherwise if ( !file.is_open() )...

What's the difference between an object initializer and a constructor?

What are the differences between the two and when would use an "object initializer" over a "constructor" and vice-versa? I'm working with C#, if that matters. Also, is the object initializer method specific to C# or .NET? ...

Copy constructors - c++

Can I write a copy constructor by just passing in a pointer instead of the const reference? (Would it be ok if I make sure that I am not going to change any values though?) Like so: SampleClass::SampleClass(SampleClass* p) { //do the necessary copy functionality } instead of: SampleClass::SampleClass(const SampleClass& copyObj) {...

Determine array size in constructor initializer

In the code below I would like array to be defined as an array of size x when the Class constructor is called. How can I do that? class Class { public: int array[]; Class(int x) : ??? { } } ...

Initializing Generic List In C#

In C# I can initialize a list using the following syntax. List<int> intList= new List<int>() { 1, 2, 3 }; I would like to how that {} syntax works, and if it has a name. There is a constructor that takes an Ienumerable, you could call that. List<int> intList= new List<int>(new int[]{ 1, 2, 3 }); That seems more "standard". When I d...

Concatenating C++ iterator ranges into a const vector member variable at construction time

I have a class X, which I provide a snippet of here: class X { public: template <typename Iter> X(Iter begin, Iter end) : mVec(begin, end) {} private: vector<Y> const mVec; }; I now want to add a new concatenating constructor to this class, something like: template <typename Iter1, typename Iter2> X(Iter1 begin1, Ite...

Handling a class with a long initialization list and multiple constructors?

I have a (for me) complex object with about 20 data member, many of which are pointer to other classes. So for the constructor, I have a big long, complex initialization list. The class also has a dozen different constructors, reflecting the various ways the class can be created. Most of these initialized items are unchanged between each...

Class design: public constuctor or private with static factory and a COM object.

Hi all, I am having a little bit a class design issue. I will outline the three patterns I am considering. Example 1: Class MyObject { public MyObject(IWrapper wrapper,string name) {//set properties.} public Void Declare() { //Some COM stuff with IWrapper } } Use: MyObject testobj = new MyObject(fake...

C# Constructor

I recently saw a C# constructor that look something like this... public Class foo { public foo() : this(new bar()) {} } Can anybody help me interpret this? where does the bar() fit in? If you could help me complete the class by inserting the bar() in its proper place so that I can compile/debug and see the whole picture. Than...