constructor

Why am I getting an “inaccessible due to protection level” error?

本小妞迷上赌 提交于 2020-01-05 00:40:50
问题 I am getting this error: 'CTest.A.A()' is inaccessible due to its protection level. when compiling this code: public class A { private A() { } } public class B : A { public void SayHello() { Console.WriteLine("Hello"); } } Can anyone explain why? 回答1: Because the default constructor for A is private, try protected A() {} as the constructor. Class B automatically calls the default constructor of A , if that is inaccessible to B or there is no default constructor (if you have constructor

implicit constructor conversion works on explicit vector::vector, only sometimes

笑着哭i 提交于 2020-01-04 14:04:06
问题 I like to initialize 2-dimensional arrays as vector<vector<int> >(x,y) . x is passed to vector<vector<int> > 's constructor and y is passed to vector<int> 's constructor, x times. Although this seems to be forbidden by C++03, because the constructor is explicit , it always works, even on Comeau. I can also call vector::assign like this. But, for some reason, not vector::push_back . vector< vector< int > > v( 20, 40 ); // OK: convert 40 to const T& v.assign( 30, 50 ); // OK v.push_back( 2 ); /

implicit constructor conversion works on explicit vector::vector, only sometimes

坚强是说给别人听的谎言 提交于 2020-01-04 14:03:23
问题 I like to initialize 2-dimensional arrays as vector<vector<int> >(x,y) . x is passed to vector<vector<int> > 's constructor and y is passed to vector<int> 's constructor, x times. Although this seems to be forbidden by C++03, because the constructor is explicit , it always works, even on Comeau. I can also call vector::assign like this. But, for some reason, not vector::push_back . vector< vector< int > > v( 20, 40 ); // OK: convert 40 to const T& v.assign( 30, 50 ); // OK v.push_back( 2 ); /

What does initializer_list do?

丶灬走出姿态 提交于 2020-01-04 07:03:04
问题 I'm currently tasked with creating a definition for a special custom class my professor provided to us; I've never seen initializer_list in use, nor did my professor go over it. What exactly does it do? template<class T> LinkedQueue<T>::LinkedQueue(const std::initializer_list<T>& il) { } I have to define the constructor, which I will do on my own. I just want to know what this parameter does. Also, my compiler keeps telling me that std::initializer_list member declaration is not found. I've

What does initializer_list do?

梦想与她 提交于 2020-01-04 07:02:15
问题 I'm currently tasked with creating a definition for a special custom class my professor provided to us; I've never seen initializer_list in use, nor did my professor go over it. What exactly does it do? template<class T> LinkedQueue<T>::LinkedQueue(const std::initializer_list<T>& il) { } I have to define the constructor, which I will do on my own. I just want to know what this parameter does. Also, my compiler keeps telling me that std::initializer_list member declaration is not found. I've

Proper way to declare and set a private final member variable from the constructor in Java?

安稳与你 提交于 2020-01-04 05:33:14
问题 There are different ways to set a member variable from the constructor. I am actually debating how to properly set a final member variable, specifically a map which is loaded with entries by a helper class. public class Base { private final Map<String, Command> availableCommands; public Base() { availableCommands = Helper.loadCommands(); } } In the above example the helper class looks like this: public class Helper { public static Map<String, Command> loadCommands() { Map<String, Command>

C# List<GenericClass>(100) Construction Principles

会有一股神秘感。 提交于 2020-01-04 05:28:39
问题 If I do the following: List<GenericClass> listObj = new List<GenericClass>(100); // Do I need this part too? for (int i = 0; i < 100; i++) { listObj[i] = new GenericClass(); } Basically I am asking if the C# compiler will automatically fire the GenericClass constructor for each of the 100 GenericClass objects in the list. I searched in the MSDN documentation as well as here on StackOverflow. Thanks for any help. 回答1: That's not how List works. When you specify a capacity, it's an initial

C# List<GenericClass>(100) Construction Principles

爷,独闯天下 提交于 2020-01-04 05:28:29
问题 If I do the following: List<GenericClass> listObj = new List<GenericClass>(100); // Do I need this part too? for (int i = 0; i < 100; i++) { listObj[i] = new GenericClass(); } Basically I am asking if the C# compiler will automatically fire the GenericClass constructor for each of the 100 GenericClass objects in the list. I searched in the MSDN documentation as well as here on StackOverflow. Thanks for any help. 回答1: That's not how List works. When you specify a capacity, it's an initial

Calling class T's implementation of pure virtual from T constructor without qualification?

本小妞迷上赌 提交于 2020-01-04 05:28:20
问题 Considering that a virtual call of a T member function (directly or indirectly) from a constructor of a class T , can at most go down to T 's implementation, does the following code, with unqualified call , have Undefined Behavior or not? Note, to avoid noise: if you believe that member functions are not called virtually when invoked from a constructor, then please don't answer or comment here, but raise that issue in a separate SO question. Thank you. struct Baze { virtual void foo();

Conversion operator vs constructor from given type. Which is preferable?

两盒软妹~` 提交于 2020-01-04 05:25:15
问题 I'm defining iterator types for my container and of course I want iterator to be convertible to const_iterator . But I'm not sure which is better/preferable: Conversion operator in iterator class iterator { operator const_iterator(); }; or non-explicit constructor in const_iterator class iterator { // implementation friend class iterator; // hard to avoid this }; class const_iterator { const_iterator(iterator const &); }; Are there any guidelines which way is better? 回答1: You should write a