default-constructor

Creating instance of type without default constructor in C# using reflection

Take the following class as an example: class Sometype { int someValue; public Sometype(int someValue) { this.someValue = someValue; } } I then want to create an instance of this type using reflection: Type t = typeof(Sometype); object o = Activator.CreateInstance(t); Normally this will work, however becaus...

Why does the parameterless Guid constructor generate an empty GUID?

Why does the parameterless Guid constructor generate an empty GUID rather than default to a generated one as with Guid.NewGuid()? Is there a particular use for an empty Guid? ...

Default constructors and inheritance in Java

Hello, I have a question about default constructors and inheritance in Java. Generally, if you write a class and do not include any constructor, Java provides automatically for you a default constructor (one without parameters), which initializes all instance variables of the class (if there are any) with some default values (0, null,...

C++ standard list and default-constructible types

Why is that the single parameter constructor of std::list<T> requires T to be a default-constructible type? I mean the following code does not compile. struct Foo { // does not have default constructor. Foo (int i) {} } int main(void) { std::list<Foo> l(10); } It seems possible to use the construct and destroy idioms as they hav...

Default construction of elements in a vector

While reading the answers to this question I got a doubt regarding the default construction of the objects in the vector. To test it I wrote the following test code: struct Test { int m_n; Test(); Test(const Test& t); Test& operator=(const Test& t); }; Test::Test() : m_n(0) { } Test::Test(const Test& t) { m_n =...

Possible to use a singleton with a non-default constructor in C#?

Hello, I'm implementing a notification framework for one of my projects. As i want it to be very generic, the user can use several transport layers, so that he doesn't really need to care about using one delivery method (lets say WCF) or another (for eg ActiveMQ). The interface the user has access is of course independent of the deliver...

Blindly converting structs to classes to hide the default constructor?

I read all the questions related to this topic, and they all give reasons why a default constructor on a struct is not available in C#, but I have not yet found anyone who suggests a general course of action when confronted with this situation. The obvious solution is to simply convert the struct to a class and deal with the consequence...

How to read a value with operator>> but no default constructor to the value ?

template <class T> T Read () { T t; cin >> t; if (cin.fail()) { // ... } return t; } This generic code read value of type T with some additional error handling. It relies on having operator>> that can parse T, and in this way it is extensible to new types. What I didn't realize is that it relies on T having a default co...

Constructor with custom classes as arguments, throws 'No matching function for call to...'

Howdy all, I'm trying to create a constructor for a custom type, but for some reason, it's trying to call, what I'm guessing is the constructor in the constructor definition of anotehr class.. Couldn't find anything which fits the same symptoms I'm having in any other questions, also as I may not know what I'm looking for. When I call:...

Is it correct to use declaration only for empty private constructors in C++?

For example is this correct: class C { private: C(); C(const & C other); } or you should rather provide definition(s): class C { private: C() {}; C(const & C other) {}; } ? Thanks for the current answers. Let's extend this question - does compiler generate better code in one of this examples? I can im...

Overloading Default Construction with Initializer List

I need to know how to get something to work. I've got a class with a constructor and some constants initialized in the initializer list. What I want is to be able to create a different constructor that takes some extra params but still use the initializer list. Like so: class TestClass { const int cVal; int newX; TestClass(i...

Java: Instantiating a generic class with no default constructor

Hi, I am trying to do this: public class BaseTable<T extends TableEntry> { protected int mRows; protected int mCols; protected ArrayList<T> mEntries; public BaseTable(int rows, int cols) { mRows = rows; mCols = cols; mEntries = new ArrayList<T>(); for (int i = 0; i < rows; i++) ...

Create an InterfaceProxyWithoutTarget with a default constructor

Hi all, Using Castle.DynamicProxy, I "simply" would like to get an Interface-Proxy-Without-Target, but... With a default-constructor so I be able to reuse the proxy-type. UPDATE I mean doing something like... var proxy = generator.CreateInterfaceProxyWithoutTarget(typeof(TInterface) ...); var proxyType = proxy.GetType(); var newproxy...

How to refrain from CS2512 correctly

Please help me with the following problem: I have the following classes: class ChemicalElement { private: std::string _name; void Init(const std::string& name); public: ChemicalElement(const std::string& name); ChemicalElement(const ChemicalElement& ce); }; class CombinationRule { private: ChemicalElement _ce1; ...

Is there a reason to explicitly code a default constructor when there are no other constructors?

I recently saw this constructor in a class: public MyClass(){ } There were no other constructors. Is there a reason for this? Java automatically creates a default constructor, so why would you declare one explicitly? Or is this considered good practice in the same way as using braces for single-statement if statements - in case othe...

Why is my default constructor for array not getting called here?

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { A[] a = new A[10]; } } public class A { static int x; public A() { System...

Why does a class used as a value in a STL map need a default constructor in ... ?

Below is the class used as the value in a map: class Book { int m_nId; public: // Book() { } Inside main(): map for( int i = 0; i ++i ) { Book b( i ); mapBooks[ i ] = b; } The statement causing the error is: mapBooks[ i ] = b; If I add a default constructor, the error does not appear...

Compiler complains about BOOST_CHECK_THROW on constructor

The following does not compile: class Foo { public: Foo( boost::shared_ptr< Bar > arg ); }; // in test-case boost::shared_ptr< Bar > bar; BOOST_CHECK_THROW( Foo( bar ), std::logic_error ); // compiler error here The implementation of Bar does not matter. The compiler complains, that Foo does not have an appropriate default cons...

Why don't std::vector's elements need a default constructor?

And how can I write my own array class to not need a default constructor for its elements? Right now, when I do the new [] to allocate space, I need a default constructor. std::vector does not. How do they do this magic? ...

compiler generated constructors

This is just a quick question to understand correctly what happens when you create a class with a constructor like this: class A { public: A() {} }; I know that no default constructor is generated since it is already defined but are copy and assignment constructors generated by the compiler or in other words do i need to declar...