default-constructor

init boost::optional of non-copyable object

淺唱寂寞╮ 提交于 2019-12-06 19:22:53
问题 What should I do to initialize boost::optional< T > if underlying type T is non-default constructible, non-copyable/moveable, but one's instance still can exist? Is it forbidden for boost::optional by any semantic reasons to have some member function like template< typename... Args > boost::optional< T >::construct(Args && ...args) , that delivers all the arguments to in-place operator new to construct the object entirely (for non-ref type T )? Variant is to have non-member function like std:

Using default Constructors in java, even if the parameterized constructors are present [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-06 15:54:07
问题 This question already has answers here : Java default constructor (11 answers) Closed 3 years ago . I just wanted to clear my concept here, so i am asking... If I define an explicit parameterized constructor for my class, can I still invoke the default constructor provided by the java compiler (which is provided for every class by default) ?? Or does it cause result in a compile time error in such case?? Please explain what exactly happens with respect to the calls made by the compiler !! 回答1

what does default constructor do when it's empty?

本小妞迷上赌 提交于 2019-12-06 04:33:21
问题 I wonder if anyone could explain what the default ctor does after memory allocated, how it initializes the allocated memory? 回答1: I don't know which languange you asked the question for, but I will try to answer anyway for C++ and Java In C++, it : leaves the built-in types ( int , float , pointers, etc.) to an uninitialized value calls the default constructor on class members In Java, I think all class members are initialized to their default value (0 or NULL). 回答2: Default constructors

Default constructor for a class with a reference data member?

不羁岁月 提交于 2019-12-05 18:59:10
I have a class MyClass in which I need to create a std::array of std::vector in the default constructor. However, this class has a data member which is a reference (of type Something ) which also needs to be initialized in the constructor and I cannot do this in a default constructor. How should I solve this? class MyClass{ public: MyClass(); //Cannot instantiate s?? MyClass(Something& s); Something& s; } MyClass array[10]; // MyClass needs a default constructor but a default // constructor won't be able to initialize s A class with a reference member needs to set the reference in its

Understanding implicitly declared default costructor

99封情书 提交于 2019-12-05 13:40:01
I'm trying to understand how the compiler's default constructor works. I made this example: #include <iostream> class Base { public: int number; }; class Test1 : public Base { }; class Test2 { public: Base base; }; int main() { Test1 test1; Test2 test2; std::cout<<test1.number<<std::endl; std::cout<<test2.base.number<<std::endl; } The output of this test program is, for test1 0 , and for test2 is a uninitialized (random) number. Now my question is: why in the first case ( test1 ) the compiler's default constructor initialize number to 0 but for test2 it doesn't? Edit : According to answers

User-declared default constructor + in-class initializers != user-provided constructor? [duplicate]

时光毁灭记忆、已成空白 提交于 2019-12-05 13:01:34
问题 This question already has answers here : Why does C++ require a user-provided default constructor to default-construct a const object? (5 answers) Closed 2 years ago . The Clang documentation neatly explains that If a class or struct has no user-defined default constructor, C++ doesn't allow you to default construct a const instance of it like this ([dcl.init], p9) The rationale being that if a const object is not correctly initialized, it cannot be changed later on. The following code has

Detect compiler generated default constructor using reflection in C#

我只是一个虾纸丫 提交于 2019-12-05 10:59:40
I'm targeting .NET 3.5 SP1 and I'm using CommentChecker to validate my XML documentation, everything works OK until I get to a class like this: /// <summary> /// documentation /// </summary> public sealed class MyClass { /// <summary> /// documentation /// </summary> public void Method() { } } In the example above, as I understand, the compiler generates a default constructor for my class. The problem with this is that CommentChecker generates warnings telling me that the constructor is missing the comments. I tried to modify the program to detect this special case and ignore it but I'm stuck,

Should trivial default constructor respect default member initializer here?

北城以北 提交于 2019-12-05 09:31:59
Consider the code: #include <atomic> #include <iostream> struct stru { int a{}; int b{}; }; int main() { std::atomic<stru> as; auto s = as.load(); std::cout << s.a << ' ' << s.b << std::endl; } Note that although stru has default member initializer, it still qualifies as an aggregate type since C++14. std::atomic has a trivial default constructor. According to the standard, should the members of as be initialized to zero? clang 6.0.0 doesn't do this (see here ), while gcc 7.2.0 seems so (see here ). Strictly speaking, I think both compilers are right, in that your program exhibits undefined

Naming user controls without default constructors in XAML

和自甴很熟 提交于 2019-12-05 07:44:35
I have a user control without a parameterless constructor; let's call it WithoutDefaultConstructor . I want to insert a WithoutDefaultConstructor called myControl into the XAML code of another control (which is called MainWindow ). However, I get this compiler error: The type 'WithoutDefaultConstructor' cannot have a Name attribute. Value types and types without a default constructor can be used as items within a ResourceDictionary. How do I fix this without adding a parameterless constructor to WithoutDefaultConstructor ? Here are the contents of MainWindow.xaml : <Window x:Class=

Google Mock: “no appropriate default constructor available”?

风流意气都作罢 提交于 2019-12-05 05:27:40
Using Visual Studio 2010 C++ with googlemock. I'm trying to use a mock I created and I'm getting the compiler error on the line: EmployeeFake employeeStub; The error is: 1>c:\someclasstests.cpp(22): error C2512: 'MyNamespace::EmployeeFake' : no appropriate default constructor available EmployeeFake: class EmployeeFake: public Employee{ public: MOCK_CONST_METHOD0(GetSalary, double()); } Employee: class Employee { public: Employee(PensionPlan *pensionPlan, const char * fullName); virtual ~Employee(void); virtual double GetSalary() const; } I gather that the problem is that the base class doesn't