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
6
votes
4 answers

Other way to prohibit a certain C++ class construction except than declaring the constructor private?

Say I have a class with some const reference member variable and I would like to forbid a certain type of construction. So I would declare the according constructor private. Of course, a constructor must initialise all const reference member…
ritter
  • 7,447
  • 7
  • 51
  • 84
6
votes
1 answer

Can a C++ constructor know whether it's constructing a const object?

In C++, object constructors cannot be const-qualified. But - can the constructor of an object of class A know whether it's constructing a const A or a non-const A? Motivated by a fine point in the discussion regarding this question.
einpoklum
  • 118,144
  • 57
  • 340
  • 684
6
votes
2 answers

How to support multiple construction signatures in a factory design?

I'm working with the following (simplified) factory design to create objects of some inheritance hierarchy, shouldn't be anything special: // class to create class Class { public: Class(Type type, Foo foo); }; // Simple creator class. // Used in…
user1709708
  • 1,557
  • 2
  • 14
  • 27
5
votes
2 answers

Weird "candidate expects 1 argument, 0 provided" in constructor

I'm making a simple threaded server application in C++, thing is, I use libconfig++ to parse my configuration files. Well, libconfig doesn't support multithreading, thus I'm using two wrapper classes in order to accomplish "support". Point is, one…
Misguided
  • 1,302
  • 1
  • 14
  • 22
5
votes
4 answers

Giant switch statement for constructors

I have a container which holds a bunch of pointers to a base class, and a function which takes some input and returns a class which is a subclass of the base class. Which subclass it returns depends on the input. Right now, I have a giant switch…
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
5
votes
2 answers

Are the following 3 ways to define objects identical?

In my understanding the following are identical: Person p{}; // Case 1 Person p = {}; // Case 1.5 I noticed Person p = Person{}; // Case 2 produces the same tracing output as the Case 1 and Case 1.5 above. Question 1: Comparing case 2 with either…
Second Person Shooter
  • 14,188
  • 21
  • 90
  • 165
5
votes
3 answers

Idiom to initialize C++ class to zero

Consider the following C++ class: struct Point { int x; int y; explicit Point() =default; // 1 explicit Point(int x_, int y_): x(x_), y(y_) { } // 2 }; The second constructor is useful for creating a Point with specific x and y…
Francis Xavier
  • 194
  • 1
  • 11
5
votes
3 answers

C++ temporary variable lifetime

Is this code valid? int foo() { std::vector& v = std::vector(5, "X"); // Do something silly... return 42; } For some reason I thought that the temporary std::vector object (right from the assignment sign)…
Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148
4
votes
10 answers

Emptying a C++ object

Often I add an Empty method to my C++ objects to clear the internal state using code similar to the following. class Foo { private: int n_; std::string str_; public: Foo() : n_(1234), str_("Hello, world!") { } void Empty() …
Rob
  • 76,700
  • 56
  • 158
  • 197
4
votes
2 answers

Utilizing move-semantics and element-wise initialization of containers

Often seen that examples of using STL algorithms are illustrated with list-initialized containers like: std::vector< int > v{1, 2, 3, 4}; But when this approach is used for (heavyweight) classes (unlike ints) it implies excessive copy operations of…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
4
votes
1 answer

I am getting 'Local Search phase started with an uninitialized Solution' when I run on a larger dataset

I am developing a solver using Optaplanner 6.1.0, similar to the Vehicle Routing Problem. When I run my solver on 700 installers and 200 bookings, it will successfully solve the planning problem. But, when I used against a larger dataset (700…
Pejal Hebat
  • 116
  • 6
4
votes
1 answer

C++11 enum class instantiation

I've encountered the following form of enum class variable instantiation and it is compiling without any warning or error under VS2012: UINT32 id; enum class X {apple, pear, orange}; X myX = X(id); Moreover, sending X(id) as an argument to a…
SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85
3
votes
2 answers

Construction vs Initialisation Formal difference

I am learning C++ using the books listed here. Now I came across the following statement from C++ Primer: When we allocate a block of memory, we often plan to construct objects in that memory as needed. In this case, we’d like to decouple memory…
Kal
  • 475
  • 1
  • 16
3
votes
2 answers

How to construct an octree in C++

I am implementing an Octree in C++ which should later contain a mesh for rendering. But at the moment I am struggeling with the construction of the Octree. To be more precisely it is the addNode() function which causes problems. I thought of a…
00Jan00
  • 117
  • 1
  • 12
3
votes
1 answer

At which phase is managed bean constructed and which constructor is used

Consider example of JSF based web-app hello1 from official tutorial with addition constructor in managed bean. The follow index.xhtml facelet …
user2953119