constructor

How can I initialize a const variable of a base class in a derived class' constructor in C++?

夙愿已清 提交于 2019-12-18 03:57:10
问题 I have an abstract C++ class with no constructor. It's supposed to be a base class so other classes can inherit from it. What I am trying to do is to declare a constant variable in the base class and initialize it in each derived class' constructor but nowhere else in each one of those classes. Is it legal in C++? If so, how can I do that? 回答1: Is it legal in C++? No. The constant must be initialized in the base class constructor. The solution is to provide an appropriate constructor in your

Java constructor inheritance?

南楼画角 提交于 2019-12-18 03:40:31
问题 I always thought that constructors aren't inherited, but look at this code: class Parent { Parent() { System.out.println("S1"); } } class Child extends Parent { Child() { System.out.println("S2"); } } public class Test5 { public static void main(String[] args) { Child child = new Child(); } } //RESULT: //S1 //S2 It shows that Child inherited constructor. Why there is S1 on result? Is there any possibility to create 2 constructors without parameters and have only Child constructor on result

Why might one also use a blank constructor?

有些话、适合烂在心里 提交于 2019-12-18 03:39:28
问题 I was reading some Java recently and came across something (an idiom?) new to me: in the program, classes with multiple constructors would also always include a blank constructor. For example: public class Genotype { private boolean bits[]; private int rating; private int length; private Random random; public Genotype() { // <= THIS is the bandit, this one right here random = new Random(); } /* creates a Random genetoype */ public Genotype(int length, Random r) { random = r; this.length =

Can we make a class copy constructor virtual in C++

落花浮王杯 提交于 2019-12-18 03:03:08
问题 Can we make a class copy constructor virtual in C++? How to use? 回答1: No you can't, constructors can't be virtual. C++03 - 12.1 Constructors 4) A constructor shall not be virtual (10.3) or static (9.4). [...] If you need something like this, you can look up the virtual constructor idiom here. 回答2: No you cannot. Furthermore, the whole concept does not make sense. Virtual functions are functions that are dispatched based on the value of an object (the dynamic type of the object). When a

What is the preferred way of constructing objects in C#? Constructor parameters or properties?

百般思念 提交于 2019-12-18 03:02:12
问题 I was wondering, what is the preferred way to construct a new object in C#? Take a Person class: public class Person { private string name; private int age; //Omitted.. } Should I create it to use: New Person("name", 24); or New Person() { Name = "name", Age = 24 }; Is it just a matter of taste or is there a good reason to use one over the other? I can imagine that one should only use the required fields in the constructor and optional fields not as constructor parameters but by using

Multiple constructors in python, using inheritance

戏子无情 提交于 2019-12-18 02:45:54
问题 I have a class AbstractDataHandle, whith his init method, and a class Classifier. I would like to have two constructors in Classifier, Java like. One inherited from it`s superclass, and one brand new. It would be something like (but i intend to "keep" the two constructors): class AbstractDataHandle(): def __init__(self, elements, attributes, labels): self._load(elements, attributes, labels) class Classifier(AbstractDataHandle): def __init__(self, classifier="LinearSVC", proba=False): self.

Python: creating class instance without calling initializer

孤街醉人 提交于 2019-12-17 23:37:31
问题 Is there any way to avoid calling __init__ on a class while initializing it, such as from a class method? I am trying to create a case and punctuation insensitive string class in Python used for efficient comparison purposes but am having trouble creating a new instance without calling __init__ . >>> class String: def __init__(self, string): self.__string = tuple(string.split()) self.__simple = tuple(self.__simple()) def __simple(self): letter = lambda s: ''.join(filter(lambda s: 'a' <= s <=

Constructor inheritance and direct member initialisation [duplicate]

拈花ヽ惹草 提交于 2019-12-17 22:58:31
问题 This question already has an answer here : Inheriting constructors and brace-or-equal initializers (1 answer) Closed 2 years ago . I am trying to use a combination of the C++ 11 direct data member initialisation and the "using" syntax to inherit the constructors of a base class. Now with gcc 5.4.0 (on Ubuntu 16.04) I observe a strange error, if the data member type has no default constructor. It is probably easiest to understand when looking on the following minimal example: #include

What difference is there in JavaScript between a constructor function, and function returning object which is invoked as a constructor?

谁说胖子不能爱 提交于 2019-12-17 22:43:08
问题 I know this is not the recommended way of doing it, but if I declare the following functions, and then invoke them as constructors, what will be the difference (if any) between the resulting objects? function Something() { this.foo = "bar"; } function something2() { var that = {}; that.foo = "bar"; return that; } var x = new Something(); var y = new something2(); var z = something2(); I.e. what will differ between x , y and z here? Wouldn't something2 be a much better way of writing the

Pass arguments from array in php to constructor [duplicate]

为君一笑 提交于 2019-12-17 22:38:34
问题 This question already has answers here : How to “invoke” a class instance in PHP? (3 answers) Closed 6 years ago . Normally, if I want to pass arguments from $myarray to $somefunction I can do this in php using call_user_func_array($somefunction, $myarray); However this does not work when the function one wishes to call is the constructor for an object. For fairly obvious reasons it does not work to do: $myobj = new call_user_func_array($classname, $myarray); is there something fairly elegant