class

What's the most idotomatic way of declaring two constructors with the same args?

て烟熏妆下的殇ゞ 提交于 2020-06-16 16:59:13
问题 As an example lets say I have a class called File . Now file can be opened as binary or text. My constructor is currently File(const char*filename) . Let's suppose the implementation of open is completely different binary and text. How the heck do I construct this? I thought about using a static function but I don't want to return a pointer. I could pass in a pointer but I rather not allow a class be constructed without actually initializing it. I was thinking about having an enum or bool in

JavaScript - Class extend on condition

柔情痞子 提交于 2020-06-16 05:29:32
问题 Here is the thing. I have a main class called A. I want this class to extend class B. class A extends B {} But in fact, I want the class B to extend C, D or E on a specific condition: class B extends B1 {} or class B extends B2 {} or class B extends B3 {} So the B class would be a "fake" class, just to check a condition and then extend the right class. In the final, the result would be the same as: class A extends B1 {} or class A extends B2 {} or class A extends B3 {} I know this is possible

Virtual overloaded operators >> and <<

♀尐吖头ヾ 提交于 2020-06-15 21:22:26
问题 I need an interface that would require its subclasses to overload << and >> , but I'm not quite sure how since these operators aren't overloaded as member functions: std::istream& operator>> (std::istream& in, Student& student) { in >> student.name >> student.group; for (int& i : student.marks) { in >> i; } return in; } Maybe there's a way to make it a member function? 回答1: You could do something like this: class StudentInterface { public: virtual void readSelfFrom(std::istream& in) = 0; };

Python classes: method has same name as property

风格不统一 提交于 2020-06-14 08:31:19
问题 I'm constructing a class Heating. Every instance of this class has the property 'temperature'. It's mandatory that Heating also supports the method temperature() that prints the property 'temperature' as an integer. When I call the method temperature() I get the error 'int' object is not callable because self.temperature is already defined as an integer. How do I solve this? code: class Heating: """ >>> machine1 = Heating('radiator kitchen', temperature=20) >>> machine2 = Heating('radiator

How to use the same Scanner across multiple classes in Java

混江龙づ霸主 提交于 2020-06-14 04:18:34
问题 I have a program that uses multiple classes, I want the other classes to be able to access the same scanner that I have declared in the main class, I assume it would be done using some sort of get method, however I am unable to find any resources to help me. Here are the Scanners that I have made in my main class: Scanner in = new Scanner(System.in); System.out.println("Enter a filename"); String filename = in.nextLine(); File InputFile = new File (filename); Scanner reader = new Scanner

How to use the same Scanner across multiple classes in Java

南楼画角 提交于 2020-06-14 04:13:06
问题 I have a program that uses multiple classes, I want the other classes to be able to access the same scanner that I have declared in the main class, I assume it would be done using some sort of get method, however I am unable to find any resources to help me. Here are the Scanners that I have made in my main class: Scanner in = new Scanner(System.in); System.out.println("Enter a filename"); String filename = in.nextLine(); File InputFile = new File (filename); Scanner reader = new Scanner

`UseMethod()` vs `inherits()` to determine an object's class in R

一个人想着一个人 提交于 2020-06-13 01:59:18
问题 If I need to treat R objects in different ways according to their class, I can either use if and else within a single function: foo <- function (x) { if (inherits(x, 'list')) { # Foo the list } else if (inherits(x, 'numeric')) { # Foo the numeric } else { # Throw an error } } Or I can define a method: foo <- function (x) UseMethod('foo') foo.list <- function (x) { # Foo the list } foo.numeric <- function (x) { # Foo the numeric } What are the advantages to each approach? Are there performance

`UseMethod()` vs `inherits()` to determine an object's class in R

时光怂恿深爱的人放手 提交于 2020-06-13 01:58:36
问题 If I need to treat R objects in different ways according to their class, I can either use if and else within a single function: foo <- function (x) { if (inherits(x, 'list')) { # Foo the list } else if (inherits(x, 'numeric')) { # Foo the numeric } else { # Throw an error } } Or I can define a method: foo <- function (x) UseMethod('foo') foo.list <- function (x) { # Foo the list } foo.numeric <- function (x) { # Foo the numeric } What are the advantages to each approach? Are there performance

Why can't classes be used as modules?

倖福魔咒の 提交于 2020-06-12 06:06:43
问题 Module is the superclass of Class : Class.superclass # => Module In OOP, this implies that an instance of Class can be used in every place where an instance of Module can be used. Surprisingly, this is not the case with Class instances in Ruby: class C end c = C.new module M end # Let's do all the extend/include/prepend stuff with M! c.extend M C.include M C.prepend M # All worked fine until this line. # Let's turn to classes now! # First, get a class to work with. class C_as_M end C_as_M

PHP Fatal error: Constant expression contains invalid operations

北战南征 提交于 2020-06-12 05:35:06
问题 here is the fatal error: Fatal error: Constant expression contains invalid operations I get a fatal error in this code: <?php class InfoClass { private $user_agent = $_SERVER['HTTP_USER_AGENT']; // error is on this line public static function getOS() { global $user_agent; $os_platform = "Unknown OS Platform"; ... } i am using php 7. why is this error showing? thanks 回答1: Do This Instead <?php class InfoClass { private $user_agent; public function __construct(){ $this->user_agent = $_SERVER[