constructor

Passing “this” to a function from within a constructor?

那年仲夏 提交于 2019-12-31 19:43:11
问题 Can I pass "this" to a function as a pointer, from within the class constructor, and use it to point at the object's members before the constructor returns? Is it safe to do this, so long as the accessed members are properly initialized before the function call? As an example: #include <iostream> class Stuff { public: static void print_number(void *param) { std::cout << reinterpret_cast<Stuff*>(param)->number; } int number; Stuff(int number_) : number(number_) { print_number(this); } }; void

How many constructors should a class have?

喜夏-厌秋 提交于 2019-12-31 13:33:09
问题 I'm currently modifying a class that has 9 different constructors. Now overall I believe this class is very poorly designed... so I'm wondering if it is poor design for a class to have so many constructors. A problem has arisen because I recently added two constructors to this class in an attempt to refactor and redesign a class (SomeManager in the code below) so that it is unit testable and doesn't rely on every one of its methods being static. However, because the other constructors were

How many constructors should a class have?

…衆ロ難τιáo~ 提交于 2019-12-31 13:33:07
问题 I'm currently modifying a class that has 9 different constructors. Now overall I believe this class is very poorly designed... so I'm wondering if it is poor design for a class to have so many constructors. A problem has arisen because I recently added two constructors to this class in an attempt to refactor and redesign a class (SomeManager in the code below) so that it is unit testable and doesn't rely on every one of its methods being static. However, because the other constructors were

C++ - initializing variables in header vs with constructor

强颜欢笑 提交于 2019-12-31 09:13:42
问题 Regarding the following, are there any reasons to do one over the other or are they roughly equivalent? class Something { int m_a = 0; }; vs class Something { int m_a; Something(int p_a); }; Something::Something(int p_a):m_a(p_a){ ... }; 回答1: The two code snippets you posted are not quite equal . class Something { int m_a = 0; }; Here you specify the value with which to initialise, i.e. 0 , at compile time. class Something { int m_a; Something(int p_a); }; Something::Something(int p_a):m_a(p

constructor does not run

允我心安 提交于 2019-12-31 07:34:47
问题 I do not understand because when you create an object of the "Users" class not the message is printed containing the constructor. class users { public: users(); private: int i; }; users::users () { cout<<"hello world"; } int main () { users users1(); return 0; } 回答1: users users1(); doesn't declare an object of the users class, it declares a function that takes no arguments and returns an object of the users class. To declare an object, use: users users1; 回答2: class users { public: users();

Java class with no constructor?

强颜欢笑 提交于 2019-12-31 06:37:45
问题 I have this class class Customer{ int ID; Time arriveTime; Time serviceTime; Time completeTime; int transaction; } Don't I need a constructor in order to set the values inside? I will be using this class to hold different values while making an array of Customers. Wouldn't this mean I would need this in order to set the values? public Customer(int id, Time arrive, Time service, Time complete, int trans){ ID = id; arriveTime = arrive; serviceTime = service; completeTime = complete; transaction

Java class with no constructor?

本秂侑毒 提交于 2019-12-31 06:36:28
问题 I have this class class Customer{ int ID; Time arriveTime; Time serviceTime; Time completeTime; int transaction; } Don't I need a constructor in order to set the values inside? I will be using this class to hold different values while making an array of Customers. Wouldn't this mean I would need this in order to set the values? public Customer(int id, Time arrive, Time service, Time complete, int trans){ ID = id; arriveTime = arrive; serviceTime = service; completeTime = complete; transaction

How to add no-argument constructor of inbuilt class for Firebase-database exception? [duplicate]

ε祈祈猫儿з 提交于 2019-12-31 06:35:41
问题 This question already has an answer here : Class android.location.Location does not define a no-argument constructor (1 answer) Closed last year . I am trying to fetch the FirebaseLocationData object from Firebase Realtime Database in Android. According to Docs, the class should have no arguments constructor which i have done as public FirebaseLocationData() {} but it is still showing error com.google.firebase.database.DatabaseException: Class android.location.Location does not define a no

constructor not accepting my information

。_饼干妹妹 提交于 2019-12-31 05:30:10
问题 so the constuctor is saying ) expected, error not a statement and ; expected Person num1, num2, num3; num1=new Person(Allison, 6600 Crescent Ave, 32, 9024231421); num2=new Person(George, 5251 Lakewood St, 24, 9024489216); num3=new Person(Michael, 2429 Inglis St, 56, 9024212345); object class public Person() { } //constructor allows programmer to define variable values in demo class public Person(String nm, String adr, int ag, long phn) { name=nm; address=adr; age=ag; phoneNumber=phn; } 回答1:

How do I refrence a non- static method from a static context

徘徊边缘 提交于 2019-12-31 05:19:09
问题 I have this constructor with a function however when i compile my main method i non-static method area cannot be referenced from a static context. I know its a simple fix i just cant quite get there. Thanks public class Rect{ private double x, y, width, height; public Rect (double x1, double newY, double newWIDTH, double newHEIGHT){ x = x1; y = newY; width = newWIDTH; height = newHEIGHT; } public double area(){ return (double) (height * width); } and this main method public class TestRect{