constructor

What is the right way to allocate memory in the C++ constructor?

為{幸葍}努か 提交于 2019-12-22 05:11:31
问题 Which is the right way to allocate memory via new in the C++ constructor. First way in the argument list: class Boda { int *memory; public: Boda(int length) : memory(new int [length]) {} ~Boda() { delete [] memory; } }; or in the body of constructor: class Boda { int *memory; public: Boda(int length) { memory = new int [length]; } ~Boda() { delete [] memory; } }; Thanks, Boda Cydo. 回答1: I think the simplest way to do this would be to use a boost scoped array and let someone else's well tested

How to create routers in akka with parameterized actors?

时光怂恿深爱的人放手 提交于 2019-12-22 04:53:30
问题 I am trying to use a broadcast router in Scala, if I'm not mistaken it should look like this: val system = ActorSystem("My beautiful system") val workerRouter = system.actorOf(Props[Agent].withRouter(BroadcastRouter(individualDefinitions.size)), name = "agentRouter") That is what I understand from the tutorial I am following. The workerRouter acts as another actor and I can send messages to this router that will send them to all the Agents (as many as individualDefinitions I have). The

Why is the base() constructor not necessary?

亡梦爱人 提交于 2019-12-22 04:34:15
问题 I have a class structure like abstract class Animal { public Animal(){ //init stuff.. } } class Cat : Animal { public Cat(bool is_keyboard) : base() //NOTE here { //other init stuff } } Now then, look at the noted line. If you remove : base() then it will compile without an error. Why is this? Is there a way to disable this behavior? 回答1: There is an implicit : base() if you don't add anything else (any : base(...) or : this(...) ). To force it to be explicit, add a parameter to the base

Is instantiating a member of class Test within class Test a recursion?

坚强是说给别人听的谎言 提交于 2019-12-22 04:30:31
问题 Is this recursion? public class Test { Test test = new Test(); public static void main(String[] args) { new Test(); } } What about version with instance initalizer? public class Test { { Test test = new Test(); } public static void main(String[] args) { new Test(); } } I'm asking, because I updated my old answer, which was showing how to make StackOverflowError without recursion, but now I'm not 100% sure if codes above are recursion or not. 回答1: My opinion is that it is recursion. I am not

C++ Default constructor not inherited with “using” when move and copy constructors present

六眼飞鱼酱① 提交于 2019-12-22 04:28:10
问题 class A{ public: A(){}; }; class B : public A{ public: using A::A; B(const B&) = default; B( B&&) = default; }; B b; The compiler (g++ (5.4.0-6ubuntu1) / c++11) says "no matching function for call to B::B()" and lists the copy and move constructors as candidates. If I comment those defaulted ones out then it compiles. What causes this? And what difference does it make that they are explicitly defaulted? If those 2 lines weren't there they would be defaulted anyway. 回答1: Before C++17, the

What CursorAdapter have I to use?

ぃ、小莉子 提交于 2019-12-22 04:19:24
问题 CursorAdapter have 3 constructors. Let see the guide and reference. 1) CursorAdapter(Context context, Cursor c) This constructor is deprecated. This option is discouraged, as it results in Cursor queries being performed on the application's UI thread and thus can cause poor responsiveness or even Application Not Responding errors. As an alternative, use LoaderManager with a CursorLoader. 2) CursorAdapter(Context context, Cursor c, boolean autoRequery) Constructor that allows control over auto

Dilemma in calling constructor of generic class

我只是一个虾纸丫 提交于 2019-12-22 04:13:10
问题 I have this generic singleton that looks like this: public class Cache<T> { private Dictionary<Guid, T> cachedBlocks; // Constructors and stuff, to mention this is a singleton public T GetCache(Guid id) { if (!cachedBlocks.ContainsKey(id)) cachedBlocks.Add(id, LoadFromSharePoint(id)) return cachedBlocks[id]; } public T LoadFromSharePoint(Guid id) { return new T(id) // Here is the problem. } } The error message is: Cannot create an instance of type T because it does not have the new()

Dilemma in calling constructor of generic class

核能气质少年 提交于 2019-12-22 04:13:10
问题 I have this generic singleton that looks like this: public class Cache<T> { private Dictionary<Guid, T> cachedBlocks; // Constructors and stuff, to mention this is a singleton public T GetCache(Guid id) { if (!cachedBlocks.ContainsKey(id)) cachedBlocks.Add(id, LoadFromSharePoint(id)) return cachedBlocks[id]; } public T LoadFromSharePoint(Guid id) { return new T(id) // Here is the problem. } } The error message is: Cannot create an instance of type T because it does not have the new()

Copy constructor for abstract class

穿精又带淫゛_ 提交于 2019-12-22 03:51:20
问题 I have an abstract class named AClass . In the same package I have AnotherClass , in which I have an ArrayList of AClass objects. In the copy constructor of AnotherClass I need to make a duplicate of AClass objects inside the ArrayList . The problem: I cannot create a copy constructor in AClass because it is an abstract class and I cannot know the name of the class which will inherit by AClass . Actually, in this project, no object will inherit from this class, but this project will be used

Java Pattern class doesn't have a public constructor, why?

别说谁变了你拦得住时间么 提交于 2019-12-22 03:44:23
问题 I've been reviewing Java Regex Library, surprised by the fact the Pattern class does not have a public constructor which I've taken for granted for years. One reason I suspect the static compile method is being used in favor of constructor could be that constructor would always return a new object while a static method might return a previously created (and cached) object provided that the pattern string is the same. However, it is not the case as demonstrated by the following. public class