constructor

explicit non-single parameter constructor

心已入冬 提交于 2020-01-03 09:04:02
问题 Can anyone explain why does non-single parameter constructor marked as explicit compile? As far as I understand this is absolutely useless keyword here, so why does this compile without error? class X { public: explicit X(int a, int b) { /* ... */} }; 回答1: In C++03, and in this particular case, it makes no sense for a two parameter constructor to be marked explicit . But it could make sense here: explicit X(int i, int j=42); So, marking a two parameter constructor with explicit does not have

explicit non-single parameter constructor

巧了我就是萌 提交于 2020-01-03 09:03:46
问题 Can anyone explain why does non-single parameter constructor marked as explicit compile? As far as I understand this is absolutely useless keyword here, so why does this compile without error? class X { public: explicit X(int a, int b) { /* ... */} }; 回答1: In C++03, and in this particular case, it makes no sense for a two parameter constructor to be marked explicit . But it could make sense here: explicit X(int i, int j=42); So, marking a two parameter constructor with explicit does not have

In which case the C++ this pointer can be NULL

﹥>﹥吖頭↗ 提交于 2020-01-03 08:32:12
问题 I experienced an issue that in the class constructor while I was using this pointer, but this pointer happen to be NULL at that time. for example MyClass::MyClass() { //this pointer happen to be NULL in this case, and it crash in teh m_callbackfunc because it does not handle null input. m_callbackFunc(this); } I wonder why this pointer can be null? and in which case this pointer can be null? 回答1: The only way a this pointer can be NULL is if some code in the program has exhibited undefined

In which case the C++ this pointer can be NULL

旧巷老猫 提交于 2020-01-03 08:32:00
问题 I experienced an issue that in the class constructor while I was using this pointer, but this pointer happen to be NULL at that time. for example MyClass::MyClass() { //this pointer happen to be NULL in this case, and it crash in teh m_callbackfunc because it does not handle null input. m_callbackFunc(this); } I wonder why this pointer can be null? and in which case this pointer can be null? 回答1: The only way a this pointer can be NULL is if some code in the program has exhibited undefined

Initializing Typescript class values from constructor

二次信任 提交于 2020-01-03 07:28:14
问题 I'm using TypeScript to create some classes with KnockoutJS, with the data being loaded from some JSON returned by WebAPI. The problem is I wanted to copy the JSON values into my TypeScript class from the constructor: but if I do this just at the base class, the inherited properties have not been defined and so are not initialised. Example We want to create an inventory item from a JSON response: { Name: "Test", Quantity:1, Price: 100 } I have a base class Product and an inherited class

Mocking objects without no-argument constructor in C# / .NET

微笑、不失礼 提交于 2020-01-03 06:44:11
问题 Is it possible to create a mock from a class that doesn't provide a no-argument constructor and don't pass any arguments to the constructor? Maybe with creating IL dynamically? The background is that I don't want to define interfaces only for testing. The workaround would be to provide a no-argument constructor for testing. 回答1: Sure thing. In this example i'll use Moq, a really awesome mocking library. Example: public class MyObject { public MyObject(object A, object B, object C) { // Assign

Fortran OOP circular dependency handling, interfaces

纵然是瞬间 提交于 2020-01-03 03:33:05
问题 Compiler: ifort version 12.1.5 I'm writing some Fortran code and I'd like to make use of some F2003 OOP features, but I'm hitting some stumbling blocks. Paring down the example, I wish to have two derived types A and B, each of which have a pointer to instances of the other. In Fortran, circular dependencies between modules are explicitly disallowed, so these two types would have to reside in the same module. This compiles: module testModule implicit none type A type(B),pointer :: b1 end type

I need C++ array class template, which is fixed-size, stack-based and doesn't require default constructor

不打扰是莪最后的温柔 提交于 2020-01-03 02:31:09
问题 So, I've been looking at boost::array but it does require default constructor defined. I think the best way of filling this array with data, would be through a push_back(const T&) method. Calling it more times than SIZE (known at compile-time) would result in assert or exception, depending on build configuration. This way it would always contain meaningful data. Does anyone know efficient, portable, reliable implementation of this concept? 回答1: Well, I would have thought that someone would

JavaScript constructor parameter types

痞子三分冷 提交于 2020-01-02 21:55:13
问题 I have a JavaScript class representing a car, which is constructed using two parameters, which represent the make and model of the car: function Car(make, model) { this.getMake = function( ) { return make; } this.getModel = function( ) { return model; } } Is there a way to verify that the make and model supplied to the constructor are strings? For example, I want the user to be able to say, myCar = new Car("Honda", "Civic"); But I don't want the user to be able to say, myCar = new Car(4, 5.5)

what does DefaultEdge.class mean in jgrapht example

只愿长相守 提交于 2020-01-02 20:18:07
问题 What does dot class mean in the parameter passed to Constructor I am using jgrapht for the first time . I have this question What are we passing to the constructor of the DefaultDirectedGraph class? I mean what does DefaultEdge.class means ? There is no static field with that name inside that class. I mean what is actual being passed to the contructor of that class. What does the dot class mean there ? DirectedGraph<String, DefaultEdge> g = new DefaultDirectedGraph<String, DefaultEdge>