Your 'first' is a raw
pointer. The size of a pointer is enough to store a memory address. Nothing more, nothing less. The size of non-pointer types has no relation to the size of pointers.
The
std::string
is a complex type. Its implementation can be different on different compilers. The std::string does store some text, but the size of the stored text is different from the size of the std::string object.
I found that strcpy can be used |
The
memcpy
is a
C function.
The
strcpy
is a
C function.
The
std::string
is a
C++ type.
C functions were not made to handle C++ constructs.
C functions operate on raw pointers and plain arrays.
The proper header to include the definition of std::string is
<string>
.
The header
<cstring>
contains C library's C functions.
You have a fixation for
pointers and
manual dynamic allocation (new). While they are good to know and understand, they are less convenient and more error-prone than C++ Standard Library constructs, like
std::string
and
smart pointer types.
This you could strive for:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#include <iostream>
#include <string>
using std::string;
struct Mystruct {
string name;
Mystruct( const string & name )
: name ( name )
{}
Mystruct() = default;
};
int main ()
{
string first {"Some"};
string second {"Dome"};
second = first;
std::cout << " The new secondvalue is " << second << '\n';
Mystruct third {"invite"};
second = third.name;
std::cout << " The new secondvalue is " << second << '\n';
return 0;
}
|