constructor

Groovy mistakenly using constructor of enclosing class?

若如初见. 提交于 2019-12-12 17:50:06
问题 Given: static class Question { // stuff List<Value> values static class Value { // stuff } When I run: Question question = new Question() question.id = "whatever" if (it == QuestionType.SELECT || it == QuestionType.MULTICHOICE) { question.values = [new Question.Value(), new Question.Value()] for (Question.Value v : question.values) { // stuff } question.values contains an array of Question objects and NOT Value. Intellij doesn't give me any error nor warnings. I tried evaluating "new Question

strange behaviour of std::vector::resize() with gcc 4.7.0

前提是你 提交于 2019-12-12 17:30:23
问题 I'm still confused about the behaviour of std::vector::resize() . Consider the following code (see also type requirements for std::vector<type>) struct A { A() : X(0) { std::cerr<<" A::A(); this="<<this<<'\n'; } A(A const&) { assert(0); } // is required but doesn't fire in vector::resize int X; }; int main() { std::vector<A> a; a.resize(4); // would not compile without A::A(A const&) or A::A(A&&) } Without A::A(A const&) or A::A(A&&) , the line with a.resize(4); doesn't compile. However, that

Different ways of using C++ constructors

≯℡__Kan透↙ 提交于 2019-12-12 16:23:02
问题 What the difference between A a1(5); and A a2 = A(5) ? Both of the works, but I really want know the difference between them, because I used method 2 in one of my project and I suffered from a bug which is fixed after I change to method 1. Thanks in advance! class A { public: int val; A() : val(0) {} A(int newVal) : val(newVal) {} }; int main() { A a1(5); // method 1 A a2 = A(5); // method 2 } 回答1: A a1(5); // method 1 A a2 = A(5); // method 2 The first one is called direct initialization,

C# Is there any benefit to assigning class properties in the class constructor?

天大地大妈咪最大 提交于 2019-12-12 16:12:52
问题 For instance, if I have a class like this: namespace Sample { public Class TestObject { private Object MyAwesomeObject = new MyAwesomeObject(); } } Is there any benefit to set it up so that the property is set in the constructor like this? namespace Sample { public Class TestObject { private Object MyAwesomeObject; public TestObject() { MyAwesomeObject = new MyAwesomeObject() } } } 回答1: The two are (nearly) identical. When you define the initializer inline: private Object MyAwesomeObject =

Spring.NET & Constructor Interceptors

两盒软妹~` 提交于 2019-12-12 16:04:20
问题 I'm trying to do some AOP over objects at construction time, and found IConstructorInterceptor, which would be perfect for what I want but it doesn't appear to work (in version 1.2 at least). I've also looked at both the IObjectPostProcessor & the IInstantiationAwareObjectPostProcessor, but I can't find any way to do processing on an object around construction time... The PostProcessPropertyValues method on the IInstantiationAwareObjectPostProcessor is close, but it only passes through setter

Java constructor design

爷,独闯天下 提交于 2019-12-12 15:17:39
问题 I was reading an open-source code, and there was a constructor designed like this: public class FeatureSequence2FeatureVector extends Pipe implements Serializable { boolean binary; public FeatureSequence2FeatureVector (boolean binary) { this.binary = binary; } public FeatureSequence2FeatureVector () { this (false); } } This may be just a trivial preference matter, but what I would do is like this: public class FeatureSequence2FeatureVector extends Pipe implements Serializable { boolean binary

Presence of constructor in an applet throws exception

拥有回忆 提交于 2019-12-12 14:52:03
问题 I'm running the below applet . In it, the moment I add the constructor ( even empty ), the applet throws a runtime exception: MainFrame.class can't be instantiated, java.lang.InstantiationException If I remove the constructor, no exception in thrown. Can't I have a constructor present in an applet? public class MainFrame extends JApplet implements WindowListener, ActionListener { public void init() { System.out.println("Applet Step1"); String[] args = null; createAndShowGUI(args); } private

Why Delphi XE3 gives “E2382 Cannot call constructors using instance variables”?

喜你入骨 提交于 2019-12-12 14:31:35
问题 I have a simple piece of code, that compiles in Delphi XE2 but not in XE3, and I don't know why. I have reduced the problematic code to a small bit and would like to know what's wrong with it in Delphi's opinion. Trying to compile a project containing this unit in Delphi XE 2 works fine, but in Delphi XE3 (trial), it gives "[dcc32 Error] AffineTransform.pas(26): E2382 Cannot call constructors using instance variables". The only "eccentric" thing I know of here is the use of the old-school

c++ undefined reference to `vtable

别来无恙 提交于 2019-12-12 14:20:50
问题 My question has changed from the other one I have posted. I started out with multiple files and decided to put it all in one main.cpp file for now just to get it working. main.cpp: #include <iostream> using namespace std; class arrayListType { public: bool isEmpty() ; bool isFull() ; int listSize() ; int maxListSize() ; void print() ; bool isItemAtEqual(int location, int item) ; virtual void insertAt(int location, int insertItem) = 0; virtual void insertEnd(int insertItem) = 0; void removeAt

use parameterized constructor in other classes constructor

爱⌒轻易说出口 提交于 2019-12-12 14:07:35
问题 I fear that this is a very basic question, however, I was not able to solve it yet. I have a class A // classA.h ... class ClassA { public: ClassA(); ClassA(int foo); private: int _foo; ... } // classA.cpp ClassA::ClassA() { _foo = 0; } ClassA::ClassA(int foo) { _foo = foo; } ... A second class B uses an instance of class A in the constructor: // classB.h ... #include "classA.h" #define bar 5 class ClassB { public: ClassB(); private: ClassA _objectA; ... } // classB.cpp ClassB::ClassB() {