एक विश्लेषण की अनुमति दें।
#include <iostream> // not #include "iostream"
using namespace std;
class A
{
public:
void f() { cout<<"f()\n"; }
};
int main()
{
}
एक सामान्य सलाह के रूप में:
0) Prefer automatic variables
int a;
MyClass myInstance;
std::vector<int> myIntVector;
1) If you need data sharing on big objects down
the call hierarchy, prefer references:
void foo (std::vector<int> const &input) {...}
void bar () {
std::vector<int> something;
...
foo (something);
}
2) If you need data sharing up the call hierarchy, prefer smart-pointers
that automatically manage deletion and reference counting.
3) If you need an array, use std::vector<> instead in most cases.
std::vector<> is ought to be the one default container.
4) I've yet to find a good reason for blank pointers.
-> Hard to get right exception safe
class Foo {
Foo () : a(new int[512]), b(new int[512]) {}
~Foo() {
delete [] b;
delete [] a;
}
};
-> if the second new[] fails, Foo leaks memory, because the
destructor is never called. Avoid this easily by using
one of the standard containers, like std::vector, or
smart-pointers.
अंगूठे के एक नियम के रूप में: यदि आपको अपने दम पर स्मृति का प्रबंधन करने की आवश्यकता है, तो आम तौर पर एक सुपरियर प्रबंधक या पहले से ही उपलब्ध विकल्प है, जो कि आरएआईआई सिद्धांत का पालन करता है।