Spionageklassen in C++ (Spionageklassen in C++), Lektion, Seite 721208
https://www.purl.org/stefan_ram/pub/spionagekonstruktoren_c++ (Permalink) ist die kanonische URI dieser Seite.
Stefan Ram
C++-Kurs

Spionageklassen in C++ 

main.cpp

#include <initializer_list>
#include <iostream>
#include <ostream>
#include <string>
#include <vector>

using namespace ::std::literals;

struct object

{ object()

{ ::std::clog << "constructed\n"; }

~object()
{ ::std::clog << "DESTROYED\n"; }

::std::string name; };

int main()
{ ::std::clog << "start\n";

{ object o;

::std::clog << "using\n"; }

::std::clog << "end\n"; }

transcript

start

constructed

using

DESTROYED

end

main.cpp

#include <initializer_list>
#include <iostream>
#include <ostream>
#include <string>
#include <vector>

using namespace ::std::literals;

template<typename T>
struct vector : public ::std::vector< T >
{ using ::std::vector< T >::vector;
~vector(){ ::std::clog << "destroying vector\n"s; }};

struct myclass
{ static int counter;
int identity;

myclass() : identity{ ::myclass::counter++ }
{ ::std::clog << "created " <<
this->identity << " from scratch.\n"s; }

myclass( myclass const & other ):
identity{ ::myclass::counter++ }
{ ::std::clog << "created " << this->identity <<
" as a copy of " << other.identity << "\n"s; }

~myclass()
{ ::std::clog << "destroying " <<
this->identity << ".\n"s; }};

int ::myclass::counter = 0;

int main()
{ ::std::clog << "main 0.\n"s; vector< ::myclass > myvec;
myvec.reserve( 9 );
::std::clog << "main 1.\n"s; ::myclass c0;
::std::clog << "main 2.\n"s; ::myclass c1;
::std::clog << "main 3.\n"s; myvec.push_back( c0 );
::std::clog << "main 4.\n"s; myvec.push_back( c1 );
::std::clog << "main 5.\n"s; }

transcript
main 0.
main 1.
created 0 from scratch.
main 2.
created 1 from scratch.
main 3.
created 2 as a copy of 0
main 4.
created 3 as a copy of 1
main 5.
destroying 1.
destroying 0.
destroying vector
destroying 2.
destroying 3.

static void escape( void * p )
{ asm volatile( "" : : "g"(p) : "memory" ); }

struct object

{

void dump()

{ ::std::clog << " to " << static_cast< void * >( this )<< '\n'; }

object( const char * s ): name(s)

{ ::std::clog << "\n### constructing "; dump(); }

object( const object & a ): name(a.name)

{ ::std::clog << "\n### copying "; dump(); }

object( object && a ): name(std::move(a.name))

{ ::std::clog << "\n### moving "; dump(); }

object& operator +=( const object & other )

{ ::std::clog << "\n### appending " << name << ' ' <<
static_cast< void * >( this ) << " + " << other.name <<
' ' << static_cast< void const * >( &other )<< " to " <<
static_cast< void * >( this )<< '\n';

name += other.name;

return *this; }

::std::string name; };

Seiteninformationen und Impressum   |   Mitteilungsformular  |   "ram@zedat.fu-berlin.de" (ohne die Anführungszeichen) ist die Netzpostadresse von Stefan Ram.   |   Eine Verbindung zur Stefan-Ram-Startseite befindet sich oben auf dieser Seite hinter dem Text "Stefan Ram".)  |   Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram. Schlüsselwörter zu dieser Seite/relevant keywords describing this page: Stefan Ram Berlin slrprd slrprd stefanramberlin spellched stefanram721208 stefan_ram:721208 Spionageklassen in C++ Stefan Ram, Berlin, and, or, near, uni, online, slrprd, slrprdqxx, slrprddoc, slrprd721208, slrprddef721208, PbclevtugFgrsnaEnz Erklärung, Beschreibung, Info, Information, Hinweis,

Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram.
https://www.purl.org/stefan_ram/pub/spionagekonstruktoren_c++