constructor

C++ - Constructing wrapper class with same syntax as wrapped data

拜拜、爱过 提交于 2020-01-24 12:31:47
问题 I'm making a template class that is a wrapper around some type of data. I would like to be able to set / construct this class the same way as I set that data when it's not wrapped. Here's the basic idea: template<typename T> class WrapperClass{ public: T data; WrapperClass(const T& _data) : data( _data) {} // others stuff }; Now with something like an integer, I can do this: WrapperClass<int> wrapped_data = 1; But with a struct or class I don't know how: struct SomeStruct{ int a, b, c;

If/else statements inside a java constructor

北战南征 提交于 2020-01-24 11:18:14
问题 This program wasn't working when I only had the if/else conditions in the Setters. I got a tip that I have to use them inside the Constructors too. Can someone explain to me.. why? Another Question: Do you place the if/else statements inside the Constructor or Setters? //Constructor public Invoice(String partNumber, String partDescription, int quantity, double pricePerItem) { super(); this.partNumber = partNumber; this.partDescription = partDescription; if (quantity <= 0) quantity = 0; else

Alternatives to using overridden methods in constructors, Java

拥有回忆 提交于 2020-01-24 10:22:13
问题 In a Java project I am coding I have ended up using methods that are overridden in constructors. Something like: class SuperClass { SuperClass() { intialise(); } protected void initialise() { //Do some stuff common to all subclasses methodA(); methodB(); } protected abstract void methodA(); protected abstract void methodB(); } class SubClass1() { SubClass() { super(); } protected void methodA() { //Do something } protected void methodB() { //Do something } } class SubClass2() { SubClass() {

How to emplace elements while constructing std::vector?

天大地大妈咪最大 提交于 2020-01-24 09:29:09
问题 I want to construct an std::vector with some elements having these elements constructed by some particular constructor rather than the default constructor. In other words I want to emplace the elements while constructing the vector. How can I do that? Consider this: struct Item { Item(double) {} Item(const Item&) = delete; Item(Item&&) = delete; }; std::vector<Item> vv(10, 3.14); // Fails! Tries to copy while I want to emplace. 回答1: Your Item class doesn't support copies nor moves. This will

Derived class explicit base constructor call

强颜欢笑 提交于 2020-01-23 18:21:15
问题 I am trying to learn C#. The below data is from a Microsoft C# help website. I don't understand this statement, "If a base class does not offer a default constructor, the derived class must make an explicit call to a base constructor by using base." I thought that if there is no default constructor for a class, C# will automatically assign default values to int, char or whatever is declared in a class. If a base class does not have a constructor and it has a child class, does the rule

Derived class explicit base constructor call

心已入冬 提交于 2020-01-23 18:21:06
问题 I am trying to learn C#. The below data is from a Microsoft C# help website. I don't understand this statement, "If a base class does not offer a default constructor, the derived class must make an explicit call to a base constructor by using base." I thought that if there is no default constructor for a class, C# will automatically assign default values to int, char or whatever is declared in a class. If a base class does not have a constructor and it has a child class, does the rule

C++ move constructor not called for rvalue reference [duplicate]

笑着哭i 提交于 2020-01-23 12:26:46
问题 This question already has answers here : On how to recognize Rvalue or Lvalue reference and if-it-has-a-name rule (3 answers) Closed 3 years ago . class MyClass { public: MyClass() { std::cout << "default constructor\n"; } MyClass(MyClass& a) { std::cout << "copy constructor\n"; } MyClass(MyClass&& b) { std::cout << "move constructor\n"; } }; void test(MyClass&& temp) { MyClass a(MyClass{}); // calls MOVE constructor as expected MyClass b(temp); // calls COPY constructor... not expected...? }

There are multiple good constructors and Room will pick the no-arg constructor. How to solve this warning

萝らか妹 提交于 2020-01-23 09:31:54
问题 I try to implement Room Persistent Database library in android kotlin project, but catch this warning at compile time. I don't know how to solve this warring. warning: There are multiple good constructors and Room will pick the no-arg constructor. You can use the @Ignore annotation to eliminate unwanted constructors. Auto Generated Class public final class Gender { ^ Kotlin Data Class import android.arch.persistence.room.Entity import android.arch.persistence.room.PrimaryKey @Entity data

Promisify imported class (constructor) with bluebird in ES6 + babel

安稳与你 提交于 2020-01-23 06:30:28
问题 Suppose I created or have a node.js library lib.js export class C { constructor(value, callback) { callback(false, `Hello ${value}`); } task(value, callback) { callback(false, "returned " + value); } } The important part is that the classes' constructor needs to accept a callback as it does database connections and file I/O. If I now import and use the library callback-style, everything is fine (see c1 below). I would really like to promisify the library where I use it to make object

How should I chain the constructors in a class hierarchy?

拈花ヽ惹草 提交于 2020-01-23 05:41:49
问题 We have the following class hierarchy: public class Base { public Base() { // do generic initialization } public Base(SomeClass param1) : this() { // init properties that require param1 } public Base(SomeClass param1, OtherClass param2) : this(param1) { // init properties that require param2 } // ... } public class Derived : Base { public Derived() { // do custom initialization } public Derived(SomeClass param1) : this() // ??? { // do custom initialization using param1 } public Derived