これと何が違うのか聞きたい
Tv *television = new Tv();
と
Tv television = Tv();
最初のものは、動的に割り当てられた を作成し、Tv
それを へのポインタにバインドしますTv
。オブジェクトの持続時間はTv
あなたの制御下にあります。オブジェクトを呼び出すことで、オブジェクトをいつ破棄するかを決定しますdelete
。
new Tv(); // creates dynamically allocated Tv and returns pointer to it
Tv* television; // creates a pointer to Tv that points to nothing useful
Tv* tv1 = new Tv(); // creates dynamicalls allocated Tv, pointer tv1 points to it.
delete tv1; // destroy the object and deallocate memory used by it.
2 つ目は、コピーの初期化によって自動的に割り当てられる を作成します。オブジェクトの持続時間は自動です。言語のルールに従って、たとえばスコープを終了すると、決定論的に破棄されます。Tv
Tv
{
// copy-initializaiton: RHS is value initialized temporary.
Tv television = Tv();
} // television is destroyed here.
「スコープの終了」は、オブジェクトを含むクラスのオブジェクトの寿命の終わりを指す場合もありTv
ます。
struct Foo {
Tv tv;
}
....
{
Foo f;
} // f is destroyed, and f.tv with it.