reference

Should my std::vector contain pointers or structs?

旧街凉风 提交于 2019-12-21 04:21:15
问题 I know that holding pointers incurs the overhead of an extra dereference operation but it saves me including the (potentially large) header file that contains the definition of my struct. However my preference is to be determined by the advantage of having a std::vector<myStruct> *ptr2Vect member. Namely, not having to call delete on each element. How big a performance advantage is this? Can vector really allocate objects on the stack? I am fairly new to template classes and wonder if it

Can “soft references” exist in Python?

孤人 提交于 2019-12-21 04:12:19
问题 In other languages (e.g. Java), object references can be Strong, Weak, Soft or Phantom (http://weblogs.java.net/blog/enicholas/archive/2006/05/understanding_w.html). In Python, references are Strong by default and the WeakRef module allows weak references. Is it possible to have "soft references" in Python? In my particular case, I have a cache of objects that are time-consuming to create. Sometimes there may be no references to a cached object, but I don't want to throw the cached object

Learning C++: returning references AND getting around slicing

醉酒当歌 提交于 2019-12-21 04:04:08
问题 I'm having a devil of a time understanding references. Consider the following code: class Animal { public: virtual void makeSound() {cout << "rawr" << endl;} }; class Dog : public Animal { public: virtual void makeSound() {cout << "bark" << endl;} }; Animal* pFunc() { return new Dog(); } Animal& rFunc() { return *(new Dog()); } Animal vFunc() { return Dog(); } int main() { Animal* p = pFunc(); p->makeSound(); Animal& r1 = rFunc(); r1.makeSound(); Animal r2 = rFunc(); r2.makeSound(); Animal v

Temporary lifetime extension

≡放荡痞女 提交于 2019-12-21 03:55:06
问题 The 12.2.5 section of standard says: A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full expression containing the call. A temporary bound to the returned value in a function return statement (6.6.3) persists until the function exits. In all these cases, the temporaries created during the evaluation of the expression initializing the reference, except the temporary to which the reference is bound, are destroyed at the end of the full

In TypeScript, when to use reference, when to use import?

有些话、适合烂在心里 提交于 2019-12-21 03:52:57
问题 in TypeScript there are two concepts of referencing files/modules. Even though I went briefly through TypeScript documentation it is unclear to me, when which approach should be used when: Triple-Slash reference: /// <reference path="..." /> Import: import { Foo } from "./Foo"; Thanks 回答1: It is important to understand that these are not two concepts to reference files/modules. Their actually two completely different things. import This keyword was introduces in ES2015 and thus is part of its

Return reference from class to this

怎甘沉沦 提交于 2019-12-21 03:41:01
问题 I have the following member of class foo. foo &foo::bar() { return this; } But I am getting compiler errors. What stupid thing am I doing wrong? Compiler error (gcc): error: invalid initialization of non-const reference of type 'foo&' from a temporary of type 'foo* const' 回答1: this is a pointer. So it should be return *this; 回答2: As Naveen points out, you need to return *this . Just a quick tip though: a way to figure out what somewhat obscure compiler errors mean is to try compiling on a

How should I compare Perl references?

早过忘川 提交于 2019-12-21 03:12:13
问题 I want to check if two references point to the same object. It seems I can simply use if ($ref1 == $ref2) { # cheap numeric compare of references print "refs 1 and 2 refer to the same thing\n"; } as mentioned in perlref, but I vaguely remember seeing the use of some function for the same purpose. Is there any reason I shouldn't use the simple numerical equality test? Note I only want to know whether the references point to the exact same object. I don't look for a way to compare the content

Why was the ampersand chosen as the symbol for references in C++? [closed]

耗尽温柔 提交于 2019-12-21 02:31:11
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago . Does anyone have an idea why the ampersand was chosen as the way to denote references in C++? AFAIK (though I don't have the book near me), Stroustroup didn't explain that choice, which I find a little odd because the same symbol was already used for address-of in C. 回答1:

Why was the ampersand chosen as the symbol for references in C++? [closed]

落爺英雄遲暮 提交于 2019-12-21 02:31:07
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago . Does anyone have an idea why the ampersand was chosen as the way to denote references in C++? AFAIK (though I don't have the book near me), Stroustroup didn't explain that choice, which I find a little odd because the same symbol was already used for address-of in C. 回答1:

Reference variable in class definition

筅森魡賤 提交于 2019-12-20 21:47:23
问题 I am learning C++, and I read that all references must be initialized upon declaration, and there can be no "uninitialized references". But what if the reference variable is a class member? class test { int &k; }; int main() { test *abc = new test; } This program compiles and runs normally (in g++, no warnings). However, abc->k is a reference, but what is it initialized to? Or, is it an "uninitialized reference" of some sort, or something else? 回答1: The program is ill-formed because it