Home>
#include<iostream>#include<fstream>#include<vector>#include #include<functional>#include<string>#include<iomanip>using namespace std;
// Base class
class Bottom
{
public:
    short x1;// Drawing area Upper left X coordinate 2B
    short y1;// Drawing area upper left Y coordinate 2B
    short x2;// Drawing area Lower right X coordinate 2B
    short y2;// Drawing area Lower right Y coordinate 2B
    long foreColor;// Foreground color 4B
    long backColor;// Background color 4B
    int recg;// Derived class recognition
    void get (char h2, char h3, char h4, char h5, char h6, char h7, char h8, char h9, char h10, char h11, char h12, char h13, char h14, char h15, char h16, char h17 );
    // Get value
    virtual void get2 ();
    void get3 (int i);
    //display
    virtual void LK ();
    //constructor
    Bottom ();
    // Destructor
    virtual ~ Bottom ();
};
// straight line
class StraightLine: public Bottom
{
public:
    char lineKind;// Line type (0: solid line, 1: dotted line)
    short startX;// Drawing start X coordinate 2B
    short startY;// Drawing start Y coordinate 2B
    short endX;// End of drawing X coordinate 2B
    short endY;// End of drawing Y coordinate 2B
    //constructor
    StraightLine ();
    // Destructor
    ~ StraightLine ();
    // Get value
    void get2 (char h18, char h20, char h21, char h22, char h23, char h24, char h25, char h26, char h27);
    //display
    void LK ();
};
// Base constructor
Bottom :: Bottom ()
{
}
// Base destructor
Bottom :: ~ Bottom ()
{
}
// Underlying representation
void Bottom :: LK () {
    cout<<"bottom and de"<<endl;
}
void Bottom :: get (char h2, char h3, char h4, char h5, char h6, char h7, char h8, char h9, char h10, char h11, char h12, char h13, char h14, char h15, char h16 , char h17) {
    // unsigned is used because I want to move the bit without sign
    // Drawing area Upper left X coordinate
    x1 = (unsigned char) h3;
    x1<<= 8;// Move 8 bits to the left
    x1 = (x1 | (unsigned char) (h2&0xff));// Concatenate data
                                                                                                      // Drawing area Upper left Y coordinate
    y1 = (unsigned char) h5;
    y1<<= 8;// Move 8 bits to the left
    y1 = (y1 | (unsigned char) (h4&0xff));// Concatenate data
                                                                                                      // Drawing area Lower right X coordinatex2 = (unsigned char) h7;
    x2<<= 8;// Move 8 bits to the left
    x2 = (x2 | (unsigned char) (h6&0xff));// Concatenate data
                                                                                                      // Drawing area Lower right Y coordinate
    y2 = (unsigned char) h9;
    y2<<= 8;// Move 8 bits to the left
    y2 = (y2 | (unsigned char) (h8&0xff));// Concatenate data
                                                                                                      // Foreground color
    foreColor = (unsigned char) h13;
    foreColor<<= 8;// Move 8 bits to the left
    foreColor = (foreColor | (unsigned char) (h12&0xff));// Concatenate data
    foreColor<<= 8;// Move 8 bits to the left
    foreColor = (foreColor | (unsigned char) (h11&0xff));// Concatenate data
    foreColor<<= 8;// Move 8 bits to the left
    foreColor = (foreColor | (unsigned char) (h10&0xff));// Concatenate data
                                                                                                                                     //Background color
    backColor = (unsigned char) h17;
    backColor<<= 8;// Move 8 bits to the left
    backColor = (backColor | (unsigned char) (h16&0xff));// Concatenate data
    backColor<<= 8;// Move 8 bits to the left
    backColor = (backColor | (unsigned char) (h15&0xff));// Concatenate data
    backColor<<= 8;// Move 8 bits to the left
    backColor = (backColor | (unsigned char) (h14&0xff));// Concatenate data
}
// Get base value
void Bottom :: get2 () {
}
void Bottom :: get3 (int i) {
    recg = i;
}
// Straight line constructor
StraightLine :: StraightLine ()
{
    x1 = 0;
    y1 = 0;
    x2 = 0;
    y2 = 0;
    foreColor = 0;
    backColor = 0;
    recg = 0;
    lineKind = 0;
    startX = 0;
    startY = 0;
    endX = 0;
    endY = 0;
}
// Straight line destructor
StraightLine :: ~ StraightLine ()
{
}

// Get linear value
void StraightLine :: get2 (char h18, char h20, char h21, char h22, char h23, char h24, char h25, char h26, char h27)
{
    lineKind = h18;// Linetype
                      // Start drawing X coordinate
    startX = (unsigned char) h21;
    startX<<= 8;// Move 8 bits to the left
    startX = (startX | (unsigned char) (h20&0xff));// Concatenate data
                                                                                                             // Start drawing Y coordinate
    startY = (unsigned char) h23;
    startY<<= 8;// Move 8 bits to the left
    startY = (startY | (unsigned char) (h22&0xff));// Concatenate data
                                                                                                             // End of drawing X coordinate
    endX = (unsigned char) h25;
    endX<<= 8;// Move 8 bits to the left
    endX = (endX | (unsigned char) (h24&0xff));// Concatenate data
                                                                                                             // End of drawing Y coordinate
    endY = (unsigned char) h27;
    endY<<= 8;// Move 8 bits to the left
    endY = (endY | (unsigned char) (h26&0xff));// Concatenate data}
// Straight line display
void StraightLine :: LK () {
    cout<<"Drawing area (X1, Y1, X2, Y2) Foreground color, background color Line type drawing start, end"<<endl;
    cout<<"\" straight line ["<<setfill ('')<<right<<setw (4)<<dec<<x1<<","<<setfill ('')<<right<<setw ( 4)<<dec<<y1<<"]"
      <<"-["<<setfill (''')<<right<<setw (4)<<dec<<x2<<","<<setfill (''')<<right<<setw (4)<<dec<<y2<<"]"
      <<"[0x"<<setfill ('0')<<setw (6)<<hex<<foreColor<<"] [0x"<<setfill ('0')<<setw (6)<<hex<<backColor<<"]";

    if (lineKind == 0) {
        cout<<"solid line";
    }
    else {
        cout<<"dotted line";
    }
    cout<<"["<<setfill ('')<<dec<<setw (4)<<startX<<","<<setfill (''')<<dec<<setw (4)<<startY<<"]";
    cout<<"["<<setfill ('')<<dec<<setw (4)<<endX<<","<<setfill (''')<<dec<<setw (4)<<endY<<"]";

    // printf ("[% 4d,% 4d]-[% 4d,% 4d] \" \ n ", startX, startY, endX, endY);
    cout<<endl;
    cout<<endl;
}
int main () {
    // Straight line instance
    StraightLine * straightine;
    // Instance for linear output
    Bottom * straightine2;
    try {
        straightine2 = new StraightLine;
    }
    catch (std :: bad_alloc&e) {
        // Failed to allocate memory
        std :: cout<<e.what ()<<std :: endl;
    }
    std :: vector<Bottom *>bases;// Table to put everything

    // Secure linear buffer capacity
    try {
        straightine = new StraightLine [2];
    }
    catch (std :: bad_alloc&e) {
        // Failed to allocate memory
        std :: cout<<e.what ()<<std :: endl;
    }
    // store the value in the instance
    straightine->get (27, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
    straightine->get2 (0, 0, 0, 0,0, 0, 0, 0, 0);
    straightine->get3 (1);
    straightine->get (27, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
    straightine->get2 (0, 0, 0, 0, 0, 0, 0, 0, 0);
    straightine->get3 (1);
    // Push the value to the vector
    bases.push_back (straightine);
    // Increment pointer
    * straightine ++;
    for (auto it = std :: begin (bases);it! = std :: end (bases);++ it)
    {
        // Linear output
        if (dynamic_cast<StraightLine *>(* it)! = 0)
        {
            straightine2 = dynamic_cast<StraightLine *>(* it);
            straightine2->LK ();
        }
    }
    // I want to delete straightine and straightine2 here
    return 0;
}


I'm having trouble deleting the pointer class.
It seems that you can comment out the function, but I don't understand the cause.

c++
  • Answer # 1

      straightine2 = new StraightLine;
    (Omitted)
        straightine = new StraightLine [1];
    (Omitted)
        * straightine ++;// changing value
    (Omitted)
        straightine2 = dynamic_cast<StraightLine *>(* it);// changing value
    (Omitted)
        // I want to delete straightine and straightine2 here

    I'm having trouble deleting the pointer class.

    straightineWhenstraightine2This is because the value of is changed.
    Save it to another variable, etc.deleteTonewPlease pass the address returned by.


    Yumetodo also commented, but raw pointers can cause missed releases and double releases.
    Consider using smart pointers (unique_ptr/shared_ptr/weak_ptr) in your C ++ code.

  • Answer # 2

    Since straightine2 should not be new, delete is not necessary either.
    Only straightine, delete [].

    #include<iostream>#include<vector>#include<iomanip>using namespace std;
    class Bottom {// base class
    public:
        short x1;// Drawing area Upper left X coordinate 2B
        short y1;// Drawing area Upper left Y coordinate 2B
        short x2;// Drawing area Lower right X coordinate 2B
        short y2;// Drawing area Lower right Y coordinate 2B
        long foreColor;// Foreground color 4B
        long backColor;// Background color 4B
        int recg;// Derived class recognition
        void get (char h2, char h3, char h4, char h5,
                char h6, char h7, char h8, char h9,
                char h10, char h11, char h12, char h13,
                char h14, char h15, char h16, char h17);
        virtual void get2 ();// Get value
        void get3 (int i);
        virtual void LK ();// Display
        Bottom ();// Constructor
        virtual ~ Bottom ();// Destructor
    };
    class StraightLine: public Bottom {// straight line
    public:
        char lineKind;// Line type (0: solid line, 1: dotted line)
        short startX;// Drawing start X coordinate 2Bshort startY;// Drawing start Y coordinate 2B
        short endX;// End of drawing X coordinate 2B
        short endY;// End of drawing Y coordinate 2B
        StraightLine ();// Constructor
        ~ StraightLine ();// Destructor
        void get2 (char h18,
                char h20, char h21, char h22, char h23,
                char h24, char h25, char h26, char h27);// Get value
        void LK ();// Display
    };
    Bottom :: Bottom () {} // Base constructor
    Bottom :: ~ Bottom () {} // Base destructor
    void Bottom :: LK () {cout<<"Bottom and de"<<endl;} // Underlying representation
    using uc = unsigned char;
    short val2 (char a, char b) {return uc (b)<<8 | uc (a);}
    long val4 (char a, char b, char c, char d) {
        return uc (d)<<24 | uc (c)<<16 | uc (b)<<8 | uc (a);
    }
    void Bottom :: get (char h2, char h3, char h4, char h5,
            char h6, char h7, char h8, char h9,
            char h10, char h11, char h12, char h13,
            char h14, char h15, char h16, char h17) {
        x1 = val2 (h2, h3), y1 = val2 (h4, h5);
        x2 = val2 (h6, h7), y2 = val2 (h8, h9);
        foreColor = val4 (h10, h11, h12, h13);
        backColor = val4 (h14, h15, h16, h17);
    }
    void Bottom :: get2 () {}
    void Bottom :: get3 (int i) {recg = i;}
    StraightLine :: StraightLine () // Straight line constructor
    {
        x1 = y1 = x2 = y2 = 0;
        foreColor = backColor = 0;
        recg = lineKind = 0;
        startX = startY = endX = endY = 0;
    }
    StraightLine :: ~ StraightLine () {}
    void StraightLine :: get2 (char h18,
            char h20, char h21, char h22, char h23,
            char h24, char h25, char h26, char h27)
    {
        lineKind = h18;
        startX = val2 (h20, h21), startY = val2 (h22, h23);
        endX = val2 (h24, h25), endY = val2 (h26, h27);
    }// Straight line display
    void StraightLine :: LK ()
    {
        cout<<"Drawing area (X1, Y1, X2, Y2) Foreground color, background color Line type drawing start, end"
          <<endl<<setfill ('')<<right
          <<"\" Straight line ["<<setw (4)<<x1<<","<<setw (4)<<y1<<"]"
          <<"-["<<setw (4)<<x2<<","<<setw (4)<<y2<<"]"
          <<hex<<setfill ('0');
          <<"[0x"<<setw (6)<<foreColor<<"] [0x"<<setw (6)<<backColor<<"]"
          <<dec<<setfill ('');
          <<(lineKind? "Dotted line": "Solid line")
          <<"["<<setw (4)<<startX<<","<<setw (4)<<startY<<"]"
          <<"["<<setw (4)<<endX<<","<<setw (4)<<endY<<"]"
          <<endl<<endl;
    }
    int main ()
    {
        StraightLine * straightine = new StraightLine [2];// Instance for straight lines
        std :: vector<Bottom *>bases;// Table to put everything
        // store the value in the instance
        straightine [0] .get (27, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
        straightine [0] .get2 (0, 0, 0, 0, 0, 0, 0, 0, 0);
        straightine [0] .get3 (1);
        straightine [1] .get (27, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
        straightine [1] .get2 (0, 0, 0, 0, 0, 0, 0, 0, 0);
        straightine [1] .get3 (1);
        // Push the pointer to the vector
        bases.push_back (straightine);
        bases.push_back (straightine + 1);
        for (auto it = std :: begin (bases);it! = std :: end (bases);++ it) {
            Bottom * straightine2 = dynamic_cast<StraightLine *>(* it);
            if (straightine2) straightine2->LK ();
        }
        delete [] straightine;
        return 0;
    }


    The name is terrible.
    The base is Base, not Bottom. Although vector is bases.
    What is a straightine variable that points to an instance of the StraightLine class?
    What are get, get2, and get3 to store values?
    Base's virtual get2 () and StraightLine's get2 (...) have different numbers of arguments.
    It will not be overridden. Although the display virtual LK () is overridden.
    Neither dynamic_cast nor straightine2 is needed because LK () is overridden.
    (* it)->LK ();is enough.