reference

Does binding temporary to a reference require a copy constructor in C++?

寵の児 提交于 2020-01-03 08:38:29
问题 Consider the following code: class A { A(const A&); public: A() {} }; int main() { const A &a = A(); } This code compiles fine with GCC 4.7.2, but fails to compile with Visual C++ 2010 with the following error: test.cc(8) : error C2248: 'A::A' : cannot access private member declared in class 'A' test.cc(2) : see declaration of 'A::A' test.cc(1) : see declaration of 'A' So is it necessary to have a copy constructor accessible when binding a temporary to a reference? This is somewhat related to

return reference of an object from an iterator

你说的曾经没有我的故事 提交于 2020-01-03 06:49:29
问题 I want to return a reference of an object from a vector, and the object is in an iterator object. How can I do that? I tried the following: Customer& CustomerDB::getCustomerById (const string& id) { vector<Customer>::iterator i; for (i = customerList.begin(); i != customerList.end() && !(i->getId() == id); ++i); if (i != customerList.end()) return *i; // is this correct? else return 0; // getting error here, cant return 0 as reference they say } In the code, customerList is a vector of

Reassignment, mutation, reference types, and value types

主宰稳场 提交于 2020-01-03 03:09:26
问题 How would you appropriately explain why these two examples differ? // Reassignment let a = 1; let b = a; a = 2; console.log(b); // → 1 // Mutation let myArray = [1, 2, 3]; let ourArray = myArray; ourArray[2] = 10; console.log(myArray); // → [1, 2, 10] In regards to why the variable label behaves differently between the two, many resources claim that it's because of the difference between reference types and value types. But, doesn't that only apply to the differences between the values

How to get T* from T, T& or T* template parameter

霸气de小男生 提交于 2020-01-02 20:06:53
问题 I am trying to find a way to get the base class of a template parameter. Consider the following class: template <class C> class Foo { public: Foo(){}; ~Foo(){}; C* ptr; }; if C is a reference (e.g. Test& ) then ptr is type C&* But I need to get a pointer the base class whether C is a reference, a pointer or anything else. if C is Test& then ptr need to be Test* , if C is Test* then ptr needs to be Test* , if C is Test then ptr needs to be Test* , etc. Is there anyway to get the "base" class

Xtext cross referencing and scoping

╄→尐↘猪︶ㄣ 提交于 2020-01-02 19:13:10
问题 I have some problems with xtext cross referencing Here is a very simple grammer: grammar org.xtext.example.mydsl1.Test with org.eclipse.xtext.common.Terminals generate test "http://www.xtext.org/example/mydsl1/Test" Model: block=Block? cs+=Company* ; Block: '{' g=[Employee] '}'; Company: 'Company' name=ID '{' es+= Employee* '}'; Employee: 'Employee' name=ID ';' ; and it is my dsl : { Pooyan } Company Sony{ Employee Pooyan; Employee John; } It always shown that "Couldn't resolve reference to

Xtext cross referencing and scoping

廉价感情. 提交于 2020-01-02 19:13:06
问题 I have some problems with xtext cross referencing Here is a very simple grammer: grammar org.xtext.example.mydsl1.Test with org.eclipse.xtext.common.Terminals generate test "http://www.xtext.org/example/mydsl1/Test" Model: block=Block? cs+=Company* ; Block: '{' g=[Employee] '}'; Company: 'Company' name=ID '{' es+= Employee* '}'; Employee: 'Employee' name=ID ';' ; and it is my dsl : { Pooyan } Company Sony{ Employee Pooyan; Employee John; } It always shown that "Couldn't resolve reference to

C++ object referencing in classes

耗尽温柔 提交于 2020-01-02 17:13:09
问题 I am wondering how to store a reference of an object inside of another object, and also set that reference as a private property. Example (pseudo-code): class foo { public: int size; foo( int ); }; foo::foo( int s ) : size( s ) {} class bar { public: bar( foo& ); private: foo fooreference; }; bar::bar( foo & reference ) { fooreference = reference; } foo firstclass( 1 ); bar secondclass( firstclass ); As you may be able to see, I just want to be able to store the reference of foo inside this

Class loses “this” scope when calling prototype functions by reference

会有一股神秘感。 提交于 2020-01-02 14:30:29
问题 Can anyone explain to me why "b" returns undefined and how I can get around this problem? Why does the "this" scope get lost when I call prototype functions by reference? MyClass = function(test) { this.test = test; } MyClass.prototype.myfunc = function() { return this.test; } var a = new MyClass('asd').myfunc(); var b = new MyClass('asd').myfunc; // Returns "asd" correctly console.log(a) // Returns undefined?? console.log(b()) === EDIT / SOLUTION === As plalx writes, the correct solution in

Lua - Count the no. of references to a table

橙三吉。 提交于 2020-01-02 10:22:31
问题 The Lua docs say When a program has no references to a table left, Lua memory management will eventually delete the table and reuse its memory. My question is : Is it possible to count the no of references to a particular table during runtime? 回答1: You can find all references to a Lua value using the debug library. See these messages: http://lua-users.org/lists/lua-l/2012-03/msg00479.html http://lua-users.org/lists/lua-l/2012-04/msg00758.html The luatraverse library found in the below link

Lua - Count the no. of references to a table

烂漫一生 提交于 2020-01-02 10:21:29
问题 The Lua docs say When a program has no references to a table left, Lua memory management will eventually delete the table and reuse its memory. My question is : Is it possible to count the no of references to a particular table during runtime? 回答1: You can find all references to a Lua value using the debug library. See these messages: http://lua-users.org/lists/lua-l/2012-03/msg00479.html http://lua-users.org/lists/lua-l/2012-04/msg00758.html The luatraverse library found in the below link