constructor

Why can't I change objects in a vector?

偶尔善良 提交于 2019-12-22 10:53:53
问题 I have a class TileGrid that holds an std::vector< std::vector<Tile> > . Accessing the Tile objects in the vector works, but I can't change their properties? For the sake of completion, here are all the relevant classes: tilegrid.h #include <vector> #include "tile.h" class TileGrid { public: TileGrid(); TileGrid(unsigned int rows, unsigned int cols); virtual ~TileGrid(); unsigned int getRows() const { return rows_; }; unsigned int getCols() const { return cols_; }; Tile atIndex(unsigned int

How do I use [] as a default value for a named function argument in python? [duplicate]

淺唱寂寞╮ 提交于 2019-12-22 10:50:20
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I have this code class Test(object): def __init__(self, var1=[]): self._var1 = var1 t1 = Test() t2 = Test() t1._var1.append([1]) print t2._var1 and I get "[[1]]" as the result. So clearly t1._var1 and t2._var1 are addressing the same list. If I put t3 = Test() print t3._var1 then I get "[[1]]" as well. So var1=[] seems to permanently bind var1 to the

RequireJS: How to define a constructor?

大兔子大兔子 提交于 2019-12-22 10:48:20
问题 I want to create constructors according to the AMD specification. I found this answer and tried to follow it. Here's what I ended up with: main.js requirejs.config({ paths: { 'jquery': 'vendor/jquery-1.9.1.min', 'lodash': 'vendor/lodash-1.3.1.min', 'knockout': 'vendor/knockout-2.2.1.min', 'bootstrap': 'vendor/bootstrap-2.3.2.min' } }); requirejs( ['jquery', 'lodash', 'knockout', 'controller/categories'], function main($,_,ko, CategoriesCtrl) { var categories = new CategoriesCtrl(); } );

Constructor chaining with intermediate variables

那年仲夏 提交于 2019-12-22 10:46:22
问题 I have the following situtation with overloaded constructors which I'm struggling to find a nice solution to. I can't see how to use an intermediate assignment with constructor chaining. The following isn't valid but shows what I want to do public MyThing(IServiceLocator services, int? userId) { // blah.... } public MyThing(IServiceLocator services, string userName) { User user = services.UserService.GetUserByName(userName); int userId = user == null ? null : (int?)user.Id; // call the other

Virtual Function During Construction Workaround

喜欢而已 提交于 2019-12-22 09:25:07
问题 I've got a base class that has a virtual function. I want to call that class during the construction because I want the function called for each of the derived classes. I know I can't call a virtual function during construction, but I can't think of an elegant (i.e., avoid repeating code) solution. What are some work arounds to calling a virtual function during construction? The reason I want to avoid this is because I don't want to have to create constructors that just call the base class.

Why are the 'context' and 'object' functions in Rebol different, but essentially the same?

五迷三道 提交于 2019-12-22 09:09:45
问题 On the one hand we have: >> source object object: make function! [[ "Defines a unique object." blk [block!] "Object words and values." ][ make object! append blk none ]] For context we see: >> source context context: make function! [[ "Defines a unique object." blk [block!] "Object words and values." ][ make object! blk ]] So, for object the object is constructed from a block to which none has been appended. This doesn't change the length, or, to my knowledge, add anything. With context , on

Copy constructor not calling

Deadly 提交于 2019-12-22 08:59:09
问题 When I read about copy initializing vs direct initializing here. copy constructor should call in copy initializing. why here copy constructor not calling? #include <iostream> using namespace std; class A{}; class B{ public: B(const A &a){cout << "B construct from A" << endl;} B(const B &b){cout << "B copy constructor" << endl;} }; int main(){ A a; B b = a; return 0; } 回答1: This is Copy Elision Ref 1: . Copy constructor calls while generating temporaries might be optimized by the compiler by

Does an explicit move ctor eliminate implicit copy ctor?

只谈情不闲聊 提交于 2019-12-22 08:54:53
问题 I read in the accepted answer here that: [a] copy constructor and copy assignment operator won't be generated for a class that explicitly declares a move constructor or move assignment operator I do notice (g++ 4.7.2) that if you define a move constructor, it will be used with, e.g., push_back() , whereas if all you do is = delete the copy constructor, you don't get an implicit move constructor -- you get an error. [...which leads me to wonder which one (move or copy) is actually used if you

JPQL/Hibernate limitation using constructor expression in SELECT

怎甘沉沦 提交于 2019-12-22 08:46:25
问题 From my reading of the JPA 2.0 spec, the following should be valid: select e.employeeId, new com.foo.Custom(e.employeeName, e.employeeCity) from Employee e However, Hibernate complains about this query, citing the first comma. If I reverse the order of the selected expressions, it still complains: select new com.foo.Custom(e.employeeName, e.employeeCity), e.employeeId from Employee e But if I select only the constructor expression, it works: select new com.foo.Custom(e.employeeName, e

“When” does the compiler implicitly declare a default constructor?

房东的猫 提交于 2019-12-22 08:42:14
问题 I know that compiler will generate a default constructor if we don't declare it. And "when" is the point I got confused. A: class Base {}; int main() { return 0; } B: class Base {}; int main() { Base b; // Declare a Base object. return 0; } The A and B difference is only that B declares a real object of Base. At my point, only when we declare a real object and the compiler finds no constructors does it generate a default constructor. My question is that: Will code fragment A generate a