default-constructor

Writing a Default Constructor Forces Zero-Initialization?

左心房为你撑大大i 提交于 2019-12-01 08:41:57
问题 These are my class definitions: class Foo{ int _ent; public: void printEnt() const{cout << _ent << ' ';} }; class Bar{ Foo _foo; public: void printEnt() const{_foo.printEnt();} }; And this is my test code: char* buf = new char[sizeof(Foo) + sizeof(Foo) + sizeof(Bar)]; fill(buf, buf + sizeof(Foo) + sizeof(Foo) + sizeof(Bar), 'J'); cout << ((int*)buf)[0] << ' ' << ((int*)buf)[1] << ' ' << ((int*)buf)[2] << endl; Foo* first = new (buf) Foo; Foo* second = new (buf + sizeof(Foo)) Foo(); Bar* third

Will the compiler-generated default constructor be public?

为君一笑 提交于 2019-12-01 04:26:23
When I write a class Widget.java public class Widget { int data; String name; } will the compiler-generated constructor be public or default ? public would be like public class Widget { int data; String name; public Widget() {} } whereas default similar to public class Widget { int data; String name; Widget() {} } It depends on your class visibility .The compiler uses the class visibility and generates a no-arg default constructor with the same visibility As said in JLS If a class contains no constructor declarations, then a default constructor that takes no parameters is automatically

Why does the String class not have a parameterless constructor?

一笑奈何 提交于 2019-12-01 02:58:37
int and object have a parameterless constructor. Why not string ? Greg Update: To provide more information for you. You don't have an empty Constructor with a string , however you do have String.Empty . The reason is because a string is an immutable object every instance of a string you modify is actually creating a new string in memory. For instance: string name = ""; though it is an empty string it will still hold around twenty bytes . Where the string.Empty will only hold around four or eight bytes . So though they mean the same thing, one is more efficient than the other. However I believe

Will the compiler-generated default constructor be public?

隐身守侯 提交于 2019-12-01 01:54:26
问题 When I write a class Widget.java public class Widget { int data; String name; } will the compiler-generated constructor be public or default ? public would be like public class Widget { int data; String name; public Widget() {} } whereas default similar to public class Widget { int data; String name; Widget() {} } 回答1: It depends on your class visibility .The compiler uses the class visibility and generates a no-arg default constructor with the same visibility 回答2: As said in JLS If a class

Default constructor c++

你。 提交于 2019-12-01 01:01:44
I am trying to understand how default constructor (provided by the compiler if you do not write one) versus your own default constructor works. So for example I wrote this simple class: class A { private: int x; public: A() { std::cout << "Default constructor called for A\n"; } A(int x) { std::cout << "Argument constructor called for A\n"; this->x = x; } }; int main (int argc, char const *argv[]) { A m; A p(0); A n(); return 0; } The output is : Default constructor called for A Argument constructor called for A So for the last one there is another constructor called and my question is which

Design without default constructor

不羁的心 提交于 2019-11-30 20:20:27
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 Authentication public BaseClass(string _UserName, string _Password) { UserName = _UserName; Password =

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

房东的猫 提交于 2019-11-30 20:17:26
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 some variadic template expansion voodoo perhaps? Note: Answers may use anything in C++11, C++14 or C++17

Why does a class used as a value in a STL map need a default constructor in …?

 ̄綄美尐妖づ 提交于 2019-11-30 20:09:01
Below is the class used as the value in a map: class Book { int m_nId; public: // Book() { } <----- Why is this required? Book( int id ): m_nId( id ) { } }; Inside main(): map< int, Book > mapBooks; for( int i = 0; i &lt 10; ++i ) { Book b( i ); mapBooks[ i ] = b; } The statement causing the error is: mapBooks[ i ] = b; If I add a default constructor, the error does not appear. However, I don't understand why the need. Can anyone explain? If I use insert() , the problem does not appear. By the way, I'm using Visual C++ 2008 to compile. operator[] performs a two step process. First it finds or

Default constructor for an inherited class

你。 提交于 2019-11-30 18:21:14
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 modify or add public members to the classes. Actually what you are looking for is the member

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

六月ゝ 毕业季﹏ 提交于 2019-11-30 17:13:51
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 ‘A::A()’ B(B &&b) {} ^ move_without_default.cc:6:2: note: declared here A() = delete; ^ Why is this an