default-constructor

How to elegantly return an object that is default-initialized?

▼魔方 西西 提交于 2020-01-29 06:51:45
问题 I have a class like below: class VeryVeryVeryLongTypeName { bool is_ok; VeryVeryVeryLongTypeName() : is_ok(false) {} }; VeryVeryVeryLongTypeName f() { VeryVeryVeryLongTypeName v; ... // Doing something if (condition_1 is true) { return v; } else { return VeryVeryVeryLongTypeName(); } ... // Doing something if (condition_2 is true) { return v; } else { return VeryVeryVeryLongTypeName(); } } I think the statement return VeryVeryVeryLongTypeName(); is very tedious and ugly, so, my question is:

Why Default constructor need to declare in POJO file which has Parameterized Constructor while instantiating Object?

懵懂的女人 提交于 2020-01-23 01:17:09
问题 Suppose I have one POJO class User with a constuctor public User(int id, String name){...} . But when I instantiate the User object like User u=new User() with no parameter Eclipse gives error like The constructor User() is undefined . But it works fine when I have no parameterized Constructor. Can someone please explain why It requires to define default constructor? 回答1: The default (no-parameter) constructor is ONLY provided if you have provided no others. If you define even a single

Should (in C++11) std::vector::resize(size_type) work for the default constructible value_type int[4]?

本小妞迷上赌 提交于 2020-01-21 10:52:35
问题 In C++11, there are two versions of std::vector::resize() : void resize( size_type count ); void resize( size_type count, const value_type& value); I understand (as suggested by one of the comments to one of the answers to this question) that the first requires value_type to be default constructible, while the second requires it to be copy constructible. However, (gcc 4.7.0) using namespace std; typedef int block[4]; vector<block> A; static_assert(is_default_constructible<block>::value,";-(")

c++ Constructor initializer list with complex assignments

久未见 提交于 2020-01-20 08:06:33
问题 Suppose I want to have a constructor that receives some parameters, and with these parameters I can calculate the values for it's member variables. Except that the values for the member variables are not simple assignments from the parameters. They require creation of other objects and transformation of the values before they can be used as values for the member variables. This is way to much to cram into an initializer list. Also very inefficient since you can't create variables and reuse

Can the default constructor of std::list<int> throw?

百般思念 提交于 2020-01-14 12:46:07
问题 I had a (quick) look into the C++ standard and into an online C++ reference, but I could not find an answer to this simple question: Can the default constructor of std::list<int> throw? If so, why would it throw? 回答1: Short answer: it can , but it may be implemented in a way that is reasonably safe: The default constructor constructs an empty list, so there is little need to actually allocate memory in the process. Most list implementations won't allocate any memory for an empty list. However

Default constructor defined with default arguments outside the class definition, why does this work? and what happens with templates involved?

寵の児 提交于 2020-01-14 09:00:32
问题 I am aware this is bad form and that default-values should be specified in the declaration, but if you would please indulge me for a moment.. why does this compile? and what is happening exactly? #include <iostream> using namespace std; class test { public: test(int n); }; test::test(int n = 666) { cout << n; } int main() { test t; cin.sync(); cin.ignore(); return 0; } Output: 666 .. how do templates affect the same piece of code? template <class T> class test { public: test(int n); };

bean class instantiation in spring for a class without default constructor

时光毁灭记忆、已成空白 提交于 2020-01-11 13:32: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

Default constructor with empty brackets

时光总嘲笑我的痴心妄想 提交于 2020-01-06 23:42:31
问题 Is there any good reason that an empty set of round brackets (parentheses) isn't valid for calling the default constructor in C++? MyObject object; // ok - default ctor MyObject object(blah); // ok MyObject object(); // error I seem to type "()" automatically everytime. Is there a good reason this isn't allowed? 回答1: Most vexing parse This is related to what is known as "C++'s most vexing parse". Basically, anything that can be interpreted by the compiler as a function declaration will be

Default Constructor Visibility in Java [duplicate]

丶灬走出姿态 提交于 2020-01-06 11:45:46
问题 This question already has answers here : Default access modifier for a Java constructor (4 answers) Will the compiler-generated default constructor be public? (7 answers) Closed 4 years ago . I'm confused about the actual visibility of default constructors. I have been given the following code in a book that explains there is a default constructor created, but does not physically show one (i.e. Employee()), despite it being automatically assigned: public class Employee { private String name;

Strange behavior of default constructor in a class inherited from POD struct

为君一笑 提交于 2020-01-05 03:30:50
问题 This question relates to this one. As I mentioned in previous question I've decided to inherit my class from Win structure BITMAP to provide some extended functionality. I've noticed interest detail in compiled program behavior. First I have defined default constructor for my class like below: CPreviewFrame::CPreviewFrame(): m_bufferSize( 0 ) { bmBits = NULL; //ensure that compiler in debug won't init it with 0xccccc... and delete[] will do the job } In idea compiler had to generate code