Does C++ force me to define a default constructor

mgttlinger

I come from a // world and am recently develloping in so please excuse if this is a dumb question.

When I declare a member variable in a class which should be initialized at some point at run time. For example if the member variable wraps functionality of a network device for which the user has to specify the address. After the user entered the address I would create the DeviceWrapper instance. My class would look something like this:

class A
{
public:
  //Method to instantiate dev and other stuff
private:
  DeviceWrapper dev;
}

When declaring the member variable it is my understanding that the default constructor of DeviceWrapper gets called if I don't explicitly call it in the constructor of A. However for the DeviceWrapper class it doesn't make sense to have a default constructor as the class is pointless without knowing the address of the device to wrap.

Am I missing something here or does C++ force me to either define a pointless default constructor in DeviceWrapper or make the class mutable so that the wrapper can receive its address after its creation?

Another alternative would be to make dev a pointer to DeviceWrapper. My idea of using pointers to objects is that the object that owns the instance has the instance directly and every other object that uses it but doesn't own it gets a pointer to the instance. This concept would be broken if I define dev as DeviceWrapper* dev;

So what would be the C++ solution to solve this problem?

Bill Lynch

The simplest solution is to wrap DeviceWrapper in one of:

std::optional<DeviceWrapper> dev;
std::unique_ptr<DeviceWrapper> dev;
std::shared_ptr<DeviceWrapper> dev;

All three will allow you to initialize DeviceWrapper dev after the construction of A. They all have different copy semantics, and you need to choose which makes sense for your application.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Define default constructor for record

Define default constructor for record

Constructor in C - Declare (in .h) and define (in .c) It does not behave as expected

users does not define no argument constructor

....does not define a no-argument constructor?

C++ default constructor does not initialize pointer to nullptr?

Why does c++ calls the parent default constructor implicitly?

C++ - Why does the default constructor work even in private inheritance

C++11 does not generates special functions / default constructor

How to define a default constructor that creates the previous object

Flutter define default value of a DateTime in constructor

How to define default constructor and user-defined constructor in the same line?

Why does this call the default constructor?

The class does not have a default constructor

Does a subclass need a default constructor?

What does the default constructor do?

Where in the C++ standard does it state that the default constructor is not generated when the copy constructor is deleted?

Does C++ create default "Constructor/Destructor/Copy Constructor/Copy assignment operator" for pure virtual class?

Why does this does not create a default constructor?

C++ copy Constructor and default constructor

Defining default constructor results in C2600 {cannot define a compiler-generated special member function (must be declared in the class first)

Calling the default constructor on c structs

C++ Structure default constructor

Visual C++: No default constructor

C++ default constructor syntax

How force developers to use constructor without arguments if value supposed to be default?

MATLAB Force constructor method to be called when assigning default array elements

How to force creation of default sequence constructor when other constructors are present?

Implicit super constructor Num() is undefined for default constructor. Must define an explicit constructor, what is the logic behind this