constructor

Why shouldn't I use Thread.start() in the constructor of my class?

情到浓时终转凉″ 提交于 2019-12-17 20:51:16
问题 I've been searching for justification as for why you should not call a thread's start method inside a constructor for a class. Consider the following code: class SomeClass { public ImportantData data = null; public Thread t = null; public SomeClass(ImportantData d) { t = new MyOperationThread(); // t.start(); // Footnote 1 data = d; t.start(); // Footnote 2 } } ImportantData is some generic box of stuff (presumably important) and MyOperationThread is a subclass of thread that knows how to

How to throw an exception from an enum constructor?

只愿长相守 提交于 2019-12-17 20:43:55
问题 How can I throw an exception from an enum constructor? eg: public enum RLoader { INSTANCE; private RLoader() throws IOException { .... } } produces the error Unhandled exception type IOException 回答1: Because instances are created in a static initializer, throw an ExceptionInInitializerError instead. 回答2: I have a case where I want to use enums as keys in some settings classes. The database will store a string value, allowing us to change the enum constants without having to udpate the

error: no suitable constructor found for

狂风中的少年 提交于 2019-12-17 20:28:42
问题 I am new to java, and am trying to make a mod for Minecraft, but I can't figure out how I can fix this error: src\minecraft\net\minecraft\src\ThreadConnectToServer.java:39: error: no suitabl e constructor found for Packet2ClientProtocol(int,Minecraft,String,String,int) GuiConnecting.getNetClientHandler(this.connectingGui).addToSendQueue (new Packet2ClientProtocol(51, GuiConnecting.func_74254_c(this.connectingGui), t his.Username, this.ip, this.port)); ^ constructor Packet2ClientProtocol

No matching function for call to Class Constructor

限于喜欢 提交于 2019-12-17 19:48:38
问题 I am practicing my OOP and I have the following classes: Point and Circle. Specifically, Circle has a center Point, and a radius. Here is the relevant code: // Point.h class Point { public: Point(double x, double y); double x() const; double y() const; std::string as_string() const; private: double x_coord; double y_coord; }; // Circle.h class Circle { public: Circle(const Point& center, double radius); Point center() const; double radius() const; std::string as_string() const; std::string

Can I use apply() with constructor to pass arbitrary number of parameters

寵の児 提交于 2019-12-17 19:44:40
问题 I've got a function wich can accept a varible number of parameter with a rest operator. I want create an object passing the argument collected with the rest operator directly to a constructor without create an object and call an initializing function and without passing the entire array but the parameters ah I do with apply() function. Is it possible ? Using apply doesn't work. public function myFunc(...arg) { // something link "new MyClass.apply(args)" return new MyClass(); } 回答1:

Linker error LNK2001

て烟熏妆下的殇ゞ 提交于 2019-12-17 19:40:55
问题 When I try to create an object I get a LNK2001 error in Visual Studio, it's a problem with the constructor I think since changing the constructor changes the error. Customer bob("Bob", "25 Bob Lane", "01bob82", "M", "bob/bob/bob"); This line gives this error: Error 1 error LNK2001: unresolved external symbol "public: __thiscall Customer::Customer(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>

How to best explain and use empty Constructors in Java? [duplicate]

大兔子大兔子 提交于 2019-12-17 19:40:04
问题 This question already has answers here : Why might one also use a blank constructor? (10 answers) Closed 6 years ago . I have been self-learning Java. I understand the scope of defining a class, but still didn't get the concept of an empty constructor usage. Usually we should pass parameters for constructor to build instance object. But, I often see empty parameter for constructor. For example: class Person { String name; int age; public Person(); public Person(String name, int age){ this

Type initialization exception

邮差的信 提交于 2019-12-17 19:34:09
问题 I created imageHolder class: public class ImageHolder : Image<Bgr, Byte> { private String imagePath; public ImageHolder(String path):base(path) { this.imagePath = path; } public String imgPathProperty { get { return imagePath; } set { imagePath = value; } } } I create instance of the class and initialize it,like this: private ImageHolder originalImageHolder; originalImageHolder = new ImageHolder(openFileDialog.FileName); In runtime i get this exception: The type initializer for 'Emgu.CV

PDO using PDO::FETCH_PROPS_LATE and __construct() call?

℡╲_俬逩灬. 提交于 2019-12-17 19:29:50
问题 I'm trying to create a new instance of Setting object calling __construct() method with PHP PDO and constrain PDO::FETCH_PROPS_LATE . Unfortunatly i'm getting this warning (and binding doesn't work). How can pass column values to the constructor method? Warning: Missing argument 1 for Setting::__construct() in pdo.php. Notice: Undefined variable: key in pdo.php. class Setting { protected $key, $value, $displayable; public function __construct($key, $value = null, $displayable = 1) { $this-

Initializer list *argument* evaluation order

不羁的心 提交于 2019-12-17 18:55:51
问题 So, the C++ standard requires that class members be initialized in the order in which they are declared in the class, rather than the order that they're mentioned in any constructor's initializer list. However, this doesn't imply anything about the order in which the arguments to those initializations are evaluated. I'm working with a system that frequently passes references to serialization objects around, and wondering if I can ensure that bits are read from it in the right order,