What is the difference between a custom default constructor and defaulted default constructor in C++

What is the difference between a custom default constructor and defaulted default constructor in C++

default constructor c
default constructor java
c default constructor initialization
c default constructor initialization
default constructor and constructor with default arguments
non default constructor c++
parameterized constructor in c++
default constructor c#

I am working on a program that behaves differently when I set the default constructor to when I omit it. Specifically I am building an object to pass it to a third-party library function. When I omit the default constructor, the function works as expected, however when I define it the function blocks and does not return control, so I think the problem is due to the constructor.

Below I show a summary excerpt of the part of the code that has to do with the problem.

class Config {
public:
    typedef std::shared_ptr<Config> SPtr;

    int frame;
    int numSolidParticles = 0;
    bool shapesChanged = false;

    Buffer* buffer;
    FlexParams params;  // struct

    // if omitted then the 3rd party method works as expected
    Config() {
    }
};

...

Config::SPtr config = std::make_shared<Config>();

initialize(config);

...
SetFlexParams(config->params);
// calls to other 3rd party methods that depends on config
...

// call 3rd party library's method that causes the issue
mapBuffer(config->buffer);

Update: What I'm trying to do is moving some of the initializations (e.g. frame, buffer and other fields not shown) in method inititalize to constructor.


Default constructors, The implicitly-declared or defaulted default constructor for class T is undefined (​until C++11)defined as deleted (since C++11) if any of the  The default constructor is a type of constructor that is called automatically when the programmer has not defined any constructor in the program. In contrast, the parameterized constructor is a type of constructor defined by the programmer by passing parameters to give initial values to the instance variables in the class.


Although this does not answer the main question I would like to explain that the problem was being caused by a field of the FlexParams struct that I was forgetting to initialize.

This struct groups a large number of parameters to 3rd party library that are initialized in the initialize function. The funny thing is that when I do not define the constructor the field is initialized to 0 (which luckily was the correct value) while when I define it the field takes a random value.

The only reasonable explanation, as some of you mentioned in the comments, is that the issue was caused by undefined behavior.

Default constructors (C++ only), or explicitly defaulted C++11 ends constructor of A . No default constructor is created for a class that has any constant or reference type members. A constructor  Default Constructor. A default constructor is a 0 argument constructor which contains a no-argument call to the super class constructor. To assign default values to the newly created objects is the main responsibility of default constructor. Compiler writes a default constructor in the code only if the program does not write any constructor in


Default default constructor does one thing your manual default constructor does not: it properly default-constructs the members. I'm pretty sure that if you do either of the following your code will magically start working:

Option 1: Explicitly state default values for all the members (not just the two you have done so for)

int frame = 0;
int numSolidParticles = 0;
bool shapesChanged = false;

Buffer* buffer = nullptr;
FlexParams params = {};  // struct

Option 2: Remember to initialize all non-trivially-constructible members in your constructor

Config()
  : frame{0}
  , numSolidParticles{0}
  , shapesChanged{false}
  , buffer{nullptr}
  , params{} // I think this is the code generated by your compiler for the default default constructor
{
}

I personally prefer the first approach as it is clearer and way less prone to errors in a long run

Constructors (C++), a custom default constructor and defaulted default constructor in C++ Below I show a summary excerpt of the part of the code that has to do  Every direct base of T has a trivial default constructor Every non-static member of class type (or array thereof) has a trivial default constructor A trivial default constructor is a constructor that performs no action. All data types compatible with the C language (POD types) are trivially default-constructible. Example


Does C++ compiler create default constructor when we write our , Default constructors are one of the special member functions. more complex initialization, then you need to implement a custom copy constructor. For more information, see Explicitly Defaulted and Deleted Functions. A delegating constructor calls a different constructor in the same class to do some of  If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A(). This constructor is an inline public member of its class. The compiler will implicitly define A::A() when the compiler uses this constructor to create an object of type A. The constructor will have


8.5, In C++, compiler by default creates default constructor for every class. But, if we define our own constructor, compiler doesn't create the default constructor. In above statement, Default constructor will be called and you will see the following output: Default constructor is starting. Default constructor can be used to prepare some resources or for allocation of memory before you will start working with an object. Parameterized Constructor. The constructor with parameters can be used to initialize


Default constructor, Here is an example of a class that has a default constructor: we call six{ 6 } , the Fraction(int, int) function is called with the second parameter defaulted to value 1. results for classes that have multiple default parameters of different types. // Default constructor Box() = delete; A compiler-generated default constructor will be defined as deleted if any class members are not default-constructible. For example, all members of class type, and their class-type members, must have a default constructor and destructors that are accessible.