default-constructor

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

杀马特。学长 韩版系。学妹 提交于 2019-12-18 08:57:30
问题 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

Class member without a default constructor

…衆ロ難τιáo~ 提交于 2019-12-18 08:32:06
问题 Suppose I have a class A without a default constructor, a factory method factoryA that returns an object of type A, and a class B that has A as its member. I know that in this case the member of type A of B has to be initialize in B's constructor initialization list. It is not entirely clear to me why so if someone could explain that to me it would be great. Also, what if the parameter to A's constructor needs to be computed inside of B's constructor, say by querying a database or something

Explicitly defaulted move constructor

。_饼干妹妹 提交于 2019-12-18 04:48:13
问题 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>

C++ Initializing Non-Static Member Array

喜你入骨 提交于 2019-12-18 04:34:17
问题 I am working on editing some old C++ code that uses global arrays defined like so: int posLShd[5] = {250, 330, 512, 600, 680}; int posLArm[5] = {760, 635, 512, 320, 265}; int posRShd[5] = {765, 610, 512, 440, 380}; int posRArm[5] = {260, 385, 512, 690, 750}; int posNeck[5] = {615, 565, 512, 465, 415}; int posHead[5] = {655, 565, 512, 420, 370}; I want to make all of these arrays private members of the Robot class defined below. However, the C++ compiler does not let me initialize data members

Why is a POD in a struct zero-initialized by an implicit constructor when creating an object in the heap or a temporary object in the stack?

左心房为你撑大大i 提交于 2019-12-17 22:33:04
问题 The standard and the C++ book say that the default constructor for class type members is called by the implicit generated default constructor, but built-in types are not initialized. However, in this test program I get unexpected results when allocating an object in the heap or when using a temporary object: #include<iostream> struct Container { int n; }; int main() { Container c; std::cout << "[STACK] Num: " << c.n << std::endl; Container *pc = new Container(); std::cout << "[HEAP] Num: " <<

C++ default destructor

柔情痞子 提交于 2019-12-17 21:44:46
问题 When I don't declare a constructor for example, the compiler will provide me with a default constructor that will have no arguments and no definition (body), and thus, will take no action . If I now don't declare a destructor , the compiler will provide me with a default destructor with no defintion (body), and thus, I think no action . So, if I'm finished with an object for example, wouldn't the default destructor reallocate (free) memory used by the object? If it doesn't, why are we getting

Purpose of Explicit Default Constructors

故事扮演 提交于 2019-12-17 17:48:12
问题 I recently noticed a class in C++0x that calls for an explicit default constructor. However, I'm failing to come up with a scenario in which a default constructor can be called implicitly. It seems like a rather pointless specifier. I thought maybe it would disallow Class c; in favor of Class c = Class(); but that does not appear to be the case. Some relevant quotes from the C++0x FCD, since it is easier for me to navigate [similar text exists in C++03, if not in the same places] 12.3.1.3

Default initialization of std::array?

做~自己de王妃 提交于 2019-12-17 08:17:15
问题 With C++11 std::array , do I have the guarantee that the syntax std::array<T, N> x; will default-initialize all the elements of the array ? EDIT : if not, is there a syntax that will work on all arrays (including zero-sized arrays) to initialize all elements to their default value? EDIT : on cppreference, the default constructor description says: (constructor) (implicitly declared) (public member function) default-constructs or copy-constructs every element of the array so the answer may be

When is a private constructor not a private constructor?

丶灬走出姿态 提交于 2019-12-17 04:54:27
问题 Let's say I have a type and I want to make its default constructor private. I write the following: class C { C() = default; }; int main() { C c; // error: C::C() is private within this context (g++) // error: calling a private constructor of class 'C' (clang++) // error C2248: 'C::C' cannot access private member declared in class 'C' (MSVC) auto c2 = C(); // error: as above } Great. But then, the constructor turns out to not be as private as I thought it was: class C { C() = default; }; int

Should we always include a default constructor in the class?

只谈情不闲聊 提交于 2019-12-17 04:23:34
问题 I have been asked this question by a colleague that should we always include a default constructor in a class? If so, why? If no, why not? Example public class Foo { Foo() { } Foo(int x, int y) { ... } } I am also interested to get some lights on this from experts. 回答1: You have to keep in mind that if you don't provide an overloaded constructor, the compiler will generate a default constructor for you. That means, if you just have public class Foo { } The compiler will generate this as: