constructor

Calling base constructor in c#

If I inherit from a base class and want to pass something from the constructor of the inherited class to the constructor of the base class, how do I do that? For example, If I inherit from the Exception class I want to do something like this: class MyExceptionClass : Exception { public MyExceptionClass(string message, string extr...

In C++ can constructor and destructor be inline functions?

VC++ makes functions which are implemented within the class declaration inline functions. If I declare a class Foo as follows, then are the CONSTRUCTOR and DESTRUCTOR inline functions? class Foo { int* p; public: Foo() { p = new char[0x00100000]; } ~Foo() { delete [] p; } }; { Foo f; (f); } ...

In a PHP5 class, when does a private constructor get called?

Let's say I'm writing a PHP (>= 5.0) class that's meant to be a singleton. All of the docs I've read say to make the class constructor private so the class can't be directly instantiated. So if I have something like this: class SillyDB { private function __construct() { } public static function getConnection() { } } A...

How can you require a constructor with no parameters for types implementing an interface?

Is there a way? I need all types that implement a specific interface to have a parameterless constructor, can it be done? I am developing the base code for other developers in my company to use in a specific project. There's a proccess which will create instances of types (in different threads) that perform certain tasks, and I need t...

How many constructor arguments is too many?

Let's say you have a class called Customer, which contains the following fields: UserName Email First Name Last Name Let's also say that according to your business logic, all Customer objects must have these four properties defined. Now, we can do this pretty easily by forcing the constructor to specify each of these properties. Bu...

Assigning a Boost Xpressive token iterator range to a vector

I'm having some trouble getting Boost Xpressive to work as I expect. I'm trying to split a line of text into fields delimited by tab characters: wstring ws = L"Field1\tField2\tField3"; wsregex_token_iterator fieldIt(ws.begin(), ws.end(), as_xpr(L'\t'), -1); wsregex_token_iterator endIt; So far, so good; the above works fine. The pr...

Virtual Constructors.

Is there any need of Virtual Constructors? If so can any one post a scenario? ...

When is it right for a constructor to throw an exception?

When is it right for a constructor to throw an exception? (Or in the case of Objective C: when is it right for an init'er to return nil?) It seems to me that a constructor should fail -- and thus refuse to create an object -- if the object isn't complete. I.e., the constructor should have a contract with its caller to provide a function...

Is there anything wrong with returning default constructed values?

Suppose I have the following code: class some_class{}; some_class some_function() { return some_class(); } This seems to work pretty well and saves me the trouble of having to declare a variable just to make a return value. But I don't think I've ever seen this in any kind of tutorial or reference. Is this a compiler-specific t...

C++ superclass constructor calling rules

What are the C++ rules for calling the superclass constructor from a subclass one?? For example I know in Java, you must do it as the first line of the subclass constructor (and if you don't an implicit call to a no-arg super constructor is assumed - giving you a compile error if that's missing). ...

What does the explicit keyword in C++ mean?

Someone posted in a comment to another question about the meaning of the explicit keyword in C++. So, what does it mean? ...

Using 'this' as a parameter to a method call in a constructor

I have a constructor like as follows: public Agent(){ this.name = "John"; this.id = 9; this.setTopWorldAgent(this, "Top_World_Agent", true); } I'm getting a null pointer exception here in the method call. It appears to be because I'm using 'this' as an argument in the setTopWorldAgent method. By removing this method call everythi...

overloading __init__ in python

Let's say I have a class that has a member called data which is a list. I want to be able to initialize the class with, for example, a filename (which contains data to initialize the list) or with an actual list. What's your technique for doing this? Do you just check the type by looking at __class__? Is there some trick I might be...

Will the below code cause memory leak in c++

class base{ int a; int *pint; someclass objsomeclass; someclass* psomeclass; public: base(){ objsomeclass = someclass(); psomeclass = new someclass(); pint = new int(); throw "constructor failed"; a = 43; } } main(){ base temp(); } in the above code constructor...

How can I prevent a base constructor from being called by an inheritor in C#?

I've got a (poorly written) base class that I want to wrap in a proxy object. The base class resembles the following: public class BaseClass : SomeOtherBase { public BaseClass() {} public BaseClass(int someValue) {} //...more code, not important here } and, my proxy resembles: public BaseClassProxy : BaseClass { public ...

Is it OK to put a database initialization call in a C# constructor?

I've seen this is various codebases, and wanted to know if this generally frowned upon or not. For example: public class MyClass { public int Id; public MyClass() { Id = new Database().GetIdFor(typeof(MyClass)); } } ...

Multi-Threaded Deep Copies

What's the best way to perform a deep copy in the constructor of an object that is part of multi-threaded C++ code? ...

Why is it an error to use an empty set of brackets to call a constructor with no arguments?

Is there any good reason that an empty set of brackets isn't valid for calling the default ctor in c++? MyObject object; // ok - default ctor MyObject object(blah); // ok MyObject object(); // error I seem to type "()" automatically everytime. I just wondered if there was a good reason this isn't allowed? ...

Default parameters with C++ constructors

Is it good practice to have a class constructor that uses default parameters, or should I use separate overloaded constructors? For example: // Use this... class foo { private: std::string name_; unsigned int age_; public: foo(const std::string& name = "", const unsigned int age = 0) : name_(name), age_(ag...

Should I use default(Foo), Foo.Empty, or null?

So C# now allows you to use default(Foo) to get a recognized "not filled in yet"/empty instance of a class -- I'm not sure if it is exactly the same as new Foo() or not. Many library classes also implement a Foo.Empty property, which returns a similar instance. And of course any reference type can point to null. So really, what's the ...