1) -.
class A {
public:
A() { a = 0; b = 0; }
A(int a, int b) { this->a = a; this->b = b; }
private:
int a;
int b;
};
, -, .
. , :
A a1;
A a2();
A* a3 = new A(1, 2);
A a4(1, 2);
2) :
A a(1, 2)
, , , , , . :
void fn() {
A a(1, 2);
...
...
}
when begging for an instance of function a, which will be automatically deleted upon exiting this function.
When:
A *a = new A(1,2)
variable a is declared, and it points to the newly created instance of A. You must manually delete the instance pointed to by the points with "delete a", but your instance can survive the area in which it is declared. For instance:
A* fn()
{
A *a = new A(1,2)
return a;
}
here the fn function returns an instance created inside its body, that is, the instance survives the scope.
source
share