construction

C++ temporary variable lifetime

别来无恙 提交于 2019-12-01 17:19:50
问题 Is this code valid? int foo() { std::vector<std::string>& v = std::vector<std::string>(5, "X"); // Do something silly... return 42; } For some reason I thought that the temporary std::vector object (right from the assignment sign) should be destructed right after it's construction (thus rendering the reference invalid) . However, debugging proves that I'm wrong and, well, I realized that I don't quite understand why does the temporary variable is destructed when the function returns. I guess

Is Object constructor called when creating an array in Java?

流过昼夜 提交于 2019-12-01 15:16:31
问题 In Java, an array IS AN Object. My question is... is an Object constructor called when new arrays is being created? We would like to use this fact to instrument Object constructor with some extra bytecode which checks length of array being constructed. Would that work? 回答1: As far as the Java Language Specification is concerned, although both use the new keyword, Class Instance Creation Expressions and Array Creation Expressions are different forms of expression, each with its own rules. The

Construction of temporary in function call is interpreted as declaration

你。 提交于 2019-11-29 13:36:19
Lately I ran into a problem which somehow (but only somehow) makes sense to me. It is based on interpreting the construction of a temporary as declaration of the single (!) constructor argument. Please have a look at the minimal example below. #include <iostream> class Foo0{ public: Foo0(int a){}; void doStuff() {std::cout<<"maap"<<std::endl;}; }; class Foo1{ public: Foo1(int a){}; void doStuff() {std::cout<<"maap"<<std::endl;}; }; class Foo2{ public: Foo2(int a){}; void doStuff() {std::cout<<"maap"<<std::endl;}; }; class Bar{ public: Bar(Foo0 foo0, Foo1 foo1, Foo2 foo2){}; }; int main () {

What is an int() Called?

…衆ロ難τιáo~ 提交于 2019-11-28 10:54:26
It's been rehashed over and over that primitive types don't have constructors. For example this _bar is not initialized to 0 when I call Foo() : class Foo{ int _bar; }; So obviously int() is not a constructor. But what is it's name? In this example I would say i is: (constructed? initialized? fooed?) for(int i{}; i < 13; ++i) Loki Astari mentions here that the technique has some sort of name. EDIT in response to Mike Seymour : #include <iostream> using namespace std; class Foo{ int _bar; public: void printBar(){ cout << _bar << endl; } }; int main() { Foo foo; foo.printBar(); Foo().printBar();

Pass a string Recursively without Recreation

瘦欲@ 提交于 2019-11-28 02:27:20
I answered a question here: https://stackoverflow.com/a/28862668/2642059 Where I needed to use recurrence to step through a string . I wanted to use a const string& as my parameter on each function, but unless I wanted to reconstruct the string each recursion I found that I needed to pass a start and finish position as well as the string itself. So it became pointless to pass the string at all. In the end I choose to just pass a start and finish pointer to the char[] . As an example, say that I'm given a string which contains nested parenthesis (but no side by side parenthetical insertions.)

What is an int() Called?

╄→гoц情女王★ 提交于 2019-11-27 03:51:45
问题 It's been rehashed over and over that primitive types don't have constructors. For example this _bar is not initialized to 0 when I call Foo() : class Foo{ int _bar; }; So obviously int() is not a constructor. But what is it's name? In this example I would say i is: (constructed? initialized? fooed?) for(int i{}; i < 13; ++i) Loki Astari mentions here that the technique has some sort of name. EDIT in response to Mike Seymour: #include <iostream> using namespace std; class Foo{ int _bar;

Thread safe lazy construction of a singleton in C++

﹥>﹥吖頭↗ 提交于 2019-11-27 02:52:58
Is there a way to implement a singleton object in C++ that is: Lazily constructed in a thread safe manner (two threads might simultaneously be the first user of the singleton - it should still only be constructed once). Doesn't rely on static variables being constructed beforehand (so the singleton object is itself safe to use during the construction of static variables). (I don't know my C++ well enough, but is it the case that integral and constant static variables are initialized before any code is executed (ie, even before static constructors are executed - their values may already be

Pass a string Recursively without Recreation

老子叫甜甜 提交于 2019-11-26 23:45:41
问题 I answered a question here: https://stackoverflow.com/a/28862668/2642059 Where I needed to use recurrence to step through a string . I wanted to use a const string& as my parameter on each function, but unless I wanted to reconstruct the string each recursion I found that I needed to pass a start and finish position as well as the string itself. So it became pointless to pass the string at all. In the end I choose to just pass a start and finish pointer to the char[] . As an example, say that

How can building a heap be O(n) time complexity?

丶灬走出姿态 提交于 2019-11-25 22:47:47
问题 Can someone help explain how can building a heap be O(n) complexity? Inserting an item into a heap is O(log n) , and the insert is repeated n/2 times (the remainder are leaves, and can\'t violate the heap property). So, this means the complexity should be O(n log n) , I would think. In other words, for each item we \"heapify\", it has the potential to have to filter down once for each level for the heap so far (which is log n levels). What am I missing? 回答1: I think there are several