czwartek, 2 lutego 2023

STL, shared_ptr, make shared - example g++ console

#include <iostream>

#include <string>

#include <memory>


using namespace std;


class CPU

{

public:

    string model_Name;

    int cache_Size;

    double cpu_MHz;


    CPU(string a1, int a2, double a3)

    {

        model_Name=a1;

        cache_Size=a2;

        cpu_MHz=a3;

        cout<<"START\n";

    }


    ~CPU()

    {

        cout<<"END\n";

    }


};


shared_ptr<double>shared_Double;

shared_ptr<int>shared_Int;

shared_ptr<string>shared_String;


int main()

{

 auto date_CPU(make_shared<CPU>("Pentium III",1024,900.65));

 shared_Double=shared_ptr<double>(date_CPU,&date_CPU->cpu_MHz);

 shared_Int=shared_ptr<int>(date_CPU,&date_CPU->cache_Size);

 shared_String=shared_ptr<string>(date_CPU,&date_CPU->model_Name);

 cout<<"Report CPU: \n";

 cout<<"Model:  "<<*shared_String<<endl;

 cout<<"Cache: "<<*shared_Int<<endl;

 cout<<"MHz: "<<*shared_Double<<endl;

    return 0;

}

Brak komentarzy:

Prześlij komentarz