Std :: map, links, pointers and memory allocation

I have a hard time with the map and the distribution of valuetype.

consider this simple class:

class Column {
private:
    char *m_Name;
public:
    // Overrides
    const char *Name(){
        return this->m_Name;
    }

    // Ctors
    Column(const char *NewName){
        this->m_Name = new char[strlen(NewName) + 1];
        strcpy(this->m_Name, NewName);
    }

    // Dtors
    ~Column(){
        cout << "wtf?\n";
        delete this->m_Name;
    }
};

I now have this card:

// Typedefs
typedef std::map<int, Column> ColumnContainer;
ColumnContainer *m_Container;

When I call this:

Column *c = new Column("Test");
cout << "CREATED: " << c->Name() << "\n";
it = this->m_Container->insert(std::make_pair(0, *c)).first;
cout << "AGAIN: " << c->Name() << "\n";

console prints "wtf?" after pasting on the map.

he seems to be destroying the column. Is it correct?

or am i doing something wrong?

I was wondering if value_type std::mapthe structure type has a specific memory size, for example, with a POD or a POD array?

the cout << AGAIN does not print "Test"

i need a map for columns based on int key

+3
source share
6 answers

, m_Name , , STL . . - m_Name .

, . .

+3

make_pair(0, *c) (, ) pair<int, Column>. map::insert , , std::map , . , Column, .

copy-construction , .

+7

:

. ( ). ? ( - .)
.

Column *c = new Column("Test");

, std:: pair < int, Column > ( , M_name ( )).

std::make_pair(0, *c)

< int, Column > . ( make_pair, ). M_name . , .

m_Container->insert( pairObject )

, std:: make_pair(), , . . , , .

.

std::string ( )
RAW, . , , :

  • destructror

:

, Java-.

Column *c = new Column("Test");
it = this->m_Container->insert(std::make_pair(0, *c)).first;

:

m_Container[0] = Column("Test");

.
Infact, .

, , RAW, .

class X
{
    char*   m_name;
  public:
    X(char const* name)  {m_name new char[strlen(m_name) +1];strcpy(m_name,name);}
    ~X()                 {delete [] m_name;}
};

. . , , , RAW.

X::X(X const& copy)
    :m_name(copy.m_name)
{}

X& X::operator=(X const& copy)
{
    m_name = copy.m_name;
}

:

X    x("Martin");
X    y(x);

"x" "y" (m_name), . "y" , derstructor, [] . "x" .

Z    z("Bob");
z = x;

, , .

?
. Coloumn. Copy . , . , .

doWork(Column const& x) { /* Do somthing intersting */

doWork(Column("Hi There"));

Column, doWork(). doWork() , . , doWork() , costructor, ? , , . , . .

+4

m_Name , .

, m_Name , .

,

class Column {
private:
    std::string m_Name;
public:
    // Overrides
    const char *Name(){
        return m_Name.c_str();
    }
};

++, .

+2

std::string ? ? .

... ( delete[], new[])

, ?

, :

class Column
{
public:
  Column() : m_name() {}
  Column(const std::string& name) : m_name(name) {}

  const std::string& getName() const { return m_name; }

private:
  std::string m_name;
};

:

std::map<int,Column> m_container;

Column myColumn = Column("Test");
std:cout << "CREATED: " << myColumn.getName() << std::endl;
m_container[0] = myColumn; // COPY the column
std::cout << "AGAIN: " << myColumn.getName() << std::endl;

.

m_container[0] = Column("Test");

C ++ already requires a fair amount of code bloat, allowing you to use the shortcuts it offers when possible.

0
source

What you see is that the temporary copy of the column is destroyed. If you push on the constructor, you will see that the created copy.

0
source

Source: https://habr.com/ru/post/1721421/


All Articles