default-constructor

init boost::optional of non-copyable object

浪尽此生 提交于 2019-12-05 02:04:01
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::make_shared< T > . It seems to me, that my problem can be solved by means of using of std::unique_ptr

Cython and constructors of classes

天涯浪子 提交于 2019-12-04 12:42:26
I have a problem with Cython usage of default constructors. My C++ class Node is the following Node.h class Node { public: Node() { std::cerr << "calling no arg constructor" << std::endl; w=0.0; d=0.0; } Node(double val, double val2); { std::cerr << "calling 2 args constructor" << std::endl; this->w=val; this->d=val2; } private: double d,w; } is wrapped in Cython as follows cdef extern from "Node.h": cdef cppclass Node: Node() except + Node(double val1, double val2) except + double d double w cdef class pyNode: cdef Node *thisptr # hold a C++ instance which we're wrapping def __cinit__(self):

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

て烟熏妆下的殇ゞ 提交于 2019-12-04 10:38:25
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? Kon The default (no-parameter) constructor is ONLY provided if you have provided no others. If you define even a single constructor for your class, you MUST use one of the explicitly defined (ie, in your code) constructors to

C++ default constructors absence and I cannot compile

∥☆過路亽.° 提交于 2019-12-04 05:57:01
问题 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

Default constructor not being called c++ OOP

99封情书 提交于 2019-12-04 05:04:01
问题 So I'm making a program in c++ to handle vectors, and it's mostly there, but I just wanted to test it, so I have this: class vector3 { protected: double x,y,z; public: // Default 3 vector Constructor vector3() { cout << "Default constructor called." << endl; x=y=z=0; } vector3(double xin, double yin, double zin) { cout << "parametrised constructor called." << endl; x=xin; y=yin; z=zin; } }; (there's more stuff, things for << etc) and as main() I have: int main() { vector3 vec1(); cout <<

Are empty constructors always called in C++?

China☆狼群 提交于 2019-12-04 04:46:38
I have a general question, that may be a little compiler-specific. I'm interested in the conditions under which a constructor will be called. Specifically, in release mode/builds optimised for speed , will a compiler-generated or empty constructor always be called when you instantiate an object? class NoConstructor { int member; }; class EmptyConstructor { int member; }; class InitConstructor { InitConstructor() : member(3) {} int member; }; int main(int argc, _TCHAR* argv[]) { NoConstructor* nc = new NoConstructor(); //will this call the generated constructor? EmptyConstructor* ec = new

Will default-constructing an integer array zero-initialize it?

爱⌒轻易说出口 提交于 2019-12-04 04:24:59
If I have a structure with an array member, and I explicitly call the default constructor of the array in the structure's constructor, will the elements get default-constructed? (In the case of an integer array, this would mean getting zero-initialized). struct S { S() : array() {} int array[SIZE]; }; ... S s; // is s.array zero-initialized? A quick test with gcc suggests that this is the case, but I wanted to confirm that I can rely on this behaviour. (I have noticed that if I don't explicitly default-construct the array in the structure constructor, the array elements have random values.)

shouldn't std::pair<T1,T2> have trivial default constructor if T1 and T2 have?

核能气质少年 提交于 2019-12-04 02:49:08
问题 I ran into a problem because std::is_trivially_default_constructible<std::pair<T1,T2>>::value == false; even if std::is_trivially_default_constructible<T1>::value == true; std::is_trivially_default_constructible<T2>::value == true; I failed to find a good reason for this design. Wouldn't it appropriate for std::pair<T1,T2> to have a =default constructor if T1 and T2 have? Is there a simple work around (simpler than defining my own pair<> )? 回答1: The simple reason is: history! The original std

Difference between default-initialize and value-initialize in C++03?

好久不见. 提交于 2019-12-03 19:59:50
问题 I had always thought that creating a new object would always call the default constructor on an object, and whether the constructor was explicit or automatically generated by the compiler made no difference. According to this highly regarded answer to a different question, this changed in a subtle way between C++98 and C++03 and now works like so: struct B { ~B(); int m; }; // non-POD, compiler generated default ctor new B; // default-initializes (leaves B::m uninitialized) new B(); // value

Default constructor does not initialize the instance members of the class?

橙三吉。 提交于 2019-12-03 11:35:13
I encountered a question that asks "Which of the following are true about the "default" constructor?" and an option "It initializes the instance members of the class." was incorrect choice. Now my understanding was that if we have a code such as Class Test { String name; } then the compiler creates default constructor that looks like Class Test { String name; Test(){ super(); name = null; } } Isn't the default constructor initializing the instance member name=null ? The class constructor is not the one doing the initialization, the JVM does this. After memory for the object is created, the