Questions tagged [construction]

Use for questions related to building a data structure, such as a heap.

Construction is a concept of Object-Oriented languages.

Objects need to be setup for use:

  • Their member variables need to be allocated
  • Any Object member variables also need to be constructed
  • The Object methods need to be setup for calling

This process is accomplished by construction.

112 questions
850
votes
19 answers

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

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…
GBa
  • 17,509
  • 15
  • 49
  • 67
38
votes
9 answers

Thread safe lazy construction of a singleton in C++

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…
pauldoo
  • 18,087
  • 20
  • 94
  • 116
15
votes
3 answers

What is an int() Called?

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…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
12
votes
9 answers

Programmatic HTMLDocument generation using Java

Does anyone know how to generate an HTMLDocument object programmatically in Java without resorting to generating a String externally and then using HTMLEditorKit#read to parse it? Two reasons I ask: Firstly my HTML generation routine needs to be…
Tom Klapiscak
  • 121
  • 1
  • 1
  • 3
11
votes
2 answers

Referencing the same variable that you're declaring

I've seen the following type mistake a couple of times while working with C++ code: QString str = str.toUpper(); This can be a fairly easy mistake to make and yet it compiles and executes (sometimes with crashes, sometimes without). I can't see any…
Chris
  • 17,119
  • 5
  • 57
  • 60
10
votes
2 answers

Shouldn't the temporary A(3) be destroyed before "Here" is printed?

Shouldn't the temporary A(3) be destroyed before "Here" gets printed? #include struct A { int a; A() { std::cout << "A()" << std::endl; } A(int a) : a(a) { std::cout << "A(" << a << ")" << std::endl; } ~A() { std::cout <<…
9
votes
5 answers

Is Object constructor called when creating an array in Java?

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…
Peter Štibraný
  • 32,463
  • 16
  • 90
  • 116
9
votes
3 answers

jquery widget, _create or _init

Some jquery plugin extend widget use _create method, while others use _init method, can someone explain the differences between the two? Also any guidance on when it is better to extend widget or directly extend jquery.fn?
Gelin Luo
  • 14,035
  • 27
  • 86
  • 139
8
votes
1 answer

How to in-place-construct an optional aggregate?

How to in-place construct an optional aggregate? It seems I can only construct an optional single thing, and not an optional aggregate of things. #include #include struct Unmovable { Unmovable(const Unmovable&) = delete; …
user2394284
  • 5,520
  • 4
  • 32
  • 38
8
votes
1 answer

Is there a difference in how member variables are initialized in Dart?

In Dart, is there a difference in assigning values right away vs in constructor like in Java? class Example { int x = 3; } vs class Example { int x; Example() { x = 3; } } I ask because when I was using Flutter and tried to…
Amigo
  • 133
  • 3
  • 5
8
votes
5 answers

Abstract syntax tree construction and traversal

I am unclear on the structure of abstract syntax trees. To go "down (forward)" in the source of the program that the AST represents, do you go right on the very top node, or do you go down? For instance, would the example program a = 1 b = 2 c = 3 d…
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
8
votes
2 answers

Construction of temporary in function call is interpreted as declaration

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…
sedriel
  • 83
  • 4
7
votes
3 answers

Building a Binary Tree (not BST) in Haskell Breadth-First

I recently started using Haskell and it will probably be for a short while. Just being asked to use it to better understand functional programming for a class I am taking at Uni. Now I have a slight problem I am currently facing with what I am…
7
votes
4 answers

Understanding the efficiency of an std::string

I'm trying to learn a little bit more about c++ strings. consider const char* cstring = "hello"; std::string string(cstring); and std::string string("hello"); Am I correct in assuming that both store "hello" in the .data section of an application…
flumpb
  • 1,707
  • 3
  • 16
  • 33
7
votes
1 answer

thread_local member variable construction

I'm facing some strange behavior with thread_local and not sure whether I'm doing something wrong or it's a GCC bug. I have the following minimal repro scenario: #include using namespace std; struct bar { struct foo { foo ()…
1
2 3 4 5 6 7 8