instantiation

Create an object in the constructor or at top of the class

痴心易碎 提交于 2019-12-19 19:12:33
问题 which declaration/instantiation is better and WHY ? public class MainWindow { private Test _test; public MainWindow() { _test = new Test(); } } OR public class MainWindow { private Test _test = new Test(); public MainWindow() { } } 回答1: Ask yourself this question: what happens when you add other constructors to MainWindow ? Do you want to then have to remember to invoke other constructors to ensure that _test is correctly initialized? Or is it okay for _test to not be initialized if another

What am I doing wrong? Python object instantiation keeping data from previous instantiation?

情到浓时终转凉″ 提交于 2019-12-19 07:38:25
问题 Can someone point out to me what I'm doing wrong or where my understanding is wrong? To me, it seems like the code below which instantiates two objects should have separate data for each instantiation. class Node: def __init__(self, data = []): self.data = data def main(): a = Node() a.data.append('a-data') #only append data to the a instance b = Node() #shouldn't this be empty? #a data is as expected print('number of items in a:', len(a.data)) for item in a.data: print(item) #b data includes

Restrict the number of object instantiations from a class to a given number

百般思念 提交于 2019-12-19 04:13:02
问题 Given a class, I would like to limit the number of objects created from this class to a given number, say 4. Is there a method to achieve this? 回答1: The basic idea is to count the number of created instances in some static variable. I would implement it like this. Simpler approaches exist, but this one has some advantages. template<class T, int maxInstances> class Counter { protected: Counter() { if( ++noInstances() > maxInstances ) { throw logic_error( "Cannot create another instance" ); } }

template instantiation check for member existing in class

耗尽温柔 提交于 2019-12-19 04:02:07
问题 I have a group of classes that have one or more members of the type memberA, memberB, memberC. Not all classes have all the members. I want to create a template that will set the members such as template <typename T> void setAttributes(t & myClass, typeA memA, typeB memB, typeC memC) { myClass.memberA = memA; myClass.memberB = memb; myClass.memberC = memC; } Obviously this will fail at compile time when attempting to instantiate a class that is missing one of the members. Is there a #if or

Instantiate JavaScript functions with custom prototypes

北战南征 提交于 2019-12-19 03:11:34
问题 I use the following function to create instances of functions in JavaScript from an array of arguments: var instantiate = function (instantiate) { return function (constructor, args, prototype) { "use strict"; if (prototype) { var proto = constructor.prototype; constructor.prototype = prototype; } var instance = instantiate(constructor, args); if (proto) constructor.prototype = proto; return instance; }; }(Function.prototype.apply.bind(function () { var args = Array.prototype.slice.call

PHP: Fatal error: Cannot instantiate abstract class

大城市里の小女人 提交于 2019-12-18 15:51:46
问题 I have an abstract database class named as: abstract class database { protected $value; } I created another abstract class abstract class my_database extends database { public function set_value($value) { $this->value = $value; } } When I try to use it: $my_db = new my_database(); I get error: Fatal error: Cannot instantiate abstract class my_database in ... What I try to do is: The abstract class database has a protected $value and I would like to create a wrapper class, to be able to change

PHP: Fatal error: Cannot instantiate abstract class

无人久伴 提交于 2019-12-18 15:51:04
问题 I have an abstract database class named as: abstract class database { protected $value; } I created another abstract class abstract class my_database extends database { public function set_value($value) { $this->value = $value; } } When I try to use it: $my_db = new my_database(); I get error: Fatal error: Cannot instantiate abstract class my_database in ... What I try to do is: The abstract class database has a protected $value and I would like to create a wrapper class, to be able to change

Declaring an object before initializing it in c++

佐手、 提交于 2019-12-18 10:59:28
问题 Is it possible to declare a variable in c++ without instantiating it? I want to do something like this: Animal a; if( happyDay() ) a( "puppies" ); //constructor call else a( "toads" ); Basially, I just want to declare a outside of the conditional so it gets the right scope. Is there any way to do this without using pointers and allocating a on the heap? Maybe something clever with references? 回答1: You can't do this directly in C++ since the object is constructed when you define it with the

How to create inline objects with properties in Python?

試著忘記壹切 提交于 2019-12-18 10:58:23
问题 In Javascript it would be: var newObject = { 'propertyName' : 'propertyValue' }; How to do it in Python? 回答1: obj = type('obj', (object,), {'propertyName' : 'propertyValue'}) there are two kinds of type function uses. 回答2: Peter's answer obj = lambda: None obj.propertyName = 'propertyValue' 回答3: Python 3.3 added the SimpleNamespace class for that exact purpose: >>> from types import SimpleNamespace >>> obj = SimpleNamespace(propertyName='propertyValue') >>> obj namespace(propertyName=

Instantiating Rhinoscript Native Objects from Java/Scala

一笑奈何 提交于 2019-12-18 09:24:46
问题 I'm trying to improve the performance of a javascript snippet evaluator . These script snippets can reference any number of variables that exist in a string-keyed map of json-like object graphs (IE: Json AST). I'm using JDK 1.6 and the embedded Rhinoscript engine (v1.6R2). Currently, processing takes the form: Snippet is parsed to discover the names of referenced variables Variables are retrieved from the map and serialized to a json string Json string is assigned to a similarly named