constructor

Why is only one implicit conversion allowed to resolve the parameters to a function in C++?

This works: #include<cstdio> class A{ public: A(int a):var(a){} int var; }; int f(A obj) { return obj.var; } int main() { std::cout<<f(23); // output: 23 return 0; } while this doesn't: #include<cstdio> class A{ public: A(int a, int b):var1(a), var2(b){} int var1, var2; }; int f(A obj) { return (...

Is it possible to bypass constructors when instantiating objects in Android

Do Android have any way to instantiate objects without calling any of its constructors? In Java, Sun have sun.reflect.ReflectionFactory.getReflectionFactory().newConstructorForSerialization(), in .Net we have System.Runtime.Serialization.FormatterServices.GetUninitializedObject() but I was not able to find anything like that in the And...

Move constructor/operator=

I'm trying to learn about new feature of C++ namely move constructor and assignment X::operaror=(X&&) and I found interesting example but the only thing I quite not even understand but more dissagree is one line in move ctor and assignment operator (marked in the code below): MemoryBlock(MemoryBlock&& other) : _data(NULL) , _leng...

SortedSet<T> and anonymous IComparer<T> in the constructor is not working

How come anonymous functions works as arguments on methods, but not in constructor arguments? If I create a List<string>, it has a Sort method with the following signature: public void Sort(IComparer<T> comparer) where the following works: List<string> list = new List<string>(); list.Sort( (a,b) => a.CompareTo(b) ); SortedSet ha...

Language literal support

Are there any languages that support easily extending the compiler to support new literals example such as for json/xml/yaml/different string encodings/extra number types. I understand that you can always fork the compiler, write a dsl but that's not quite what I'm asking about. I understand that some languages such as smalltalk and lisp...

How does mockito create an instance of the mock object

When i create a mock object of say class Employee. It doesnt call the constructor of Employee object. I know internally Mockito uses CGLIb and reflection, creates a proxy class that extends the class to mock. If it doesnt call the constructor of employee how is the mock instance of employee class created ? ...

How to select a partial object including a list with new() syntax in Hibernate

In my Hibernated system, I have a class Picture that has some simple data, a list of Installations and some (huge) binary data. The list of Installations is implemented with a join table, as it's a ManyToMany relation, and it's defined on the Installation side: @Entity public class Picture { @Id private long pictureId; private Str...

Overloaded constructor error

I have the following class: class Step(kind:TipoStep.Value, message:String = "", study:List[Formula] = List(), lastAdd:Set[Formula] = Set(), lastDel:Set[Formula] = Set(), add:List[Formula] = List(), del:List[Formula] = List() ) { def this(step:Step, ...

Creating a .NET object in IronRuby when a static .New() method is defined

It seems impossible to create an object using its default constructor when there is a static .New() method defined on the class: .NET class: public class Tester { public static void New() { Console.WriteLine("In Tester.New()"); } public Tester() { Console.WriteLine("In constructor"); } } IronR...

help with static variables in java

I have a class, class MyClass { private int val; public static final MyClass myObject = new MyClass(1); MyClass(int a){ val = a; } public int getVal(){ return val; } public MyClass func1(){ MyClass temp = myObject; temp.val = 2; return temp; } public static void...

C#: if a class has two constructors, what is the best way for these constructors to share some code?

C# in VS2005: if a class has two constructors, what is the best way for these constructors to share some code? eg. How could I avoid having the x = 5 and y = 10 lines in both constructors for the following: public class MyObject { int x; int y; int z; public MyObject() { x = 5; y = 10; } public MyObject(int setZ) { x = ...

C# in VS2005: will this() execute the base constructor code for an inherited class?

For C# in VS2005, will calling this() in an inherited class cause the execution of the base constructor? EDIT: How can I avoid having to re-write the x and y assignments? Note, I do not want the MyObject(int num) constructor to execute the base() contructor. public class MyObject : MyParentObject { int x; int y; int z; public M...

C++ : Invoking indirect base class constructor without boilerplate code ?

Hi. Here's what I thought of doing : A - Structure { Position Name } B1 - Vehicle { Fuel } B2 - Building { -nothing- } foo - Car foo2 - Truck bar - Hospital bar2 - Market It's impossible to declare constructors like the following since A is not the direct base class : foo(x,y,z):A(x,y,z) bar(a,b,c):A(a,b,c) So I tried doing...

Why java has a requirement that the first line of constructor should call parent constructor? Are there any pitfalls if we bypass the requirement?

I have the next code: class Foo { public Foo (String param) { ... } } class Bar extends Foo { public Bar () { super (doSmth()); ... } private static String doSmth () { //what I can NOT do here? } } I wonder is it safe? Are there any limitations about doSmth method? ...

why default constructor is not available by default in some case

class foo { public: int a; int b; foo(int a_, int b_) : a(a_), b(b_) {} }; int main() { foo f; } when I try to compile the above code snippet, I got error message as below: foo.cc: In function 'int main()' foo.cc:12: error: no matching function for call to 'main()::foo::foo()' foo.cc:10: note: candidates are: main()::foo...

Object instance without new?

As we know we can instantiate an object without new keyword by using classloader/object cloning/object serialization. When I am using these techniques to create an object, is the constructor called or not? ...

How can I force a Constructor to be defined in all subclass of my abstract class.

Hi, I have an abstract class A that define abstract methods. This means that, for a class to be instanciable, all the abstract method have to be implemented. I'd like all my subclasses to implement a constructor with 2 ints as parameters. Declaring a constructor defeats my purpose, as I want the constructor defined in subclasses and ...

WPF DataGrid and parameterless constructors

I have a class "MyEntity", which does not have a default constructor (well, it does, yet it is not suitable for use). I have a form with a DataGrid, which has a cool feature for creating new rows. Problem: DataGrid cannot create new object when no parameterless constructor is defined. Question: is there a way to provide the DataGrid w...

const reference must be initialized in constructor base/member initializer list

I am trying to block access to the default constructor of a class I am writing. The constructor I want others to use requires a const reference to another object. I have made the default constructor private to prevent others from using it. I am getting a compiler error for the default constructor because the const reference member var...

Is there a convention for using Inherited Constructors.

I know that when inheriting classes, you can also inherit Constructors example: class Book : Genre { Book(string param1, int param2, string inherit1, string inherit2) : Base(inherit1,inherit2) { Prop1 = param1 Prop2 = param2 } } But wouldn't it just be easier to set the inherited properties via the constru...