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 coordinatey1 = (unsigned char) h5;
    y1<<= 8;// Move 8 bits to the left
    y1 = (y1 | (unsigned char) (h4&0xff));// Concatenate data
                                                                                                      // Drawing area Lower right X coordinate
    x2 = (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 () {
    // Instance for straight line (put elements in this array)
    shared_ptr<StraightLine>straightine (new StraightLine) [2];
    // Instance for linear output
    std :: vector<std :: shared_ptr<Bottom >>bases;// Table to put all
    shared_ptr<StraightLine>straightine2 (new StraightLine);
// Enter by processing here
    for (int i = 0;i<2;i ++) {
        // 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);
        // Push the value to the vector
        bases.push_back (straightine);
       
        // Originally, the pointer address was incremented here.
        
    }
    
    
   
    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 ();
        }
    }
    return 0;
}

I made the instance into an array of shared_ptr, but I want to put the elements in each array, then put it in the vector table, and then turn it with an iterator to output each, but I don't know how. I would appreciate it if anyone could tell me.

c++
  • Answer # 1

    Is this what you want to do?

    int main () {
        std :: vector<std :: shared_ptr<Bottom >>bases;// Table to put all
        for (int i = 0;i<2;i ++) {
            std :: shared_ptr<StraightLine>straightine = std :: make_shared<StraightLine>();
            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);
            bases.push_back (straightine);
        }
        // Put something other than StraightLine for checking dynamic_pointer_cast
        bases.push_back (std :: make_shared<Bottom>());
        for (auto base: bases)
        {
            std :: shared_ptr<StraightLine>straightine = std :: dynamic_pointer_cast<StraightLine>(base);
            if (straightine)
            {
                // Want to call a method that exists only in StraightLine or perform additional processing?
                // straightine->doSomething ();
                straightine->LK ();
            }
            else else
            {
                // For dynamic_pointer_cast confirmation
                base->LK ();
            }
        }
        return 0;
    }

  • Answer # 2

    If you really want to use arrays with smart pointers, it looks like this.
    With a smart pointer, you cannot point to an element in the middle of the array, so refer to it with a normal pointer.

    int main () {
        // Instance for straight line (put elements in this array)
        shared_ptr<StraightLine []>straightine (new StraightLine [2]);
        // Instance for linear output
        std :: vector<Bottom *>bases;// Table to put everything
        StraightLine * straightine2 = nullptr;
        // Enter in the process here
        for (int i = 0;i<2;i ++) {
            // store the value in the instance
            straightine [i] .get (27, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
            straightine [i] .get2 (0, 0, 0, 0, 0, 0, 0, 0, 0);
            straightine [i] .get3 (1);
            // Push the value to the vector
            bases.push_back (&​​straightine [i]);
        }
        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 ();
            }
        }
        return 0;
    }


    However, in this case, I think it is better to change straightine to vector.

      vector<StraightLine>straightine (2);