0

I have a class :

class InterruptDevice { public: InterruptDevice(); InterruptDevice(USHORT deviceAddress); ~InterruptDevice(); USHORT deviceAddress; bool obsolete; std::map<USHORT, std::vector<HIDReportDescriptorInputParse>> inputparser; std::map<BYTE, std::vector<BYTE>> endpoints;T }; 

where HIDReportDescriptorInputParse is struct :

typedef struct HIDReportDescriptorInputParse { HIDReportDescriptorInputParse() : inputSize(0), reportDefined(false), reportID(0), deviceAddress(0), globalItemUsage(0), localItemUsage(0) { } int inputSize; bool reportDefined; uint32_t reportID; uint32_t globalItemUsage; uint32_t localItemUsage; USHORT deviceAddress; std::vector<InputValues> inputValues; } HIDReportDescriptorInputParse, * PHIDReportDescriptorInputParse; typedef struct InputValues { InputValues() : GlobalUsagePage(0), ReportSize(0), ReportCount(0), UsageMinimum(0), UsageMaximum(0), LogicalMinimum(0), LogicalMaximum(0), Variable(true) { } std::vector<uint32_t> LocalUsageNames; uint32_t GlobalUsagePage; uint32_t ReportSize; uint32_t ReportCount; uint32_t UsageMinimum; uint32_t UsageMaximum; uint32_t LogicalMinimum; uint32_t LogicalMaximum; bool Variable; }InputValues, * PInputValues; 

Now, I want to have a class which takes InterruptDevice as a constructor argument, but I dont want it to be mandatory, so I need default argument value. Can it be written like this :

class A { public: A(int a, InterruptDevice device = InterruptDevice()); } 

When I try this, it works, but I am afraid of some undefined behaviour. Is this correct way how to do this ?

3
  • 2
    It will work, nothing wrong with this approach. You could also make 2 constructors for A, one that has InterruptDevice in params, and one that doesn't. Commented Feb 19, 2021 at 10:05
  • Should be correct. (You have a custom ~InterruptDevice() so you have to respect rule of 3/5). Commented Feb 19, 2021 at 10:06
  • This could help I think learn.microsoft.com/en-us/cpp/cpp/… Commented Feb 19, 2021 at 14:14

2 Answers 2

1

You can define alternative constructor or you can use defaults, that's usually a design choice. Creation of default value is an added cost though.

Additionally, to avoid repeating code and initialization lists, you can delegate one constructor to another, calling it in beginning of initialization list. The delegated constructor is one that would have base class's initialization.

Sign up to request clarification or add additional context in comments.

Comments

0

As far as I understand you also want a constructor A() in class A, correct? As such your implementation of that constructor could be something like

A::A() { a = 0; InterruptDevice device = InterruptDevice(); }; 

In the InterruptDevice() constructor you can define in a similar way how to initialize your member variables as you've done for HIDReportDescriptorInputParse.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.