constructor

New constructors for classes with Scala rich wrapping (implicit)

时光总嘲笑我的痴心妄想 提交于 2019-12-10 15:32:58
问题 In Scala, you can "add new methods" to existing classes by creating wrapper class and using "implicit def" to convert from the original class to the rich wrapper class. I have a java library for graphics that uses plenty of constructors with looong lists of floats. I would love to add new constructors to these classes with rich wrapping but this doens't seem to work like the above for methods. In other words, I would like to have simpler constructors but still to be able to keep using the

Java constructor final variable assignment

北城余情 提交于 2019-12-10 15:28:04
问题 public class User { private final String _first_name; private final String _last_name; private final String _org_ID; private final TimeZone _time_zone; private final InternetAddress _email; private final Date _last_login; private final Date _creation_date; public User( final String org_ID, final String username, final String first_name, final String last_name, final List<String> roles, final TimeZone time_zone, final InternetAddress email, final Date last_login, final Date creation_date ) {

How to properly use tag dispatching to pick a constructor

本秂侑毒 提交于 2019-12-10 15:14:30
问题 I'm trying to implement a set of mutex and lock classes for an embedded system. I've never used tag dispatching before, and I'm not sure if I'm doing it right. The description included in the boost documentation simply has this: struct input_iterator_tag { }; and uses it to pick a specialized template function. I don't have a template, I just want to pick a specific constructor based on the presence of a tag: // in mutex.h: struct TryLock { }; // defined nowhere, i.e. there's no cpp file for

c++ call specific template constructor of template class

柔情痞子 提交于 2019-12-10 15:08:00
问题 Is it possible to call a constructor with template arguments if the class is a template too? #include <stdio.h> #include <iostream> template <class A> struct Class { template <class B> Class(B arg) { std::cout << arg << std::endl; } }; int main() { Class<int> c<float>(1.0f); Class<int>* ptr = new Class<int><float>(2.0f); return 0; } edit: so I guess the only way to call a specific template constructor is to call it with casted paramterers to the template type you want: #include <stdio.h>

Is it a code smell to have a special constructor only used during testing?

旧城冷巷雨未停 提交于 2019-12-10 15:04:33
问题 Assume I have a class Foo which is only instantiated with an instance of class Bar : public Foo(Bar x) { this.a = x.a(); this.b = x.b(); ... } Now I would like to test Foo , further assuming an instance of Bar with the desired state is difficult to create. As an additional constraint, the fields a, b, ... are declared as final, so setters for this fields are not available. A possibility would be to create an additional constructor in Foo: protected Foo(A a, B b, ...) { this.a = a; this.b = a;

constructor initialization list execution order with delegated constructors

给你一囗甜甜゛ 提交于 2019-12-10 14:57:09
问题 I have a tricky C++ question: When you have a constructor initialization list with delegated constructors, what is the list execution order? There exist two conflicting standard rules here: 1.) The constructor initialization list gets executed NOT by the list order but by the declaration order of the items. 2.) Delegated constructors in the constructor initialization list always get called before the "mother constructor" is executed. Which rule is superior? (since a constructor is a class

.ctor is ambiguous because multiple kinds of members with this name exist in class

旧街凉风 提交于 2019-12-10 14:56:38
问题 I am replicating a situation that I am facing. Let's say we have an assembly, with C# class as: public class Program { int n = 0; public void Print() { Console.WriteLine(n); } public Program() { } public Program(int num = 10) { n = num; } } We refer the above assembly in VB.NET project and trying to create an instance of the Program class: Module Module1 Sub Main() Dim p As New Program() p.Print() p = New Program(20) p.Print() Console.ReadLine() End Sub End Module The VB.NET project is not

Calling Class Constructor in Member Function

感情迁移 提交于 2019-12-10 14:24:53
问题 Here's a program, where I am trying to call the class constructor multi::multi(int, int) , in the function void multi::multiply() . The output is 30 30 instead of expected 30 25 Why? #include <iostream.h> class multi{ private: int a; int b; public: multi(int m, int n){ a = m; b = n; } void multiply(){ cout << "\n\n" << a*b; multi (5, 5); cout << "\n" << a*b; } }; main(){ multi x(5,6); x.multiply(); return 0; } 回答1: multi (5, 5); It creates a temporary object, and gets destroyed by the end of

Circular dependency in constructor initialization list

杀马特。学长 韩版系。学妹 提交于 2019-12-10 14:24:11
问题 Is the following well-defined? class A; class B; // define A, which takes B& in constructor // define B, which takes A& in constructor class C { A a; B b; public: C() : a(b), b(a) { /* stuff with a and b */ } } Full example at ideone.com. Is it safe/well-defined so long as the constructors for A and B don't do anything with the references they get? 回答1: N4140 [class.cdtor]/1 reads: For an object with a non-trivial constructor, referring to any non-static member or base class of the object

Can Typescript infer the type of an instance of an extension class instantiated by a method of its base?

孤街浪徒 提交于 2019-12-10 14:18:35
问题 Consider the following Typescript snippet: class Animal { constructor(name: string) { this.name = name; } name: string; haveBaby(name: string): ?? return type ?? { return new this.constructor(name); // Error } } class Cat extends Animal {} class Dog extends Animal {} class Gerbil extends Animal {} // etc. let sorachi = new Cat("Sorachi"); // a: Cat let sorachiJr = a.haveBaby("Sorachi Jr."); // I want: sorachiJr: Cat Animals can have babies, and a baby should be the same kind of animal as the