constructor

Constructor overload contains already defines a member with the same signature

大憨熊 提交于 2021-02-17 05:49:33
问题 public Module(string a, object obj) : this(a, null, obj) { } public Module(string b, object obj) : this(null, b, obj) { } These constructor overloads do not work 'already defines a member with same parameter types' I have looked around and realise that I cannot do this in c# but can anyone suggest a way around this? Edit: Thanks for answers. In this case I have decided that to go with this for now: public Module(string a, object obj) : this(a, null, obj) { } public Module(string a, string b,

Creating an object inside an if statement [duplicate]

两盒软妹~` 提交于 2021-02-17 05:42:13
问题 This question already has answers here : Create Object in if-statement and use it later (3 answers) Closed 3 years ago . I'm trying to make a simple program for a bank account. I have created a class called Bank to make and instance and in the main class, where the main method is, I have made an if statement which will create an instance of the class "Bank" depending on the conditions met. The problem is that I can use the instance methods, which is outside the if statement. I have created

Why can't you instantiate the same object of that class inside constructor?

懵懂的女人 提交于 2021-02-17 05:15:52
问题 public class Run{ public static void main(String... args){ A a1 = new A(); } } class A{ public A(){ A a = new A(); } //here as well A a = new A(); } Why does this give a java.lang.StackOverflowError ? Is there a recursive call happening here? How does it happen? 回答1: You're calling the constructor inside the constructor--that's what new does, constructs a new object. 回答2: Is there a recursive call happening here? Yup How it happens? When you new A() , it calls the constructor for A , which

How to use both default and custom copy constructor in C++?

送分小仙女□ 提交于 2021-02-16 15:56:18
问题 I have a long class with a lot of data members. I want to write a copy constructor for it. But, if I write my own copy constructor, I lose access to the default copy constructor. I just want to repair a few pointers in my own copy constructor. So I want to have a shallow copy of the object which can be done by the default copy constructor. Is there a possibility to access the default copy constructor when I have my own copy constructor? 回答1: Wrap the things you don't want to change in a

Why does assignment operator call constructor?

天涯浪子 提交于 2021-02-16 15:55:28
问题 I am just playing around to understand smart pointers and trying to make mine but I come across a situation that I do not fully understand. Here is the code: #include <iostream> template <class T> class Holder { private: T * obj; public: Holder(T * tt) : obj(tt) { std::cout << "ctor : " << tt->dummy << std::endl; } T * operator -> () { return obj; } operator bool() { return obj; } T * const get() const { return obj; } void reset() {swap(0);} void swap(T * other) { obj = other; } Holder &

How to simplify multiple constructors?

北城以北 提交于 2021-02-16 08:40:13
问题 I would like to have two constructors for a class, as follows: public MyClass() { // do stuff here } public MyClass(int num) { MyClass(); // do other stuff here } Is the above the correct way to achieve my purpose? Is there some kind of shorthand which is better? 回答1: public MyClass() { // do stuff } public MyClass(int num) : this () { // do other stuff with num } The : this() bit is called a Constructor Initialiser . Every constructor in C# has a an initialiser which runs before the body of

How to simplify multiple constructors?

[亡魂溺海] 提交于 2021-02-16 08:35:47
问题 I would like to have two constructors for a class, as follows: public MyClass() { // do stuff here } public MyClass(int num) { MyClass(); // do other stuff here } Is the above the correct way to achieve my purpose? Is there some kind of shorthand which is better? 回答1: public MyClass() { // do stuff } public MyClass(int num) : this () { // do other stuff with num } The : this() bit is called a Constructor Initialiser . Every constructor in C# has a an initialiser which runs before the body of

Ternary operator + C++11 constructor from initializer_list

泄露秘密 提交于 2021-02-16 05:39:11
问题 While developing an application, I had the following problem. I wanted to return an empty std::list<string> when a given function pointer was null, or the result of that function otherwise. This is a simplified version of my code: typedef std::list<std::string> (*ParamGenerator)(); std::list<std::string> foo() { /* ... */ ParamGenerator generator = ...; if(generator) return generator(); else return {}; } However, I usually like to use the ternary ( ?: ) operator in these cases, so I tried

Ternary operator + C++11 constructor from initializer_list

这一生的挚爱 提交于 2021-02-16 05:37:02
问题 While developing an application, I had the following problem. I wanted to return an empty std::list<string> when a given function pointer was null, or the result of that function otherwise. This is a simplified version of my code: typedef std::list<std::string> (*ParamGenerator)(); std::list<std::string> foo() { /* ... */ ParamGenerator generator = ...; if(generator) return generator(); else return {}; } However, I usually like to use the ternary ( ?: ) operator in these cases, so I tried

difference between copy constructor and assignment operator

Deadly 提交于 2021-02-11 14:21:47
问题 I have gone through [question] (What's the difference between assignment operator and copy constructor?) and understood difference between a copy constructor and assignment operator. Now my question is although copy constructor initializes the previously uninitialized object where as assignment operator replaces data from previously initialized object what's the difference in terms of final outcome. I am thinking that final outcome in both case comes out to be same right? In the end after