836 questions
4
votes
3
answers
191
views
Why is having ambiguous (default) constructor due to overloading not an error or at least a warning?
The following class compiles just fine:
class Time {
public:
Time(int h = 0, int m = 0, int s = 0); // constructor with default parameters
Time(); // default ...
1
vote
3
answers
115
views
Calling parameterized constructor only to update one member variable
I have a class with a default constructor which initializes many member variables via initializer lists.
I wish to have a parametrized constructor which only updates one of the member variables. A ...
2
votes
1
answer
136
views
Member of struct constructed twice in custom constructor?
struct PhiBlock
{
int64_t bsize; // block size
vector<int64_t> ind; // 0/1 to track [pmin(y) > pb]
fenwick_tree phi_sum; // partial sums
PhiBlock ...
0
votes
1
answer
96
views
std::string and default move constructor causing heap corruption
While refactoring some code, I've run into an issue with heap corruption.
namespace GPU
{
struct ShaderStage
{
public:
ShaderStage(ShaderStageType, const Path&);
...
6
votes
1
answer
318
views
Overloaded default constructor with initializer list constructor that is also a default constructor and list initialization
Consider the following example that compiles with clang but is rejected by edg, gcc and msvc. Demo
#include <initializer_list>
struct C
{
C(){}
C(std::initializer_list<int> i = {...
3
votes
1
answer
173
views
If I do not declare move constructor, the copy one is called, but if I delete the move constructor - compilation error - why?
struct X
{
X() = default;
X(const X& src)
{
cout << "copy" << endl;
}
};
int main()
{
X x1;
X x2(move(x1));
}
Output:
copy
struct ...
1
vote
1
answer
121
views
Trying to understand the different ways of initializing std::string
std::string str;
std::string(str);
std::string str = {};
Are they the same? If no then what's the difference? And when should I use each of them?
New to C++ and trying to understand the different ...
2
votes
0
answers
113
views
Difference between deleting and not writing a constructor [duplicate]
The following code snippet
#include <iostream>
#include <vector>
class IntegerContainer {
public:
IntegerContainer()
{
std::cout << "default" << std::...
2
votes
3
answers
457
views
Why do we still need to implement 'noArgsConstructor' if Java gives a non-parameterized constructor by default?
In Spring boot if we are creating a POJO class and we end up creating only a parameterized constructor and not any default constructor then Java will throw errors, why does this happen if Java ...
1
vote
2
answers
154
views
What value to assign to a char* in default constructor c++?
I have a class String and I've made a field char* name, default, parameterized, copy constructor, destructor and overwritten operator =.
My question is how my default constructor should look like for ...
1
vote
3
answers
195
views
Are these three default constructors equivalent in C++?
Consider the following code:
#include <type_traits>
template<typename T>
struct A1 {
T t;
// implicitly-declared default constructor
};
template<typename T>
struct A2 {
...
0
votes
1
answer
64
views
Using default constructor to set the object attribute to default value in C++
I created a class Event
class Event {
char m_event_desc[DESC_LENGTH + 1]; // description of the event
unsigned int m_time; // time when the event starts
unsigned int getHour();
...
-1
votes
1
answer
107
views
why default constructor is not used in value initialisation in c++ [duplicate]
c++ primer says :
The default constructor is used automatically whenever an object is default or
value initialized.
Default initialization happens
• When we define nonstatic variables (§ 2.2.1, p. 43)...
0
votes
1
answer
82
views
Non-aggregate initialization
I need to implement an AutoDocs class that will contain information about the license plate number and driver's full name. It is necessary to implement the class with the following invariants: the ...
-1
votes
2
answers
183
views
Could an implicit compiler created default constructor have more than a null body?
Could an implicit compiler created default constructor have more than a null body?
According to IBM's website, the answer is: no.
I have this project that's kind of stumping me though:
This is where ...
0
votes
0
answers
37
views
C# default constructor even if there is an explicit constructor [duplicate]
I've defined a C# struct:
public struct Separator {
readonly StringBuilder sb;
readonly char ch;
bool more;
public void Write() {
if (more)
sb.Append(ch);
...
0
votes
2
answers
69
views
How to write conditional instantiation in C#
I have a below lambda default Employee constructor which is working fine.
this.For<IEmployee>().Use(() => new Employee());
Now I want to call another another constructer based on flag value.
...
3
votes
1
answer
2k
views
Why is the explicitly defaulted default constructor implicitly deleted when there is a const std::vector data member?
I've got a code really similar to the snippet below:
#include <vector>
struct dummy
{
std::vector<int> const data;
dummy() = default;
};
Most compilers accept this code without a ...
17
votes
1
answer
699
views
Why do I need to specify the type of a default constructed object in this situation?
I don't understand why in foobar below I need to specify std::vector<int>{} whereas in foobar2 I do not:
#include <iostream>
#include <memory>
#include <vector>
#include <...
3
votes
0
answers
402
views
initializing from empty std::initializer_list vs default constructor
I've got a class that basically contains a std::vector.
I can default-construct an object of this class, leaving the contained std::vector empty.
I can also value-construct it from a set of values, ...
0
votes
2
answers
122
views
initialize array of object without default constructor
Is there a way to initialize an array of objects which don't have a default constructor?
struct IndexAndSign {
const int depth;
const bool baseIsPositive;
IndexAndSign(int depth, bool ...
1
vote
1
answer
121
views
Is there a way to implement the same behaviour of the new operator calling default constructors?
I want to implement my own heap allocation function to do the same as the C++ new operator when it comes to allocating elements of a class type. My allocation function (for Windows) does not use the ...
-2
votes
1
answer
81
views
Subclass declares "no appropriate default constructor available" [closed]
I made a class with a default constructor and a subclass. When I try to create an instance of the subclass using a default constructor, I get the errors
C2512 (no appropriate default constructor ...
0
votes
1
answer
76
views
Deleting a struct that has a class member
I have a struct:
struct holder
{
int val;
std::unordered_map<int, int> num_to_addr;
};
I dynamically allocate a struct holder:
struct holder* handle = new struct holder;
I do work and ...
1
vote
1
answer
95
views
All variables in new object are set to zero, despite default arguments in constructor
I am writing a student project. For some reason all variables of this object are set to 0 or false, despite the fact that the constructor have default arguments set. The class of this object inherit ...
1
vote
1
answer
151
views
Why does the copy constructor is not called?
#include <iostream>
#include <memory>
using namespace std;
class Init {
private:
int x;
public:
Init(int y) {
x = y;
cout << "default constructor ...
0
votes
1
answer
496
views
error: no matching function for call to copy constructor, c++
Sorry in advance for what may be a bad post. I've scoured stackoverflow for pre existing posts that answer my question, but although many posts on here are similar, none of them seem to apply to my ...
-3
votes
1
answer
1k
views
Getting "No default constructor exists for class" error
I have been trying to make a struct with general information I want to use throughout my program. I'm updating this information in my Mario::init() function, but now its giving me this error:
No ...
1
vote
0
answers
66
views
Why does MSVC not auto-generate code for an explicitly-defaulted, noexcept default constructor used to copy construct a designated initializer?
See this godbolt:
struct Foo
{
Foo() noexcept = default;
int t{};
};
struct Bar
{
Foo baz{};
};
int main()
{
return Bar
{
.baz = Foo{}
}.baz.t;
}
When deleting ...
-2
votes
1
answer
177
views
What is the usage of default and delete in class constructors [duplicate]
class MyClass : public ParentClass
{
public:
explicit MyClass(classA a, const classB b) : A(a), B{b} {}
MyClass() override = default;
MyClass(const MyClass&) = delete;
...
0
votes
0
answers
178
views
Where to declare a default constructor in C++?
Is there any difference where (e.g. in a header or in a source file) to declare a default constructor? I mean, is there any difference in time execution performance in these cases?
That is, should
// ...
0
votes
1
answer
1k
views
mapstruct and lombok: How to avoid usage of no args constructor in generated mapper?
I want mapstruct to not use a no-args-constructor even though it exists in my DTOs and Entities (as far as I know jsonb, jpa, jaxb, ... usually require a no-args-constructor):
@RequiredArgsConstructor
...
2
votes
1
answer
121
views
How to create an array of unique_ptrs with custom deleters?
Here's what I have:
Demo
#include <cstdio>
#include <memory_resource>
#include <memory>
#include <string_view>
#include <array>
#include <utility> /* declval */
...
1
vote
1
answer
109
views
Preventing the creation of a synthetic constructor
While exploring the ArrayList class, I can't figure out the following part (constructor of the inner class Itr):
// prevent creating a synthetic constructor
Itr() {}
I kinda understand what the "...
-1
votes
2
answers
183
views
Can someone explain why this doeasn't work? The default constructor of "B" cannot be referenced -- it is a deleted function [closed]
I'm currently making c++ project but this error is bothering me for long time and i cannot figure out why this doesn't work.
I was searching about this error but still i don't understand it.
Thanks in ...
3
votes
2
answers
130
views
Why can this C++ child class be constructed by objects of parent class type [duplicate]
class AAA
{
int m_Int;
public:
AAA() : m_Int{12} {}
};
class BBB
{
int m_Int1;
public:
BBB() : m_Int1{12} {}
};
class CCC : public AAA, public BBB {};
AAA a;
BBB b;
CCC c{ a, b };
...
1
vote
1
answer
386
views
Why adding `explicit` to a defaulted copy constructor prevents returning an object? [duplicate]
Considering this MRE (the real case involves some class with some inheritance and some member variables)
class A {
public:
A() = default;
explicit A(const A&) = default;
// ...
1
vote
1
answer
1k
views
How to designated initialize a C++ struct that has a construcotr? [duplicate]
I have a very large struct that has customized copying constructor, customized moving constructor, customized moving assignator, and customized copying assignator, but I also need to use Designated ...
0
votes
1
answer
67
views
default constructor called instead of parameterized constructor
**the first constructor is supposed to take words from a txt file and the second one takes words from the string and add it to fileVec vector.
i'm trying to call a parameterized constructor but it ...
1
vote
2
answers
118
views
Why operator= and copy constructor are treated differently in virtual inheritance?
It seems that in virtual inheritance, operator= and copy constructor are treated differently. Consider the following code:
#include <iostream>
#include <ostream>
class A {
public:
A(...
0
votes
1
answer
117
views
Initializing an array of objects created on the heap
Given the non trivial data structure:
claas MyClass
{
public:
MyClass():x(0), p(nullptr)
{}
private:
int x;
int* p;
};
Is there any guarantee provided by the c++ specification that the ...
1
vote
1
answer
202
views
How to declare a default constructor for subclass?
Very recently, I have started learning C++ full-time.
It is my understanding that constructors in C++ are not inherited by subclasses, and therefore must be declared in the subclass.
Below, I have a ...
0
votes
3
answers
555
views
Does declaring a constexpr object marks the constructor as constexpr
I just a have a problem in understanding when the compiler marks the constructor as constexpr.
If I write the following program:
struct S{ S() {}; }
constexpr S s{ };
Does this mean that the default ...
0
votes
0
answers
22
views
Error when creating default inherited constructor
I am making an object class inherited from my base class. I am trying to make my default non argument constructor using this(), and it keeps highlighting red when I try to make a default Date object ...
1
vote
2
answers
1k
views
Assigning a class variable in class definition versus at class instantiation
What are the ramifications of assigning a class variable when defining the class versus in the class constructor? Is the variable assigned in the class definition accessible by all class instances?
...
1
vote
0
answers
115
views
Kotlin NoArg plugin ignores declaration assignment
This problem is related to Kotlin noarg plugin not initializing default values.
I've got my NoArg plugin set up like this:
plugins {
id "org.jetbrains.kotlin.plugin.noarg" version "...
1
vote
2
answers
545
views
Base class default constructor in derived class constructor initializer list
I have seen a lot of times people adding the default constructor of base class in the derived class constructor initializer list like so
DerivedClass::DerivedClass(int x) : BaseClass(), member_derived(...
-1
votes
1
answer
48
views
Why the parameterless constructor is called without "new" keyword
I am doing a little .NET application to get use to it but I struggle to understand something.
When I pass through the following line of code the parameterless constructor of my Historique class is ...
1
vote
1
answer
535
views
Value-initialization of class types
Per cppreference, the syntax for value initialization is:
[..]
T object {}; (since C++11)
[..]
It's already known that value-initialization is performed when an object is constructed with an empty ...
1
vote
2
answers
492
views
Why specifying "user-provided" constructor makes the class non-aggregate? [duplicate]
I came across this example from cppreference:
...
struct T3
{
int mem1;
std::string mem2;
T3() {} // user-provided default constructor
}
...
This example clearly show that the given ...