constructor

Calling PHP Parent Constructors With Old/New Syntax

荒凉一梦 提交于 2019-12-18 19:09:10
问题 Given the class Foo with an old-style constructor class Foo { public function Foo() { //does constructing stuff } } Is there any functional difference between calling the parent constructor with a new style constructor or the old style constructor? class Bar extends Foo { public function Bar() { //does it matter? //parent::__construct(); //parent::Foo(); } } Put another way, is there anything special about the static call parent::__construct() when it's made from a constructor, or is it just

How do I pass an array to a constructor?

感情迁移 提交于 2019-12-18 18:56:22
问题 I want to pass an array to a constructor, but only the first value is passed--the rest looks like garbage. Here's a simplified version of what I'm working on: #include <iostream> class board { public: int state[64]; board(int arr[]) { *state = *arr; } void print(); }; void board::print() { for (int y=0; y<8; y++) { for (int x=0; x<8; x++) std::cout << state[x + y*8] << " "; std::cout << "\n"; } } int main() { int test[64] = { 0, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, 2, 3, 4, 5, 6, 7, 8

C++ compile error constructing object with rvalue std::string

情到浓时终转凉″ 提交于 2019-12-18 18:54:29
问题 I'm faced with a compile error that I don't even know how to describe! It completely baffles me. The situation : Code tries to create an object on the stack with an rvalue std::string that is initialized with a char*. The code : #include <iostream> #include <string> class Foo { public: Foo(double d) : mD(d) { } Foo(const std::string& str) { try { mD = std::stod(str); } catch (...) { throw; } } Foo(const Foo& other) : mD(other.mD) { } virtual ~Foo() {} protected: double mD; }; class Bar {

JvmOverloads annotation for class primary constructor

老子叫甜甜 提交于 2019-12-18 18:40:10
问题 Why is it prohibited to autogenerate many constructors visible to Java from class primary constructor with default params likes this? @JvmOverloads class Video(private val id: Long, val ownerId: Long, var title: String? = null, var imgLink: String? = null, var videoLink: String? = null, var description: String? = null, var created: Date? = null, var accessKey: String? = null, var duration: Long? = null, var views: Long? = null, var comments: Long? = null) : Entity This annotation is not

How to set @Autowired constructor params as “required=false” individually

倖福魔咒の 提交于 2019-12-18 18:35:09
问题 I'm using the @Autowired annotation under a @Configuration class constructor. @Configuration public class MyConfiguration { private MyServiceA myServiceA; private MyServiceB myServiceB @Autowired public MyConfiguration(MyServiceA myServiceA, MyServiceB myServiceB){ this.myServiceA = myServiceA; this.myServiceB = myServiceB; } } As the Spring documentation sais, I'm able to declare whether the annotated dependency is required. If I mark the @Autowired annotation under the constructor as

Why can I not call my class's constructor from an instance of that class in C++?

本小妞迷上赌 提交于 2019-12-18 18:28:51
问题 When can an object of a class call the destructor of that class, as if it's a regular function? Why can't it call the constructor of the same class, as one of its regular functions? Why does the compiler stops us from doing this? For example: class c { public: void add() ; c(); ~c() ; }; void main() { c objC ; objC.add() ; objC.~c() ; // this line compiles objC.c() ; // compilation error } 回答1: By definition, a constructor is only called once, when the object is created. If you have access to

PHP MVC: Too many dependencies in controller?

泄露秘密 提交于 2019-12-18 18:23:20
问题 I'm working on a personal HMVC project: No service locators, no global state (like static or global ), no singletons. The model handling is encapsulated in services (service = domain objects + repositories + data mappers). All controllers extend an abstract controller. All project dependencies are injected through Auryn dependency injection container. All needed dependencies are injected in the constructor of the abstract controller. If I want to override this constructor, then I have to pass

Calling a constructor without creating object

半城伤御伤魂 提交于 2019-12-18 15:19:18
问题 What exactly is happening in the code below. #include<iostream.h> class Demo { public : Demo() { cout<<"\nIn Demo const"; } ~Demo() { cout<<"\nin demo dest"; } }; void main() { Demo(); } Demo() simply calls the constructor and destructor. Is a object being created in this process? And thus is the memory being allocated? 回答1: You're not explicitly calling the constructor, instead this code creates a temporary unnamed object with type Demo , which is destroyed immediately after ; . Yes, memory

Value initialization: default initialization or zero initialization?

天大地大妈咪最大 提交于 2019-12-18 14:14:46
问题 I have templated gray_code class which is meant to store some unsigned integer whose underlying bits are stored in Gray code order. Here it is: template<typename UnsignedInt> struct gray_code { static_assert(std::is_unsigned<UnsignedInt>::value, "gray code only supports built-in unsigned integers"); // Variable containing the gray code UnsignedInt value; // Default constructor constexpr gray_code() = default; // Construction from UnsignedInt constexpr explicit gray_code(UnsignedInt value):

Initialize() vs Constructor() method, proper usage on object creation

落爺英雄遲暮 提交于 2019-12-18 14:12:48
问题 We all know the difference between a Constructor and a User-Defined Initialize() method fundamentally. My question is focused on best design practice for object creation. We can put all Initialize() code into Constructor() and vice versa (move all warm-up code to Initialize method and call this method from Constructor ). Currently, designing a new class, I create any new instances inside constructor() and move any other warm-up code into Initialize() method. What's the best trade-off point in