constructor

classmethod as constructor and inheritance

旧街凉风 提交于 2020-03-01 03:37:48
问题 The problem is quite simple. If a class B inherit a class A and wants to override a ´classmethod´ that is used as a constructor (I guess you call that a "factory method"). The problem is that B.classmethod will want to reuse A.classmethod , but then it will have to create an instance of the class A, while it subclasses the class A - since, as a classmethod, it has no self. And then, it doesn't seem the right way to design that. I did the example trivial, I do more complicate stuff by reading

c++11 constructor with variadic universal references and copy constructor

时光怂恿深爱的人放手 提交于 2020-03-01 02:33:07
问题 How to declare copy constructor, if we have constructor with universal reference arguments, also? http://coliru.stacked-crooked.com/a/4e0355d60297db57 struct Record{ template<class ...Refs> explicit Record(Refs&&... refs){ cout << "param ctr" << endl; } Record(const Record& other){ // never called cout << "copy ctr" << endl; } Record(Record&& other){ // never called cout << "move ctr" << endl; } }; int main() { Record rec("Hello"); Record rec2(rec); // do "param ctr" return 0; } According to

Java Generics method code duplication issue regarding primitive arrays

て烟熏妆下的殇ゞ 提交于 2020-02-29 04:37:09
问题 So I'm working on an implementation of a Grid class for a small, personal utilities library. After much work, I'm tidying up the Grid class and adding some functionality. One of the key pieces of functionality that I wish to use with my Grid class is to be able to take a single, 2D array of any given type as an argument to the constructor. This worked fine until I realized that I can't compile any code that attempts to pass in any array of primitives to the constructor. Since autoboxing doesn

Unclear most vexing parse [duplicate]

一曲冷凌霜 提交于 2020-02-25 10:14:22
问题 This question already has answers here : Why does printing a function name returns a value? (3 answers) Closed 7 months ago . Let's assume the following class Foo. struct Foo { int i; }; if I want to make an instance of this class and initialize it, I should do: Foo foo1 = Foo(); to call the constructor. int main(void) { foo1 = Foo(); cout << foo1.i << " : " << foo1.j << endl; // " 0 " } then I thought I'd call the default constructor with the following line but it doesn't: int main(void) {

Unclear most vexing parse [duplicate]

半城伤御伤魂 提交于 2020-02-25 10:14:09
问题 This question already has answers here : Why does printing a function name returns a value? (3 answers) Closed 7 months ago . Let's assume the following class Foo. struct Foo { int i; }; if I want to make an instance of this class and initialize it, I should do: Foo foo1 = Foo(); to call the constructor. int main(void) { foo1 = Foo(); cout << foo1.i << " : " << foo1.j << endl; // " 0 " } then I thought I'd call the default constructor with the following line but it doesn't: int main(void) {

Malloc on a struct containing a std::vector

时光毁灭记忆、已成空白 提交于 2020-02-14 12:11:34
问题 Here is the situation : I use a malloc to allocate memory for a struct. The struct contains various items such as pointers, string variables and vectors. The fact is, when we use malloc, no constructors are called. Using a code similar to the following one, I've experienced some situation where some variables worked while others didn't. Note : The following code doesn't compile. It's purpose is only to illustrate the situation. struct MyStruct { MyClass* mFirstClass; bool mBool; std::string

Child Class constructor using super() - getting unbound method __init__()

江枫思渺然 提交于 2020-02-04 13:27:20
问题 I'm trying to create classmethod constructors for a child class, but I cannot initialize the instance properly. I have read many blogs and answers on this site and even tried exactly what some other people have posted, still to no avail. Hopefully I'm missing something really simple. Basic example of what I'm trying: class A(object): def __init__(self, foo): self.foo = foo class B(A): @classmethod def make_new(cls): super(B, cls).__init__('bar') foobar = B.make_new() I keep getting unbound

Child Class constructor using super() - getting unbound method __init__()

*爱你&永不变心* 提交于 2020-02-04 13:26:45
问题 I'm trying to create classmethod constructors for a child class, but I cannot initialize the instance properly. I have read many blogs and answers on this site and even tried exactly what some other people have posted, still to no avail. Hopefully I'm missing something really simple. Basic example of what I'm trying: class A(object): def __init__(self, foo): self.foo = foo class B(A): @classmethod def make_new(cls): super(B, cls).__init__('bar') foobar = B.make_new() I keep getting unbound

By-Name-Parameters for Constructors

孤者浪人 提交于 2020-02-04 09:01:04
问题 coming from my other question is there a way to get by-name-parameters for constructors working? I need a way to provide a code-block which is executed on-demand/lazy/by-name inside an object and this code-block must be able to access the class-methods as if the code-block were part of the class. Following Testcase fails: package test class ByNameCons(code: => Unit) { def exec() = { println("pre-code") code println("post-code") } def meth() = println("method") def exec2(code2: => Unit) = {

By-Name-Parameters for Constructors

蹲街弑〆低调 提交于 2020-02-04 09:01:01
问题 coming from my other question is there a way to get by-name-parameters for constructors working? I need a way to provide a code-block which is executed on-demand/lazy/by-name inside an object and this code-block must be able to access the class-methods as if the code-block were part of the class. Following Testcase fails: package test class ByNameCons(code: => Unit) { def exec() = { println("pre-code") code println("post-code") } def meth() = println("method") def exec2(code2: => Unit) = {