Code: Select all
#include
#include
#include
struct point{
int X; int Y;
point(): X(0), Y(0) {
printf("D ");
}
point(int X, int Y): X(X), Y(Y) {
printf("P ");
}
point(const point& Point): X(Point.X), Y(Point.Y) {
printf("C ");
}
point(point&& Point): X(Point.X), Y(Point.Y) {
printf("M ");
}
point& operator=(const point& Point){
X=Point.X;
Y=Point.Y;
printf("CA ");
return *this;
}
point& operator=(point&& Point){
X=Point.X;
Y=Point.Y;
printf("MA ");
return *this;
}
~point(){
printf("DS ");
}
};
Ein anderer als EmplaBackpn , der den Destruktor des Elements an der Vorderseite des Arrays aufruft.
Code: Select all
template
struct array {
public:
element Buffer[10];
size_t Size = 0;
public:
template
void EmplaceBackMA(ctorargs&&... Args){
Buffer[Size] = element(std::forward(Args)...);
++Size;
}
template
void EmplaceBackPN(ctorargs&&... Args){
Buffer[Size].~element();
new(Buffer + Size) element(std::forward(Args)...);
++Size;
}
};
Code: Select all
int main(){
puts("Creating Particles");
array
Particles; //DDDDDDDDDD
puts("\nEmplaceBackMA");
Particles.EmplaceBackMA(4, 4); //P MA DS
puts("\nEmplaceBackPN");
Particles.EmplaceBackPN(4, 4); //DS P
puts("\nEndingProgram");
//Is Placement New Better?
}
< /code>
Ausgabe: < /p>
Creating Particles
D D D D D D D D D D
EmplaceBackMA
P MA DS
EmplaceBackPN
DS P
EndingProgram
DS DS DS DS DS DS DS DS DS DS