constructor

Manual invocation of constructor?

空扰寡人 提交于 2019-12-11 04:16:28
问题 Suppose I am allocating an arbitrary block of memory. Part of this block is atomic data (ints, bytes, etc.) and some of this block of data I want to be occupied by objects. Can I turn any arbitrary piece of memory into an object through a constructor call, such as data->MyObject () and subsequently destroying the object via data->~MyObject() , or is this impossible? 回答1: What you are looking for is called placement new. 来源: https://stackoverflow.com/questions/1229433/manual-invocation-of

iTextSharp OcspClientBouncyCastle constructor is deprecated, what's the replacement?

天大地大妈咪最大 提交于 2019-12-11 04:13:10
问题 I'm using iTextSharp 5.5.10. OcspClientBouncyCastle default's constructor is deprecated. IOcspClient ocspClient = new OcspClientBouncyCastle(); The other one is : OcspClientBouncyCastle(OcspVerifier verifier) But i cant't find any way to use it. Could anybody provide a sample with this new constructor, please ? Thank you very much. 回答1: If you want the former behavior, i.e. the OCSP response retrieved by the OcspClientBouncyCastle is trusted without further ado, you can simply use null as

why is base only possible in private members?

匆匆过客 提交于 2019-12-11 04:08:44
问题 I have some understanding of the difference between private members and let bindings. It may help me clarify my doubts understanding why something like this is not possible type B () = inherit A () let doSomething () = base.CallToA () Is it to prevent partially constructed objects or some leaks during construction? 回答1: The base keyword is only really needed to call a base-class implementation of a virtual method. That is the only case where you need base because you cannot invoke the method

New instance has old instance attribute values [duplicate]

一笑奈何 提交于 2019-12-11 03:47:48
问题 This question already has answers here : “Least Astonishment” and the Mutable Default Argument (31 answers) Closed 4 years ago . I want to instantiate multiple instances of books. In my __init__() method I specify some default values. So I am expecting that every time I instantiate a new book, all the old values will be replaced by the default ones I specified as arguments for __init__() . On the contrary, I get this: class Book(object): def __init__(self, title=None, book_rows=[]): self

When and how are static data initialized in C++?

别等时光非礼了梦想. 提交于 2019-12-11 03:37:55
问题 When are static data initialized ? I think: It can be initialized by constructor, or when it is declared, or outside of the class by class A::member_d = 5; // member_d is static others ? Also, When do file scope static variables initialized and when do function scope static variables initizliaed? I think they are initialized when they are declared. thanks 回答1: Static members of a class are initialized at the point of definition. Const integral data types are an exception that can be

C++11 Magically Deleted Constructor in BST

◇◆丶佛笑我妖孽 提交于 2019-12-11 03:32:57
问题 SOLVED ! See below So, I'm trying to learn C++11 by doing some simple data structures and playing around with them. I did something similar to the following BST example using raw pointers and new and delete and it worked fine. Then I wanted to do it in a way that was more leak-safe. // tree.cpp // // #include <iostream> #include <memory> /* DECLARATIONS */ template <typename T> struct Tree { // members T data; std::unique_ptr<Tree<T> > left; std::unique_ptr<Tree<T> > right; // methods Tree (T

Best practice to creating Objects in java

久未见 提交于 2019-12-11 03:32:56
问题 I'm reading the Effective Java book by Joshua Bloch. In the first chapter, he says to use factories instead of constructors and lists the advantages and disadvantages of this approach. As far as I see disadvantages are not closely related to to object construction. Moreover, Bloch says that Java Bean style of construction has disadvantages as well. Source: http://www.javapractices.com/topic/TopicAction.do?Id=84 OK, so using constructors is not great, Java beans are not great, factories are

Generate HashCode in constructor of immutable type

ぐ巨炮叔叔 提交于 2019-12-11 03:29:49
问题 i have some questions about the HashCode of immutable types. May i "pre"-generate the HashCode of an immutable type in the constructor or are there any reason not do that? Should i always generate the Hashcode again, when the method GetHashCode() is called? Here´s a sample class : public class Id { private readonly object _value; private readonly int _hash = -1; public Id( object value ) { _value = value; _hash = ( int ) ( 7 * value.GetType().GetHashCode() + 7 + 7 * _value.GetHashCode() ); }

Why C# don't accept constructor requirements with parameters on generics?

你离开我真会死。 提交于 2019-12-11 03:19:19
问题 Working with C# Generics you can have a class like this: class Foo<T> where T:new() {} Which means that the type T should have a constructor without parameters. It would be nice if we could have: class Foo<T> where T : new(string) { private T CreateItem() { string s=""; return new T(s); } } Is there any reason that Microsoft haven't added this feature to the language? 回答1: Is there any reason that Microsoft haven't added this feature to the language? The feature you describe is a specific

What does 'this' mean in a c# constructor? [duplicate]

吃可爱长大的小学妹 提交于 2019-12-11 03:11:24
问题 This question already has answers here : what is 'this' constructor, what is it for (4 answers) Closed 4 years ago . I am very new to c# and I am working through a textbook. The textbook shows this piece of code: public class BankAccount { // Bank accounts start at 1000 and increase sequentially. public static int _nextAccountNumber = 1000; // Maintain the account number and balance for each object. public int _accountNumber; public decimal _balance; // Constructors public BankAccount() :