Home > Back-end >  How to use c pointer containers
How to use c pointer containers

Time:03-04

I have a base class BaseOption
 class BaseOption {
Protected:
BaseOption (char short_name, STD: : string & amp; The name, STD: : string & amp; The description)
: short_name (short_name) {
This - & gt; Name=& amp; The name;
This - & gt; Description=& amp; The description;
}

~ BaseOption () {
Delete the name;
Delete the description;
}

Friend STD: : ostream & amp; Operator<(STD: : ostream & amp; Out, const BaseOption & amp; Option) {
Return the out & lt; <"-" & lt; }

Char short_name;
STD: : string * name;
STD: : string * description;
};

Then have a derived class DefaultOption:
 class DefaultOption: public BaseOption {
Public:
DefaultOption (char short_name, STD: : string & amp; The name, STD: : string & amp; The description)
: BaseOption (short_name, name, description) {}
};

There is also a kind of Options:
 class Options {
Public:
The Options ()=default;

~ the Options () {
For (const auto & amp; Item: options) {
The delete & amp; The item;
}
}

Void the Add (STD: : string name, STD: : string description) {
Char short_name=0;
STD: : string _name;
If (name. The length () & gt; 1) {
If (name [1]=='|') {
Short_name=name [0];
_name=STD: : string (name, 2, and the name. The length ());
}
} else if (the name length ()==1) {
Short_name=name [0];
}
DefaultOption option (short_name, _name, description, flag);
//TODO
}

Friend STD: : ostream & amp; Operator<(STD: : ostream & amp; Out, const Options & amp; The options) {
For (const auto & amp; Item: options. The options) {
STD: : cout & lt; }
Return the out;
}

Private:
STD: : vector};

The Add method in class Options of how to Add elements, like the Options have been submitted to the invalid pointer,

CodePudding user response:

If it involves class contains Pointers, it is better to realize his copy constructors, assignment function, if you pay attention to efficiency, and move the constructor, and move the mutator

CodePudding user response:

In addition as a base class, it is better to write the virtual destructors

CodePudding user response:

reference 1/f, the truth is right or wrong response:
if it involves class contains Pointers, realize his best copy constructors, assignment function, if you pay attention to efficiency, and move the constructor, and move the mutator

Ok, I try

CodePudding user response:

The options. The push_back (new BaseOption ());
  • Related