constructor

Define a scope for javascript Function constructor

99封情书 提交于 2019-12-24 06:34:08
问题 I'm trying to create some dynamic functions that require an enclosing scope. Currently this works: eval("with (scope) (function () {return scope.prop})") This approach seems a bit hacky. User supplied code is nowhere near this so I'm not worried about security, it's just that it seems like there should be a better way. I know the Function constructor does not include anything apart from the global scope, but I was hoping there was some way to 'inject' scope into the Function constructor. Not

C++ Compiler Error C2751 - What exactly causes it?

久未见 提交于 2019-12-24 06:29:51
问题 I am wrestling with the C2751 compiler error and don't quite understand what exactly causes it. The following little code produces the error: #include <iostream> class A { public: A () { std::cout << "A constructed" << std::endl; }; static A giveA () { return A (); } }; class B { public: B (const A& a) { std::cout << "B constructed" << std::endl; } }; int main () { B b1 = B (A::giveA ()); // works B b2 (B (A::giveA ())); // C2751 B b3 (A::giveA ()); // works } Compiler output:

Limit the number of DIRECT Instances of a class

心不动则不痛 提交于 2019-12-24 04:59:09
问题 To put in other words: How can a class track whether its constructor is called due to instantiating its child class or its instance is directly created? [Please cosider the following sample code]: class Parent { ............. ......... .............. } class Child1 extends Parent { ............. ......... .............. } class Child2 extends Parent { ............. ......... .............. } I want to limit number of direct instances of Parent class created by calling new Parent(...) , and

super() not letting me call the super constructor with parameters

吃可爱长大的小学妹 提交于 2019-12-24 04:45:11
问题 I want to call the super class' constructor that takes two arguments, so I call super(arguments), but the compiler says: "Constructor Person in class Person cannot be applied to given types; required: no arguments Found: Java.lang.String, int reason: actual and formal argument lists differ in length" I have no clue why this is happening. The call to super is the first line. The arguments match (but not to the compiler for some reason). Does anyone know what's going on? I'm using NetBeans, by

Virtual function invocation from constructor

这一生的挚爱 提交于 2019-12-24 04:40:54
问题 Maybe I am wrong, but this seems to be a very basic question. Suddenly my inheritance chain stopped working. Writing a small basic test application proved that it was me that was wrong (so I can't blame the compiler). I have a base class, with the default behavior in a virtual function. A child class derives from that and changes the behavior. #include <iostream> class Base { public: Base() { print(); } ~Base() {} protected: virtual void print() { std::cout << "base\n"; } }; class Child :

Python call constructor in a member function

梦想与她 提交于 2019-12-24 04:40:23
问题 Let's take for example this class, which is extending MySQLDB's connection object. class DBHandler(mysql.connections.Connection): def __init__(self,cursor=None): if cursor == None: cursor = 'DictCursor' super(DBHandler,self).__init__(host = db_host, user = db_user, passwd = db_pass, db = db, cursorclass=getattr(mysql.cursors, cursor)) def getall(self,q,params=None): try: cur = self.cursor() cur.execute(q,params) res = cur.fetchall() return res except mysql.OperationalError: #this is the line

How to copy elements from an ArrayList to another one NOT by reference?

谁都会走 提交于 2019-12-24 04:23:25
问题 I'm trying to copy each element from one ArrayList (av) to another one (copia). The thing is that they're copied by reference, so whenever I make any changes in the original one, the copy is modified as well. Of course, this behavior is not wanted. How should I write this method? public void copiarArrayList(ArrayList<Articulo_Venta> copia, ArrayList<Articulo_Venta> av){ copia.clear(); for (int i = 0; i < av.size(); i++) { copia.add(av.get(i)); } } Articulo_Venta has these fields: int codigo;

Calling of Constructors in a Java

≡放荡痞女 提交于 2019-12-24 03:44:11
问题 In the book Java: The complete reference // Demonstrate when constructors are called. // Create a super class. class A { A() { System.out.println("Inside A's constructor."); } } // Create a subclass by extending class A. class B extends A { B() { System.out.println("Inside B's constructor."); } } // Create another subclass by extending B. class C extends B { C() { System.out.println("Inside C's constructor."); } } class CallingCons { public static void main(String args[]) { C c = new C(); } }

Static resource constructor with parameters

喜夏-厌秋 提交于 2019-12-24 03:17:51
问题 I have to create in my XAML file a static resource. <Window.Resources> <vm:ViewModel x:Key="viewModel" /> </Window.Resources> I need this static resource to get the items for my combobox ItemsSource="{Binding Source={StaticResource viewModel}, Path=GetItems, Mode=TwoWay}" But how can I give the ViewModel (constructor) a instance of my code behind class? 回答1: If I understand this correctly, you are violating the MVVM pattern. You should never provide items from the ComboBox into your VM. You

C++ conditional storage allocation for object caused by scope

我怕爱的太早我们不能终老 提交于 2019-12-24 03:05:41
问题 Currently I'm reading Think in C++ . I'm confused about the conditional storage allocation for an object . As the code below, the goto and switch generate warning or an error . But if-else works fine, which is conditionally passed through during execution. So why there is no warning or error for if-else ? /* crosses initializaion error */ #include <iostream> using namespace std; class X { public: X(); }; X::X() {} void f(int i) { if (i > 2) { X x1; } else { X x2; // why conditionally executed