C + + Why Ifstream do not use new, run out of delete

Source: Internet
Author: User

Just starting from C # to C + +, there are a lot of things that don't understand, like this code below:

#include <fstream>
#include <iostream>

int main () {
using namespace Std;
Ifstream file;
Basic_ifstream <wchar_t> Wfile;
char c;
Open and close with a basic_filebuf
File.rdbuf ()->open ("Basic_filebuf_close.txt", ios::in);
File >> C;
cout << c << Endl;
File.rdbuf ()->close ();

Open/close directly
File.Open ("Iotest.txt");
File >> C;
cout << c << Endl;
File.close ();

Open a file with a wide character name
Wfile.open (L "Iotest.txt");

Open and close a nonexistent with a basic_filebuf
File.rdbuf ()->open ("Ziotest.txt", ios::in);
cout << file.fail () << Endl;
File.rdbuf ()->close ();

Open/close directly
File.Open ("Ziotest.txt");
cout << file.fail () << Endl;
File.close ();
}

Below is my chat record with Daniel, the question untie. (There is a Daniel to help answer the question feeling is cool ah!) )

Skyline Wolf 2014/8/11 9:35:42Have 2 questions
Ifstream file;
File.Open ("Iotest.txt");
File.close ();
1. Can I call the file method without new?
2. When you're done, call File.close () and end up without delete.

Daniel 2014/8/11 9:33:55
Think about it yourself. What does new do?

Skyline Wolf 2014/8/11 9:35:42
New is used to create objects that allocate memory space on the heap. You mean this ifstream is a template, so you don't have to?
9:36:22
Daniel 2014/8/11 9:36:22
The understanding of new is right. This object is declared on the stack.

Skyline Wolf 2014/8/11 9:38:19
What you're saying is that if an object is just a reputation in front of you, then this famous variable is allocated on the stack. If you have a reputation, use new to allocate it on the heap. Is that so?
9:38:40
Daniel 2014/8/11 9:38:40
Yes

Daniel 2014/8/11 9:39:09
The object on the stack is not delete.

Skyline Wolf 2014/8/11 9:39:15
Well, the second question is clear. The first question is still a mystery,

Skyline Wolf 2014/8/11 9:39:37
Objects on the stack don't have to delete I know that.

Skyline Wolf 2014/8/11 9:40:16
Oh, isn't C + + using the default constructor?

Skyline Wolf 2014/8/11 9:40:38
The object was initialized before the call
9:41:46
Daniel 2014/8/11 9:41:46
Yes
You set a breakpoint in the constructor to see

Skyline Wolf 2014/8/11 9:43:01
3Q, clear. Well, this debugging method is very good.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.