Tag: 初始化

如何初始化一个属于类成员的shared_ptr?

我不确定初始化是类的成员的shared_ptr的好方法。 你能告诉我,在C::foo()select的方式是好还是有更好的解决scheme? class A { public: A(); }; class B { public: B(A* pa); }; class C { boost::shared_ptr<A> mA; boost::shared_ptr<B> mB; void foo(); }; void C::foo() { A* pa = new A; mA = boost::shared_ptr<A>(pa); B* pB = new B(pa); mB = boost::shared_ptr<B>(pb); }

在子类中覆盖init

在Objective-C中,是否有必要重写一个子类的所有inheritance构造函数来添加自定义的初始化逻辑? 例如,对于具有自定义初始化逻辑的UIView子类,以下是否正确? @implementation CustomUIView – (id)init { self = [super init]; if (self) { [self initHelper]; } return self; } – (id)initWithFrame:(CGRect)theFrame { self = [super initWithFrame:theFrame]; if (self) { [self initHelper]; } return self; } – (id)initWithCoder:(NSCoder *)decoder { self = [super initWithCoder:decoder]; if (self) { [self initHelper]; } return self; } – (void) […]

Python类成员初始化

我刚刚在Python中遇到了一个bug。 这是那些愚蠢的新手bug之一,但它让我想到了Python的机制(我是一个很长时间的C ++程序员,是Python的新手)。 我将列出错误的代码,并解释我做了什么来解决它,然后我有几个问题… 场景:我有一个名为A的类,它有一个字典数据成员,下面是它的代码(这当然是简化): class A: dict1={} def add_stuff_to_1(self, k, v): self.dict1[k]=v def print_stuff(self): print(self.dict1) 使用此代码的类是B类: class B: def do_something_with_a1(self): a_instance = A() a_instance.print_stuff() a_instance.add_stuff_to_1('a', 1) a_instance.add_stuff_to_1('b', 2) a_instance.print_stuff() def do_something_with_a2(self): a_instance = A() a_instance.print_stuff() a_instance.add_stuff_to_1('c', 1) a_instance.add_stuff_to_1('d', 2) a_instance.print_stuff() def do_something_with_a3(self): a_instance = A() a_instance.print_stuff() a_instance.add_stuff_to_1('e', 1) a_instance.add_stuff_to_1('f', 2) a_instance.print_stuff() def __init__(self): self.do_something_with_a1() […]

在C#中对类实例数组的优雅初始化

假设我有这样的一个class级: public class Fraction { int numerator; int denominator; public Fraction(int n, int d) { // set the member variables } // And then a bunch of other methods } 我想以一个很好的方式初始化它们的一个数组,这个post是一个容易出错或者在语法上很麻烦的大方法列表。 当然,一个数组构造函数会很好,但是没有这样的事情: public Fraction[](params int[] numbers) 所以我不得不使用类似的方法 public static Fraction[] CreateArray(params int[] numbers) { // Make an array and pull pairs of numbers for constructor […]

有没有办法通过哈希来初始化一个对象?

如果我有这个class级: class A attr_accessor :b,:c,:d end 和这个代码: a = A.new h = {"b"=>10,"c"=>20,"d"=>30} 是否有可能直接从哈希值初始化对象,而不需要通过每对来调用instance_variable_set ? 就像是: a = A.new(h) 这应该导致每个实例variables被初始化为散列中具有相同名称的variables。

在C#中多次初始化'for'循环

我怎么能(如果有可能的话)在C# for循环中初始化多个不同types的variables? 例: for (MyClass i = 0, int j = 1; j<3; j++,i++)

为什么使用初始化方法而不是构造函数?

我刚进入一个新的公司,许多代码库使用初始化方法而不是构造函数。 struct MyFancyClass : theUberClass { MyFancyClass(); ~MyFancyClass(); resultType initMyFancyClass(fancyArgument arg1, classyArgument arg2, redundantArgument arg3=TODO); // several fancy methods… }; 他们告诉我,这与时间有关。 有些事情必须在施工之后完成, 在构造函数中失败。 但是大多数构造函数都是空的,我没有看到任何不使用构造函数的理由。 所以我转向你,哦向导的C + +:为什么你会用一个init方法,而不是一个构造函数?

在Java中,最终的字段可以从一个构造函数助手初始化?

我有一个最终的非静态成员: private final HashMap<String,String> myMap; 我想用构造函数调用的方法初始化它。 由于myMap是final的,我的“helper”方法无法直接初始化。 当然我有select: 我可以直接在构造函数中实现myMap初始化代码。 MyConstructor (String someThingNecessary) { myMap = new HashMap<String,String>(); myMap.put("blah","blahblah"); // etc… // other initialization stuff unrelated to myMap } 我可以让我的帮助器方法构buildHashMap,将其返回给构造函数,然后让构造函数将对象分配给myMap。 MyConstructor (String someThingNecessary) { myMap = InitializeMyMap(someThingNecessary); // other initialization stuff unrelated to myMap } private HashMap<String,String> InitializeMyMap(String someThingNecessary) { HashMap<String,String> initializedMap = new HashMap<String,String>(); initializedMap.put("blah","blahblah"); […]

Swift的子类化 – 如何重写Init()

我有以下类,用init方法: class user { var name:String var address:String init(nm: String, ad: String) { name = nm address = ad } } 我想这个类的子类,但我不断收到super.init()部分的错误: class registeredUser : user { var numberPriorVisits: Int // This is where things start to go wrong – as soon as I type 'init' it // wants to autocomplete it for me with […]

Java中静态块的必要性

我发现,在Java中,有一个称为static block的function,其中包括在首次加载类时执行的代码(我不明白“加载”是什么意思,是否意味着初始化? 是否有任何理由做一个静态块内的初始化位,而不是在构造函数? 我的意思是,即使构造函数也做同样的事情,当一个类首次被初始化时,做所有必要的事情。 有没有什么静态块完成哪个构造函数不能?