default-constructor

Unintuitive behaviour with struct initialization and default arguments

大憨熊 提交于 2019-12-03 11:01:35
public struct Test { public double Val; public Test(double val = double.NaN) { Val = val; } public bool IsValid { get { return !double.IsNaN(Val); } } } Test myTest = new Test(); bool valid = myTest.IsValid; The above gives valid==true because the constructor with default arg is NOT called and the object is created with the standard default val = 0.0. If the struct is a class the behaviour is valid==false which is what I would expect. I find this difference in behaviour and particularly the behaviour in the struct case suprising and unintuitive - what is going on? What does the default arg on

Default constructor/destructor outside the class?

夙愿已清 提交于 2019-12-03 10:51:08
Is the following legal according to the C++11 standard ( = default outside the definition of the class) ? // In header file class Test { public: Test(); ~Test(); }; // In cpp file Test::Test() = default; Test::~Test() = default; Yes, a special member function can be default-defined out-of-line in a .cpp file. Realize that by doing so, some of the properties of an inline-defaulted function will not apply to your class. For example, if your copy constructor is default-defined out-of-line, your class will not be considered trivially copyable (which also disqualifies it from being recognized as a

User Defined C++11 enum class Default Constructor

孤者浪人 提交于 2019-12-03 08:12:58
问题 Is there a way to specify the default constructor of an enum class ? I am using an enum class to specify a set of values which are allowable for a particular datatype in a library: in this case, it's the GPIO pin id numbers of a Raspberry Pi. It looks something like this: enum class PinID : int {N4 = 4, N17 = 17, /* ...etc... */ } The point of me doing this instead just of using, say, an int is to ensure that code is safe: I can static_assert (or otherwise compile-time ensure -- the actual

Why PHP has no default constructor? [closed]

孤人 提交于 2019-12-03 06:19:58
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Why can't I use code like this? <?php class NoConstructor { } class ChildWithConstructor extends NoConstructor { public function __construct() { parent::__construct(); // do something } } $foo = new ChildWithConstructor(); // **Fatal error: Cannot call

C# - Calling a struct constructor that has all defaulted parameters

拜拜、爱过 提交于 2019-12-03 05:58:06
I ran into this issue today when creating a struct to hold a bunch of data. Here is an example: public struct ExampleStruct { public int Value { get; private set; } public ExampleStruct(int value = 1) : this() { Value = value; } } Looks fine and dandy. The problem is when I try to use this constructor without specifying a value and desiring to use the defaulted value of 1 for the parameter: private static void Main(string[] args) { ExampleStruct example1 = new ExampleStruct(); Console.WriteLine(example1.Value); } This code outputs 0 and does not output 1 . The reason is that all structs have

User Defined C++11 enum class Default Constructor

笑着哭i 提交于 2019-12-02 21:51:52
Is there a way to specify the default constructor of an enum class ? I am using an enum class to specify a set of values which are allowable for a particular datatype in a library: in this case, it's the GPIO pin id numbers of a Raspberry Pi. It looks something like this: enum class PinID : int {N4 = 4, N17 = 17, /* ...etc... */ } The point of me doing this instead just of using, say, an int is to ensure that code is safe: I can static_assert (or otherwise compile-time ensure -- the actual method used is not important to me) things like that someone hasn't made a spelling error (passing a 5

Why default constructor is not provided by compiler when class contains parametrized constructor defined by user? [duplicate]

风流意气都作罢 提交于 2019-12-02 13:07:31
This question already has an answer here: Java default constructor 11 answers I am a newbie in java and was wondering "Why default constructor is not provided by compiler when class contains parametrized constructor defined by user?" When an author decides to not provide any constructor, it is perfectly fine that the compiler adds that default constructor. Obviously the user doesn't care "how" objects of that class are created, he accepts that the "default" kicks in. But as soon as the author writes down a constructor with parameters, it is also obvious that he assumes that one of his

bean class instantiation in spring for a class without default constructor

谁说我不能喝 提交于 2019-12-02 11:38:07
I am using a third party library class XYZ as an argument in my model. XYZ does not have a default constructor. So spring is not able to create bean for it giving error message as org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.data.mapping.model.MappingInstantiationException: Could not instantiate bean class [org.abc.def.XYZ]: No default constructor found;nested exception is java.lang.NoSuchMethodException: org.abc.def.XYZ./<init/>() org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:681

C++ default constructors absence and I cannot compile

妖精的绣舞 提交于 2019-12-02 09:29:14
I have this very simple class class myclass { public: int id; double x, y, z; myclass() = default; // If I omit this line I get an error myclass(int ID, double X, double Y, double Z): id(ID), x(X), y(Y), z(Z) {}; }; If I omit the line with the line myclass() = default; and then attempt at creating one object #include <vector> using namespace std; int main() { int ID = 0; double X = 1.0, Y = 2.0, Z = 3.0; vector<myclass> a_vector(10); myclass an_object(ID,X,Y,Z); return 0; } I get an error no matching function for call to ‘myclass::myclass() . Why does this happen? When is it mandatory to

What is the difference between Object b(); and Object b;?

懵懂的女人 提交于 2019-12-02 07:11:48
问题 To be more explicit, I get a compile time error when I try accessing an instance variable when I create an object using (), but when I don't, the code compiles and runs as expected. Also, this problem only applies to the default constructor. I would like to understand why. using namespace std; #include <iostream> class Student { public: int gpa; Student() { gpa = 4; } Student( int x ) { gpa = x; } }; int main() { Student zero; Student sally( 2 ); Student jack(); cout << zero.gpa << endl; /