default-constructor

compiler generated constructors [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-11-30 08:25:30
问题 This question already has answers here : Conditions for automatic generation of default/copy/move ctor and copy/move assignment operator? (3 answers) Closed 2 years ago . This is just a quick question to understand correctly what happens when you create a class with a constructor like this: class A { public: A() {} }; I know that no default constructor is generated since it is already defined but are copy and assignment constructors generated by the compiler or in other words do i need to

Design without default constructor

被刻印的时光 ゝ 提交于 2019-11-30 04:50:30
问题 I want to restrict creating object using default constructor. Because I have a desing like below: class Program { static void Main(string[] args) { BaseClass bc = new BaseClass("",""); XmlSerializer xml = new XmlSerializer(typeof(BaseClass)); StreamWriter sw = new StreamWriter(File.Create("c:\\test.txt")); xml.Serialize(sw,bc); sw.Flush(); sw.Close(); } } [Serializable] public class BaseClass { public string UserName, Password; // I don't want to create default constructor because of

Initializing an std::array of non-default-constructible elements?

微笑、不失礼 提交于 2019-11-30 03:51:39
问题 Suppose type foo_t with a named constructor idiom, make_foo() . Now, I want to have exactly 123 foo's - no more, no less. So, I'm thinking about an std::array<foo_t, 123> . Now, if foo_t were default-constructible, I would write: std::array<foo_t, 123> pity_the_foos; std::generate( std::begin(pity_the_foos), std::end(pity_the_foos), []() { return make_foo(); } ); and Bob's my uncle, right? Unfortunately... foo_t has no default ctor. How should I initialize my array, then? Do I need to use

Default constructor for an inherited class

余生颓废 提交于 2019-11-30 02:38:06
问题 I've reduced my problem down to the following example code: class pokemon{ public: pokemon(int n); }; class MewTwo : public pokemon { public: MewTwo(int n); }; MewTwo::MewTwo(int n) {} Which produces an error: no matching function for call to ‘pokemon::pokemon()’ What I think is happening is that a default constructor for pokemon is called when I try to write the MewTwo constructor, which doesn't exist. I'm relatively new to C++ so I'm just guessing here. Any ideas? Restraint: Fixes cannot

Why does a move constructor require a default constructor for its members?

。_饼干妹妹 提交于 2019-11-30 01:20:18
问题 I was trying to implement a move constructor for a class without a copy constructor. I got an error that the default constructor for a member of the class was missing. Here's a trivial example to illustrate this: struct A { public: A() = delete; A(A const&) = delete; A(A &&a) {} }; struct B { A a; B() = delete; B(B const&) = delete; B(B &&b) {} }; Trying to compile this, I get: move_without_default.cc: In constructor ‘B::B(B&&)’: move_without_default.cc:15:11: error: use of deleted function

Do I really need to define default constructor in java?

假如想象 提交于 2019-11-29 20:18:07
It works fine when constructors are not defined, but gives errors if I define a parameterized constructor and not a default one and not passing any values while creating an object . I thought constructors are predefined. Why do I need to define a default constructor if I've defined a parameterized constructor? Ain't default constructor predefined? A default (no-argument) constructor is automatically created only when you do not define any constructor yourself. If you need two constructors, one with arguments and one without, you need to manually define both. Sahil J While all the answers above

How to mock the default constructor of the Date class with JMockit?

柔情痞子 提交于 2019-11-29 15:48:05
I want to mock the default constructor of java.util.date so it does not construct a Date object representing the time when it was created, but always the same Date object (in my example below 31 Dec 2010). I tried doing this with JMockit and JUnit , but when executing my test below, the output is always Thu Jan 01 01:00:00 CET 1970 . So what is wrong with my mock of Date() ? import java.util.Date; import org.junit.*; import mockit.*; public class AppTest { @Before public void setUp() { Mockit.setUpMocks(MockedDate.class); } @After public void tearDown() { Mockit.tearDownMocks(); } @Test public

c++ Constructor initializer list with complex assignments

旧时模样 提交于 2019-11-29 14:44:29
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 them so you will have to copy code (and make several copies of the same object) to fit all the code in

Is there a reason to explicitly code a default constructor when there are no other constructors?

纵饮孤独 提交于 2019-11-29 13:29:05
I recently saw this constructor in a class: public MyClass(){ } There were no other constructors. Is there a reason for this? Java automatically creates a default constructor, so why would you declare one explicitly? Or is this considered good practice in the same way as using braces for single-statement if statements - in case other constructors are added later and you forget that you don't have a default...? A couple minor points that aren't likely to be why you saw it in this case. It gives you something to set a breakpoint on. You could make it non-public As far as "in case other

Explicitly defaulted move constructor

醉酒当歌 提交于 2019-11-29 06:58:16
According to the c++11 standard a default move constructor is only generated if: X does not have a user-declared copy constructor, and X does not have a user-declared copy assignment operator, X does not have a user-declared move assignment operator, X does not have a user-declared destructor, and the move constructor would not be implicitly defined as deleted. Can I still explicitly default it? Seems to work correctly in clang. Like this for example: class MyClass { private: std::vector<int> ints; public: MyClass(MyClass const& other) : ints(other.ints) {} MyClass(MyClass&& other) = default;