constructor

“Constructor call must be the first statement in a constructor” issue in Java [duplicate]

淺唱寂寞╮ 提交于 2019-12-30 18:27:28
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why does this() and super() have to be the first statement in a constructor? I'd like to have constructor chain in Java. For example, with the first constructor I have a string as a parameter, and call the second constructor as I create an object from the parameter string. public class IMethodFinder { public IMethodFinder(String projectName, String methodName, int numberOfParameters) { IProject project =

Inherited Constructors in C++

让人想犯罪 __ 提交于 2019-12-30 11:09:30
问题 I'm trying to practice Inheriting Constructors in C++. I have compiled and run following program in gcc and it's working fine. #include<iostream> using namespace std; class Base1 { public: Base1() { cout<<"Base1 Default Constructor\n"; } Base1(int i) { cout<<"Base1 parametrized Constructor\n"; } }; class Base2 { public: Base2() { cout<<"Base2 Default Constructor\n"; } Base2(const string& s) { cout<<"Base2 parametrized Constructor\n"; } }; class Derived :public Base1, public Base2 { public:

What is the difference between instantiation in constructor or in field definition?

白昼怎懂夜的黑 提交于 2019-12-30 10:35:34
问题 What is the difference between this: public class Foo { private Bar bar; public Foo() { bar = new Bar(); } } and this: public class Foo { private Bar bar = new Bar(); public Foo() { } } 回答1: The difference is that in the second case field initialization happens before this/base constructor while in the first case initialization happens inside the constructor. 回答2: The CLR does not support initializing fields like this. To make it work, the C# compiler rewrites your constructor. The IL looks

Flaw: Constructor does Real Work

假装没事ソ 提交于 2019-12-30 10:29:08
问题 I have a class which represents a set of numbers. The constructor takes three arguments: startValue , endValue and stepSize . The class is responsible for holding a list containing all values between start and end value taking the stepSize into consideration. Example: startValue: 3, endValue: 1, stepSize = -1, Collection = { 3,2,1 } I am currently creating the collection and some info strings about the object in the constructor. The public members are read only info strings and the collection

Is it possible to defer member initialization to the constructor body?

﹥>﹥吖頭↗ 提交于 2019-12-30 10:17:24
问题 I have a class with an object as a member which doesn't have a default constructor. I'd like to initialize this member in the constructor, but it seems that in C++ I can't do that. Here is the class: #include <boost/asio.hpp> #include <boost/array.hpp> using boost::asio::ip::udp; template<class T> class udp_sock { public: udp_sock(std::string host, unsigned short port); private: boost::asio::io_service _io_service; udp::socket _sock; boost::array<T,256> _buf; }; template<class T> udp_sock<T>:

Initialize array in constructor without using default constructor or assignment

别说谁变了你拦得住时间么 提交于 2019-12-30 10:09:30
问题 Consider: struct A { A (int); A (const A &); }; struct B { A foo [2]; B (const A & x, const A & y) : foo {x, y} /* HERE IS THE PROBLEM */ {} }; I was expecting this to work since I'm using C++0x support in GCC4.3, which allegedly supports initialiser lists. No joy. I have a class A which has no default constructor. This is not negotiable. Assignment post-default is not an option. I am trying to create B which uses A. B::foo may not be std::vector. How can I initialise B::foo in B(...) ,

Initialize array in constructor without using default constructor or assignment

我的梦境 提交于 2019-12-30 10:09:29
问题 Consider: struct A { A (int); A (const A &); }; struct B { A foo [2]; B (const A & x, const A & y) : foo {x, y} /* HERE IS THE PROBLEM */ {} }; I was expecting this to work since I'm using C++0x support in GCC4.3, which allegedly supports initialiser lists. No joy. I have a class A which has no default constructor. This is not negotiable. Assignment post-default is not an option. I am trying to create B which uses A. B::foo may not be std::vector. How can I initialise B::foo in B(...) ,

why the c++ constructor was not called when it appear as the static member variable?

余生颓废 提交于 2019-12-30 09:41:11
问题 I had a strange problem , declare a static member variable whose name is B class in A class . And initialize in the cpp file. but the constructor of B class was never called. I try to use some small test , the test constructor could be called normally. so it is very strange for our production system. The code like this , in hpp : class Test { public: Test() { ofstream file("/tmp/wup.txt",ios::app); file << "wup in test" << endl; file.close(); } }; //## An extended personality class

C++: Constructor versus initializer list in struct/class

送分小仙女□ 提交于 2019-12-30 08:15:27
问题 An object of a struct/class (that has no constructor ) can be created using an initializer list . Why is this not allowed on struct/class with constructor ? struct r { int a; }; struct s { int a; s() : a(0) {} }; r = { 1 }; // works s = { 1 }; // does not work 回答1: No, an object with a constructor is no longer considered a POD (plain old data). Objects must only contain other POD types as non-static members (including basic types). A POD can have static functions and static complex data

Invoking virtual function and pure-virtual function from a constructor

你离开我真会死。 提交于 2019-12-30 04:56:05
问题 When i invoke a virtual function from a base constructor, the compiler does not give any error. But when i invoke a pure-virtual function from the base class constructor, it gives compilation error. Consider the sample program below: #include <iostream> using namespace std; class base { public: void virtual virtualfunc() = 0; //void virtual virtualfunc(); base() { virtualfunc(); } }; void base::virtualfunc() { cout << " pvf in base class\n"; } class derived : public base { public: void