constructor

Defaulted constructor vs implicit constructor

一个人想着一个人 提交于 2019-12-19 21:20:30
问题 It's possible that someone has already asked about this but googling for "default", "defaulted", "explicit" and so on doesn't give good results. But anyway. I already know there are some differences between an explicitly defined default construtor (i.e. without arguments) and an explicitly defined defaulted constructor (i.e. with the keyword default ), from here: The new keyword =default in C++11 But what are the differences between an explicitly defined defaulted constructor and implicitly

Defaulted constructor vs implicit constructor

余生颓废 提交于 2019-12-19 21:20:12
问题 It's possible that someone has already asked about this but googling for "default", "defaulted", "explicit" and so on doesn't give good results. But anyway. I already know there are some differences between an explicitly defined default construtor (i.e. without arguments) and an explicitly defined defaulted constructor (i.e. with the keyword default ), from here: The new keyword =default in C++11 But what are the differences between an explicitly defined defaulted constructor and implicitly

Class base() constructor and pass this

不羁岁月 提交于 2019-12-19 20:43:42
问题 I am trying to implement good design patterns for a program I am writing. I have a class structure like this. abstract class SomeBase { public SomeObject obj { get; protected set; } protected SomeBase(SomeObject x) { obj = x; } //Other methods and fields... } public class SomeDerived : SomeBase { public SomeDerived() : base(new SomeObject(this)) { } } Now as I;m sure you now, you can't pass this in a contructor because the object hasn't bee initialized. But I was really hoping there was a

Reference to uninitialized object iniside constructor

我与影子孤独终老i 提交于 2019-12-19 19:58:45
问题 It is possible to pass uninitialized object to a parent class like in the following example class C { public: C(int i): m_i(i) {}; int m_i; } class T { public: T(C & c): m_c(c) { }; C & m_c; }; class ST : public T { public: ST(): T(m_ci), m_ci(999) { }; C m_ci; }; In class T constructor, c is a reference to uninitialized object. If class T were using c object during construction, this would possibly lead to an error. But since it's not, this compiles and works fine. My question is - does it

Construction a vector from the concatenation of 2 vectors

偶尔善良 提交于 2019-12-19 19:44:12
问题 Is there a way to construct a vector as the concatenation of 2 vector s (Other than creating a helper function?) For example: const vector<int> first = {13}; const vector<int> second = {42}; const vector<int> concatenation = first + second; I know that vector doesn't have an addition operator like string , but that's the behavior that I want. Such that concatenation would contain: 13 and 42. I know that I can initialize concatenation like this, but it prevents me from making concatenation

What is benefit of this constructor definition [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-19 19:38:09
问题 This question already has answers here : In this specific case, is there a difference between using a member initializer list and assigning values in a constructor? (12 answers) Closed 6 years ago . I was just going through random pages on Cprogramming.com and noticed the Constructors and Destructors tutorial/example page. They have used the following method of defining a constructor: class String { private: char *str; int size; public: String() : str(NULL), size(0) { } // <- This statement

Create an object in the constructor or at top of the class

痴心易碎 提交于 2019-12-19 19:12:33
问题 which declaration/instantiation is better and WHY ? public class MainWindow { private Test _test; public MainWindow() { _test = new Test(); } } OR public class MainWindow { private Test _test = new Test(); public MainWindow() { } } 回答1: Ask yourself this question: what happens when you add other constructors to MainWindow ? Do you want to then have to remember to invoke other constructors to ensure that _test is correctly initialized? Or is it okay for _test to not be initialized if another

Java setting private fields inside constructors

百般思念 提交于 2019-12-19 17:05:49
问题 Common design practice is to make instance variables private and have public getters and setters to access them. But many times I have seen code samples on the internet that have constructors that assign values directly to the private instance variable instead of using the setters inside constructors. Am I missing something? public class Person{ private String name; public Person(String name){ //is this right, seems like the whole encapsulation purpose is defeated this.name = name; //shouldn

In kotlin, how to make the setter of properties in primary constructor private?

非 Y 不嫁゛ 提交于 2019-12-19 14:23:41
问题 In kotlin, how to make the setter of properties in primary constructor private? class City(val id: String, var name: String, var description: String = "") { fun update(name: String, description: String? = "") { this.name = name this.description = description ?: this.description } } I want the setter of properties name to be private, and the getter of it public, how can I do? 回答1: The solution is to create a property outside of constructor and set setter's visibility. class Sample(var id: Int,

In kotlin, how to make the setter of properties in primary constructor private?

烂漫一生 提交于 2019-12-19 14:23:27
问题 In kotlin, how to make the setter of properties in primary constructor private? class City(val id: String, var name: String, var description: String = "") { fun update(name: String, description: String? = "") { this.name = name this.description = description ?: this.description } } I want the setter of properties name to be private, and the getter of it public, how can I do? 回答1: The solution is to create a property outside of constructor and set setter's visibility. class Sample(var id: Int,