c++ delete[]在vs2017中 运行时出错,但在code::blocks中不出错

嘿碳头 2018-03-08 11:47:17
c++ delete[]在vs2017中 运行时出错,但在code::blocks中不出错
并且在vs2017中,有时出错,有时不出错,求大神解答

#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;


int main() {
int k = 9;
int *C = new int(k);
int i = 0;
for (i = 0; i < k; ++i) {
C[i] = 0;
}

for ( i = 0; i < k; ++i) {
cout << i << endl;
cout << C[i] << " ";
}

delete[] C;

cout << "Hello....." << endl;
cin.get();
return 0;
}




...全文
213 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
狐狸小十 2018-03-08
  • 打赏
  • 举报
回复
new int[k] ()是初始化
mstlq 2018-03-08
  • 打赏
  • 举报
回复
int *C = new int(k); 改成
int *C = new int[k];
注意括号
系统信息程序的开发片段: // MainFrm.cpp : implementation of the CMainFrame class // #include "stdafx.h" #include "sysinfo.h" #include "MainFrm.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif CRgn rgn; ///////////////////////////////////////////////////////////////////////////// // CMainFrame int SCR_Width = 350,SCR_Height = 140; int Org_xPos = 20,Org_yPos = 0; IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd) BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) //{{AFX_MSG_MAP(CMainFrame) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code ! ON_WM_CREATE() //}}AFX_MSG_MAP END_MESSAGE_MAP() static UINT indicators[] = { ID_SEPARATOR, // status line indicator ID_INDICATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL, }; ///////////////////////////////////////////////////////////////////////////// // CMainFrame construction/destruction CMainFrame::CMainFrame() { // TODO: add member initialization code here } CMainFrame::~CMainFrame() { } int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; // TODO: Delete these three lines if you don't want the toolbar to // be dockable return 0; } BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if( !CFrameWnd::PreCreateWindow(cs) ) return FALSE; cs.dwExStyle = WS_EX_TOOLWINDOW | WS_EX_TOPMOST;//|WS_EX_TOPMOST; cs.style=WS_POPUP; cs.style &= ~WS_CAPTION; cs.x = Org_xPos; cs.y = Org_yPos; cs.cx = SCR_Width; cs.cy = SCR_Height; cs.hMenu = NULL; return TRUE; } ///////////////////////////////////////////////////////////////////////////// // CMainFrame diagnostics #ifdef _DEBUG void CMainFrame::AssertValid() const { CFrameWnd::AssertValid(); } void CMainFrame::Dump(CDumpContext& dc) const { CFrameWnd::Dump(dc); } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMainFrame message handlers
1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1 A Brief Introduction to C++ . . . . . . . . . . . . . . . . . . . 1.1.1 C++ is “Object-Oriented” . . . . . . . . . . . . . . . . 1.1.2 Why You Should Write Scientific Programs in C++ . . 1.1.3 Why You Should Not Write Scientific Programs in C++ 1.1.4 Scope of This Book . . . . . . . . . . . . . . . . . . . 1.2 A First C++ Program . . . . . . . . . . . . . . . . . . . . . . 1.3 Compiling a C++ Program . . . . . . . . . . . . . . . . . . . . 1.3.1 Integrated Development Environments . . . . . . . . . 1.3.2 Compiling at the Command Line . . . . . . . . . . . . 1.3.3 Compiler Flags . . . . . . . . . . . . . . . . . . . . . . 1.4 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.4.1 Basic Numerical Variables . . . . . . . . . . . . . . . . 1.4.2 Other Numerical Variables . . . . . . . . . . . . . . . 1.4.3 Mathematical Operations on Numerical Variables . . . 1.4.4 Division of Integers . . . . . . . . . . . . . . . . . . . 1.4.5 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . 1.4.6 ASCII Characters . . . . . . . . . . . . . . . . . . . . 1.4.7 Boolean Variables . . . . . . . . . . . . . . . . . . . . 1.4.8 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . 1.5 Simple Input and Output . . . . . . . . . . . . . . . . . . . . . 1.5.1 Basic Console Output . . . . . . . . . . . . . . . . . . 1.5.2 Keyboard Input . . . . . . . . . . . . . . . . . . . . . 1.6 The assert Statement . . . . . . . . . . . . . . . . . . . . . 1.7 Tips: Debugging Code . . . . . . . . . . . . . . . . . . . . . . 1.8 Exercises . . . . . . . . . . . . . . . . . . . . . . 2 Flow of Control . . . . . . . . . . . . . . . . . . . . . 2.1 The if Statement . . . . . . . . . . . . . . . . . 2.1.1 A Single if Statement . . . . . . . . . . 2.1.2 Example: Code for a Single if Statement 2.1.3 if–else Statements . . . . . . . . . . . 2.1.4 Multiple if Statements . . . . . . . . . . 2.1.5 Nested if Statements . . . . . . 2.1.6 Boolean Variables . . . . . . . . . . . . . . . . . . . . 2.2 Logical and Relational Operators . . . . . . . . . . . . . . . . 2.3 The while Statement . . . . . . . . . . . . . . . . . . . . . . 2.4 Loops Using the for Statement . . . . . . . . . . . . . . . . . 2.4.1 Example: Calculating the Scalar Product of Two Vectors 2.5 The switch Statement . . . . . . . . . . . . . . . . . . . . . 2.6 Tips: Loops and Branches . . . . . . . . . . . . . . . . . . . . 2.6.1 Tip 1: A Common Novice Coding Error . . . . . . . . 2.6.2 Tip 2: Counting from Zero . . . . . . . . . . . . . . . . 2.6.3 Tip 3: Equality Versus Assignment . . . . . . . . . . . 2.6.4 Tip 4: Never Ending while Loops . . . . . . . . . . . 2.6.5 Tip 5: Comparing Two Floating Point Numbers . . . . 2.7 Exercises . . . . . . . . . . . . . . . . . . . . 3 File Input and Output . . . . . . . . . . . . . 3.1 Redirecting Console Output to File . . . . 3.2 Writing to File . . . . . . . . . . . . . . . 3.2.1 Setting the Precision of the Output 3.3 Reading from File . . . . . . . . . . . . . 3.4 Reading from the Command Line . . . . . 3.5 Tips: Controlling Output Format . . . . . 3.6 Exercises . . . . . . . . . . 4 Pointers . . . . . . . . . . . . . . . . . . . . . 4.1 Pointers and the Computer’s Memory . . . 4.1.1 Addresses . . . . . . . . . . . . . 4.1.2 Pointer Variables . . . . . . . . . . 4.1.3 Example Use of Pointers . . . . . 4.1.4 Warnings on the Use of Pointers . . 4.2 Dynamic Allocation of Memory for Arrays 4.2.1 Vectors . . . . . . . . . . . . . . . 4.2.2 Matrices . . . . . . . . . . . . . . 4.2.3 Irregularly Sized Matrices . . . . . 4.3 Tips: Pointers . . . . . . . . . . . . . . . 4.3.1 Tip 1: Pointer Aliasing . . . . . . . 4.3.2 Tip 2: Safe Dynamic Allocation . . 4.3.3 Tip 3: Every new Has a delete . 4.4 Exercises . . . . . . . . . . . 5 Blocks, Functions and Reference Variables . . . . . . . 5.1 Blocks . . . . . . . . . . . . . . . . . . . . . . . . 5.2 Functions . . . . . . . . . . . . . . . . . . . . . . . 5.2.1 Simple Functions . . . . . . . . . . . . . . 5.2.2 Returning Pointer Variables from a Function 5.2.3 Use of Pointers as Function Arguments . . . 5.2.4 Sending Arrays to Functions . . Example: A Function to Calculate the Scalar Product of Two Vectors . . . . . . . . . . . . . . . . . . . . . . . Reference Variables . . . . . . . . . . . . . . . . . . . . . . . Default Values for Function Arguments . . . . . . . . . . . . . Function Overloading . . . . . . . . . . . . . . . . . . . . . . Declaring Functions Without Prototypes . . . . . . . . . . . . Function Pointers . . . . . . . . . . . . . . . . . . . . . . . . Recursive Functions . . . . . . . . . . . . . . . . . . . . . . . Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Tips: Code Documentation . . . . . . . . . . . . . . . . . . . Exercises . . . . . . . . . . . . . . . . . . . . . . An Introduction to Classes . . . . . . . . . . . . . . . . . . . . . . . 6.1 The Raison d’Être for Classes . . . . . . . . . . . . . . . . . . . 6.1.1 Problems That May Arise When Using Modules . . . . . 6.1.2 Abstraction, Encapsulation and Modularity Properties of Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.2 A First Example Simple Class: A Class of Books . . . . . . . . . 6.2.1 Basic Features of Classes . . . . . . . . . . . . . . . . . 6.2.2 Header Files . . . . . . . . . . . . . . . . . . . . . . . . 6.2.3 Setting and Accessing Variables . . . . . . . . . . . . . . 6.2.4 Compiling Multiple Files . . . . . . . . . . . . . . . . . 6.2.5 Access Privileges . . . . . . . . . . . . . . . . . . . . . 6.2.6 Including Function Implementations in Header Files . . . 6.2.7 Constructors and Destructors . . . . . . . . . . . . . . . 6.2.8 Pointers to Classes . . . . . . . . . . . . . . . . . . . . . 6.3 The friend Keyword . . . . . . . . . . . . . . . . . . . . . . 6.4 A Second Example Class: A Class of Complex Numbers . . . . . 6.4.1 Operator Overloading . . . . . . . . . . . . . . . . . . . 6.4.2 The Class of Complex Numbers . . . . . . . . . . . . . . 6.5 Some Additional Remarks on Operator Overloading . . . . . . . 6.6 Tips: Coding to a Standard . . . . . . . . . . . . . . . . . . . . . 6.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Inheritance and Derived Classes . . . . . . . . . . . . . . . . . 7.1 Inheritance, Extensibility and Polymorphism . . . . . . . . . 7.2 Example: A Class of E-books Derived from a Class of Books 7.3 Access Privileges for Derived Classes . . . . . . . . . . . . . 7.4 Classes Derived from Derived Classes . . . . . . . . . . . . 7.5 Run-Time Polymorphism . . . . . . . . . . . . . . . . . . . 7.6 The Abstract Class Pattern . . . . . . . . . . . . . . . . . . . 7.7 Tips: Using a Debugger . . . . . . . . . . . . . . . . . . . . 7.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 8.1 Templates to Control Dimensions and Verify Sizes . . . . . . . . 8.2 Templates for Polymorphism . . . . . . . . . . 8.3 A Brief Survey of the Standard Template Library 8.3.1 Vectors . . . . . . . . . . . . . . . . . . 8.3.2 Sets . . . . . . . . . . . . . . . . . . . . 8.4 Tips: Template Compilation . . . . . . . . . . . 8.5 Exercises . . . . . . . . . . . . . . . . . . . . . Errors and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . 9.1 Preconditions . . . . . . . . . . . . . . . . . . . . . . . . . . 9.1.1 Example: Two Implementations of a Graphics Function 9.2 Three Levels of Errors . . . . . . . . . . . . . . . . . . . . . . 9.3 Introducing the Exception . . . . . . . . . . . . . . . . . . . . 9.4 Using Exceptions . . . . . . . . . . . . . . . . . . . . . . . . 9.5 Tips: Test-Driven Development . . . . . . . . . . . . . . . . . 9.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Developing Classes for Linear Algebra Calculations 10.1 Requirements of the Linear Algebra Classes . . . 10.2 Constructors and Destructors . . . . . . . . . . . 10.2.1 The Default Constructor . . . . . . . . . . 10.2.2 The Copy Constructor . . . . . . . . . . . 10.2.3 A Specialised Constructor . . . . . . . . . 10.2.4 Destructor . . . . . . . . . . . . . . . . . 10.3 Accessing Private Class Members . . . . . . . . . 10.3.1 Accessing the Size of a Vector . . . . . . . 10.3.2 Overloading the Square Bracket Operator . 10.3.3 Read-Only Access to Vector Entries . . . . 10.3.4 Overloading the Round Bracket Operator . 10.4 Operator Overloading for Vector Operations . . . 10.4.1 The Assignment Operator . . . . . . . . . 10.4.2 Unary Operators . . . . . . . . . . . . . . 10.4.3 Binary Operators . . . . . . . . . . . . . . 10.5 Functions . . . . . . . . . . . . . . . . . . . . . . 10.5.1 Members Versus Friends . . . . . . . . . . 10.6 Tips: Memory Debugging Tools . . . . . . . . . . 10.7 Exercises . . . . . . . . . . . . . . . . 11 An Introduction to Parallel Programming Using MPI . 11.1 Distributed Memory Architectures . . . . . . . . . 11.2 Installing MPI . . . . . . . . . . . . . . . . . . . . 11.3 A First Program Using MPI . . . . . . . . . . . . . 11.3.1 Essential MPI Functions . . . . . . . . . . . 11.3.2 Compiling and Running MPI Code . . . . . 11.4 Basic MPI Communication . . . . . . . . . . . . . 11.4.1 Point-to-Point Communication . . . . . . . 11.4.2 Collective Communication . . . . 11.5 Example MPI Applications . . . . . . . . . . . 11.5.1 Summation of Series . . . . . . . . . . . 11.5.2 Parallel Linear Algebra . . . . . . . . . 11.6 Tips: Debugging a Parallel Program . . . . . . . 11.6.1 Tip 1: Make an Abstract Program . . . . 11.6.2 Tip 2: Datatype Mismatch . . . . . . . . 11.6.3 Tip 3: Intermittent Deadlock . . . . . . . 11.6.4 Tip 4: Almost Collective Communication 11.7 Exercises . . . . . . . . . . . . . . . 12 Designing Object-Oriented Numerical Libraries . . . . . . . . . . . 12.1 Developing the Library for Ordinary Differential Equations . . . . 12.1.1 Model Problems . . . . . . . . . . . . . . . . . . . . . . . 12.1.2 Finite Difference Approximation to Derivatives . . . . . . 12.1.3 Application of Finite Difference Methods to Boundary Value Problems . . . . . . . . . . . . . . . . . . . . . . . 12.1.4 Concluding Remarks on Boundary Value Problems in One Dimension . . . . . . . . . . . . . . . . . . . . . . . . . . 12.2 Designing a Library for Solving Boundary Value Problems . . . . 12.2.1 The Class SecondOrderOde . . . . . . . . . . . . . . . 12.2.2 The Class BoundaryConditions . . . . . . . . . . . . 12.2.3 The Class FiniteDifferenceGrid . . . . . . . . . . 12.2.4 The Class BvpOde . . . . . . . . . . . . . . . . . . . . . 12.2.5 Using the Class BvpOde . . . . . . . . . . . . . . . . . . 12.3 Extending the Library to Two Dimensions . . . . . . . . . . . . . 12.3.1 Model Problem for Two Dimensions . . . . . . . . . . . . 12.3.2 Finite Difference Methods for Boundary Value Problems in Two Dimensions . . . . . . . . . . . . . . . . . . . . . 12.3.3 Setting Up the Linear System for the Model Problem . . . 12.3.4 Developing the Classes Required . . . . . . . . . . . . . . 12.4 Tips: Using Well-Written Libraries . . . . . . . . . . . . . . . . . 12.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . Appendix A Linear Algebra . . . . . . . . . . . . . . . . . A.1 Vectors and Matrices . . . . . . . . . . . . . . . . . A.1.1 Operations Between Vectors and Matrices . A.1.2 The Scalar Product of Two Vectors . . . . . A.1.3 The Determinant and the Inverse of a Matrix A.1.4 Eigenvalues and Eigenvectors of a Matrix . . A.1.5 Vector and Matrix Norms . . . . . . . . . . A.2 Systems of Linear Equations . . . . . . . . . . . . A.2.1 Gaussian Elimination . . . . . . . . . . . . A.2.2 The Thomas Algorithm . . . . . . . . . . . A.2.3 The Conjugate Gradient Method . . . . . . 213 Appendix B Other Programming Constructs You Might Meet . B.1 C Style Output . . . . . . . . . . . . . . . . . . . . . . . B.2 C Style Dynamic Memory Allocation . . . . . . . . . . . B.3 Ternary ?: Operator . . . . . . . . . . . . . . . . . . . . B.4 Using Namespace . . . . . . . . . . . . . . . . . . . . . B.5 Structures . . . . . . . . . . . . . . . . . . . . . . . . . B.6 Multiple Inheritance . . . . . . . . . . . . . . . . . . . . B.7 Class Initialisers . . . . . . . . . . . . . . . . Appendix C Solutions to Exercises . . . . . . . . . . . . . . . . . . . . . 231 C.1 Matrix and Linear System Classes . . . . . . . . . . . . . . . . . . 231 C.2 ODE Solver Library . . . . . . . . . . . . . . . . . . . . . . . . . 240 Further Reading . . . . . . . . . . . . . . . . . . . Mathematical Methods and Linear Algebra C++ Programming . . . . . . . . . . . . . The Message-Passing Interface (MPI) . . .
1 Writing an ANSI C++ Program 1 1.1 Getting Ready to Program....... 2 1.1 A First Program......... 3 1.2 Problem Solving: Recipes....... . 7 1.2.1 Algorithms—Being Precise...... . . 8 1.3 Implementing Our Algorithm in C++.... . . . 10 1.4 Software Engineering: Style...... . . 12 1.5 Common Programming Errors...... 13 1.6 Writing and Running a C++ Program.... . . . 14 1.6.1 Interrupting a Program...... 16 1.6.2 Typing an End-of-File Signal..... 16 1.7 Dr. P’s Prescriptions....... . . . 16 1.8 C++ Compared with Java....... 17 Summary.......... 20 Review Questions........ . 21 Exercises.......... 22 2 Native Types and Statements 25 2.1 Program Elements........ . 26 2.1.1 Comments........ . 26 2.1.2 Keywords........ . . 27 2.1.3 Identifiers........ . . 27 2.1.4 Literals......... 29 2.1.5 Operators and Punctuators..... . 31 2.2 Input/Output......... . 31 2.3 Program Structure........ . 34 2.3.1 Redirection........ . 36 2.4 Simple Types......... . 37 2.4.1 Initialization........ 39 2.5 The Traditional Conversions...... . 40 2.6 Enumeration Types........ 43 2.6.1 typedef Declarations...... . 44 2.7 Expressions.......... . 44 2.7.1 Precedence and Associativity of Operators.. . 45 2.7.2 Relational, Equality, and Logical Operators.. . 47 2.8 Statements......... . . 50 2.8.1 Assignment and Expressions..... . . . 51 2.8.2 The Compound Statement...... . 52 2.8.3 The if and if-else Statements.... . 52 2.8.4 The while Statement...... . 55 2.8.5 The for Statement...... . . . 56 2.8.6 The do Statement....... 57 2.8.7 The break and continue Statements... . 58 2.8.8 The switch Statement...... 59 2.8.9 The goto Statement...... . . 62 2.9 Software Engineering: Debugging..... . 62 2.10 Dr. P’s Prescriptions....... . . . 65 2.11 C++ Compared with Java....... 67 Summary.......... 69 Review Questions........ . 70 Exercises.......... 71 3 Functions, Pointers, and Arrays 75 3.1 Functions......... . . . 75 3.2 Function Invocation........ 76 3.3 Function Definition........ 78 3.4 The return Statement........ . 79 3.5 Function Prototypes........ . . . 80 3.6 Call-By-Value......... . 81 3.7 Recursion......... . . . 83 3.8 Default Arguments........ 84 3.9 Functions as Arguments....... 86 3.10 Overloading Functions........ . 88 3.11 Inlining.......... . 89 3.11.1Software Engineering: Avoiding Macros... 89 3.12 Scope and Storage Class....... 90 3.12.1The Storage Class auto...... . . . 92 3.12.2The Storage Class extern..... . . 92 3.12.3The Storage Class register..... 93 3.12.4The Storage Class static..... . . 94 3.12.5Header Files and Linkage Mysteries... . . . 95 3.13 Namespaces......... . 98 3.14 Pointer Types......... 99 3.14.1Addressing and Dereferencing.... . 100 3.14.2Pointer-Based Call-By-Reference..... 100 3.15 Reference Declarations....... 102 3.16 The Uses of void......... 104 3.17 Arrays.......... . 105 3.17.1Subscripting....... . . . 106 3.17.2 Initialization....... . . . 106 3.18 Arrays and Pointers....... . . . 106 3.19 Passing Arrays to Functions....... 107 3.20 Problem Solving: Random Numbers.... . . . 108 3.21 Software Engineering: Structured Programming.. . . 111 3.22 Core Language ADT: char* String..... 114 3.23 Multidimensional Arrays...... . . . 117 3.24 Operators new and delete...... . 120 3.24.1 Vector Instead of Array..... . . . 123 3.24.2 String Instead of char*..... . . . 124 3.25 Software Engineering: Program Correctness.... 124 3.26 Dr. P’s Prescriptions....... . . 127 3.27 C++ Compared with Java...... . . . 128 Summary......... . . . 130 Review Questions........ 132 Exercises......... . . . 133 4 Classes and Abstract Data Types 139 4.1 The Aggregate Type class and struct.... . . . 140 4.2 Member Selection Operator...... . 141 4.3 Member Functions........ . . . 143 4.4 Access: Private and Public...... . . 146 4.5 Classes.......... 147 4.6 Class Scope......... . 150 4.6.1 Scope Resolution Operator..... 150 4.6.2 Nested Classes....... . 152 4.7 An Example: Flushing....... . 153 4.8 The this Pointer........ . 158 4.9 static Members......... 159 4.10 const Members......... . 161 4.10.1Mutable Members...... . . . 163 4.11 A Container Class Example: ch_stack..... 164 4.12 Software Engineering: Class Design.... . . . 166 4.12.1Trade-Offs in Design...... . 168 4.12.2Unified Modeling Language (UML) and Design. . . 169 4.13 Dr. P’s Prescriptions....... . . 170 4.14 C++ Compared with Java...... . . . 171 4.15 Advanced Topics........ . 172 4.15.1Pointer to Class Member..... . . 172 4.15.2Unions........ . . . 174 4.15.3Bit Fields........ . . 175 Summary......... . . . 177 Review Questions........ 178 Exercises......... . . . 179 5 Ctors, Dtors, Conversions, and Operator Overloading 183 5.1 Classes with Constructors...... . . 184 5.1.1 The Default Constructor..... . . 186 5.1.2 Constructor Initializer...... 187 5.1.3 Constructors as Conversions..... . . 187 5.1.4 Improving the point Class..... 189 5.1.5 Constructing a Stack...... . 190 5.1.6 The Copy Constructor...... 193 5.2 Classes with Destructors...... . . . 195 5.3 Members That Are Class Types..... . . 195 5.4 Example: A Singly Linked List..... . . . 196 5.5 Strings Using Reference Semantics.... . . . 201 5.6 Constructor Issues and Mysteries..... 204 5.6.1 Destructor Details...... . . . 205 5.6.2 Constructor Pragmatics...... . . 206 5.7 Polymorphism Using Function Overloading... . 206 5.8 ADT Conversions......... 207 5.9 Overloading and Signature Matching.... . . 208 5.10 Friend Functions........ . 211 5.11 Overloading Operators....... 213 5.12 Unary Operator Overloading...... 214 5.13 Binary Operator Overloading...... 217 5.14 Overloading the Assignment Operator.... . 219 5.15 Overloading the Subscript Operator..... . . 220 5.16 Overloading Operator () for Indexing.... 221 5.17 Overloading << and >>....... 222 5.18 Overloading ->........ . . 223 5.19 Overloading new and delete...... . . . 224 5.20 More Signature Matching....... . . 227 5.21 Software Engineering: When to Use Overloading.. . 228 5.22 Dr. P’s Prescriptions....... . . 229 5.23 C++ Compared with Java...... . . . 231 Summary......... . . . 235 Review Questions........ 236 Exercises......... . . . 237 6 Templates and Generic Programming 243 6.1 Template Class stack....... . 246 6.2 Function Templates....... . . . 248 6.2.1 Signature Matching and Overloading.... 250 6.2.2 How to Write a Simple Function: square().. . . . 252 6.3 Generic Code Development: Quicksort..... 253 6.3.1 Converting to a Generic quicksort()... . . . 256 6.4 Class Templates........ . 260 6.4.1 Friends........ . . . 260 6.4.2 Static Members....... . 260 6.4.3 Class Template Arguments..... 261 6.4.4 Default Template Arguments..... . . 261 6.4.5 Member Templates...... . . 262 6.5 Parameterizing the Class vector...... 262 6.6 Using STL: string, vector, and complex... . . 265 6.6.1 string and basic_string<>.... . . . 265 6.6.2 vector<> in STL....... 267 6.6.3 Using complex<>...... . . . 267 6.6.4 limits and Other Useful Templates.... . . 268 6.7 Software Engineering: Reuse and Generics.... . 269 6.7.1 Debugging Template Code..... 269 6.7.2 Special Considerations..... . . . 270 6.7.3 Using typename....... 271 6.8 Dr. P’s Prescriptions....... . . 272 6.9 C++ Compared with Java...... . . . 272 Summary......... . . . 276 Review Questions........ 277 Exercises......... . . . 278 7 Standard Template Library 280 7.1 A Simple STL Example....... . 280 7.2 Containers.......... . 283 7.2.1 Sequence Containers....... 285 7.2.2 Associative Containers..... . . . 288 7.2.3 Container Adaptors...... . . 293 7.3 Iterators......... . . . 296 7.3.1 Iterators for istream and ostream... . . . 297 7.3.2 Iterator Adaptors....... . . . 300 7.4 Algorithms......... . 302 7.4.1 Sorting Algorithms...... . . 302 7.4.2 Nonmutating Sequence Algorithms... . . 305 7.4.3 Mutating Sequence Algorithms.... . 307 7.4.4 Numerical Algorithms...... 310 7.5 Numerical Integration Made Easy..... . 311 7.6 STL: Function Objects....... . 315 7.6.1 Building a Function Object..... 317 7.6.2 Function Adaptors....... . . 318 7.7 Allocators......... . . 320 7.8 Software Engineering: STL Use...... . . 320 7.8.1 Syntax Bugs....... . . . 321 7.9 Dr. P’s Prescriptions....... . . 322 7.10 C++ Compared with Java...... . . . 323 Summary......... . . . 324 Review Questions........ 325 Exercises......... . . . 326 8 Inheritance and OOP 328 8.1 A Derived Class........ . . 329 8.1.1 More Unified Modeling Language (UML)... . . 333 8.2 A Student ISA Person....... . . 334 8.3 Virtual Functions: Dynamic Determination... . . 337 8.3.1 Overloading and Overriding Confusion.. . . . 340 8.3.2 A Canonical Example: Class shape... . . 342 8.4 Abstract Base Classes....... . 343 8.5 Templates and Inheritance....... . 350 8.6 Multiple Inheritance........ . . 351 8.7 RTTI and Other Fine Points...... . 353 8.7.1 Finer Points........ 354 8.8 Software Engineering: Inheritance and Design.. . . . 355 8.8.1 Subtyping Form........ 356 8.8.2 Code Reuse........ . . . 357 8.9 Dr. P’s Prescriptions....... . . 358 8.10 C++ Compared with Java...... . . . 358 Summary......... . . . 361 Review Questions........ 362 Exercises......... . . . 363 9 Input/Output 366 9.1 The Output Class ostream....... . 366 9.2 Formatted Output and iomanip..... . . 367 9.3 User-Defined Types: Output....... 372 9.4 The Input Class istream....... . . . 374 9.5 Files.......... . . 375 9.6 Using Strings as Streams...... . . . 379 9.7 The Functions and Macros in ctype.... . . . 380 9.8 Using Stream States........ . . 380 9.9 Mixing I/O Libraries........ . . 383 9.10 Software Engineering: I/O...... . . 384 9.11 Dr. P’s Prescriptions....... . . 386 9.12 C++ Compared with Java...... . . . 386 Summary......... . . . 389 Review Questions........ 390 Exercises......... . . . 391 10Exceptions and Program Correctness 394 10.1 Using the assert Library....... . . . 394 10.2 C++ Exceptions........ . . 397 10.3 Throwing Exceptions....... . . 398 10.3.1Rethrown Exceptions....... 400 10.3.2Exception Expressions..... . . . 401 10.4 try Blocks.......... . 404 10.5 Handlers......... . . . 405 10.6 Converting Assertions to Exceptions.... . . 405 10.7 Exception Specification....... 408 10.8 terminate() and unexpected()..... 409 10.9 Standard Exceptions and Their Uses.... . . 409 10.10 Software Engineering: Exception Objects... . . . 411 10.11 Dr. P’s Prescriptions....... . . 413 10.12 C++ Compared with Java...... . . . 414 Summary......... . . . 417 Review Questions........ 418 Exercises......... . . . 419 11OOP Using C++ 421 11.1 OOP Language Requirements...... . . . 422 11.1.1ADTs: Encapsulation and Data Hiding... 423 11.1.2Reuse and Inheritance...... 423 11.1.3Polymorphism........ . 424 11.2 OOP: The Dominant Programming Methodology.. . 425 11.3 Designing with OOP in Mind...... 432 11.4 Class-Responsibility-Collaborator...... 434 11.4.1CRC Cards......... 435 11.5 Design Patterns........ . . 436 11.6 A Further Assessment of C++..... . . . 437 11.6.1Why C++ Is Better Than Java.... . . . 438 11.6.2A Short Rebuttal....... 439 11.7 Software Engineering: Last Thoughts.... . . 439 11.8 Dr. P’s Prescriptions....... . . 440 11.9 C++ Compared with Java...... . . . 441 Summary......... . . . 447 Review Questions........ 448 Exercises......... . . . 449 AASCII Character Codes 451 B Operator Precedence and Associativity 453 C String Library 455 C.1 Constructors......... 456 C.2 Member Functions........ . . . 457 C.3 Global Operators........ . 460 DThe tio Library 462 D.1 Console.......... . . . 462 D.2 FormattedWriter........ . 463 D.3 PrintFileWriter........ . . . 472 D.4 ReadException......... . . 472 D.5 ReadInput......... . . 473 Index 482
Program: DelforExp, Delphi Formatter Version: 2.4.1 for Delphi 2-7 Note DelForEx7.dll might not work, please notify me if you have problems Category: Programmers tool Description: DelforExp is a customizable source code formatter. It can improve the indentation, spacing, capitalization and the use of blank lines of Delphi 5.0 source code. In the default settings, the style of the Borland source code is followed closely. It is an expert that is integrated in the Delphi IDE. Status: The program is released as FREEWARE to improve the productivity of Delphi. You may distribute the files freely as long as you don't make money by it. The use of the program is at own risk. (see also license.txt) The source code is partly included, to make it possible to customize the user interface and upgrade to future Delphi versions. Only the engine of the formatter is included as compiled dll file. Files: DelForEx5.dll Delphi 5 version DelForDll.dll The engine of the parser/formatter DelFor.hlp On-line help SetupEx.dpr Install/deinstall Readme.txt this file License.txt license notes Preview.pas Example pascal file for preview window Problems.pas Nonsense Pascal with solved (and some known) problems in DelFor Source.zip Zipped file with source of the interface parts of DelForEx2.dpr (Delphi 2 version) DelForEx3.dpr (Delphi 3 version) DelForEx4.dpr (Delphi 4 version) DelForEx5.dpr (Delphi 5 version) DelForEx6.dpr (Delphi 6 version) DelForEx7.dpr (Delphi 7 version) DelFor.dpr (Stand-alone version (needs the freeware opensource component mwEdit) SetupEx.dpr (the setup utility) Install: Copy all files to any directory. [If necessary unzip source.zip and build the required version in Delphi.] Close Delphi Run the program SetupEx. Restart Delphi. The second item of the "Tools" menu should be "Source Formatter..." To install/remove manually: set or delete the following registry keys D7: HKEY_CURRENT_USER\Software\Borland\Delphi\7.0\Experts\DelForEx6=[Path]\DelForEx7.dll D6: HKEY_CURRENT_USER\Software\Borland\Delphi\6.0\Experts\DelForEx6=[Path]\DelForEx6.dll D5: HKEY_CURRENT_USER\Software\Borland\Delphi\5.0\Experts\DelForEx5=[Path]\DelForEx5.dll D4: HKEY_CURRENT_USER\Software\Borland\Delphi\4.0\Experts\DelForEx4=[Path]\DelForEx4.dll D3: HKEY_CURRENT_USER\Software\Borland\Delphi\3.0\Experts\DelForExp=[Path]\DelForEx3.dll D2: HKEY_CURRENT_USER\Software\Borland\Delphi\2.0\Experts\DelForEx=[Path]\DelForEx2.dll Compile instructions The version of the DFM files is Delphi 5. Therefore you will get errormessages in earlier versions. Please open all dialog boxes and ignore all errors before compiling DelForEx2.dpr and DelForEx3.dpr. DeInstall: Rerun the program SetupEx. Delete all files. Contact: Egbert van Nes http://www.dow.wau.nl/aew/DelForExp.html (NOTE: HAS CHANGED) egbert.vannes@wur.nl Wageningen University The Netherlands FAQ Q: Could you please add xxx option in your formatter? A: There are many wishes like yours. Currently, I only add those that - I like a LOT - are not too difficult to add My time is limited, I don't make money with this tool and my priority is to keep the formatter stable and to fix bugs. Q: Please, give me the source code! A: Pardon? Q: Could I please buy the code? A: No, I am sorry it is not for sale. Q: Where can I find your free formatter? A: The latest version is at the official DelForExp Homepage. http://www.slm.wau.nl/wkao/DelForExp.html There are also copies at: Torry pages,The Delphi Super Page, DelphiSource.COM and The Delphi Pages. On these pages you can find many other useful components too. Q: Have you also created a similar formatter for C++, Java or Visual Basic? A: No, I am sorry, I have only created one for Delphi. Being an addicted Delphi user, I have no plans to create one for any other computer language. Q: I want to develop an expert. I'm completely new to the subject. Where can I find good resources about writing experts? A: I have used the following resources: (1) Templest software has gathered some information at http://www.tempest-sw.com/opentools/. (2) At the Torry pages there are many freeware components (with source) of Martin Waldenburg that are very usefull (e.g., TOTA, MPasLex, MWLexGen, IDEStrea (used for DelForExp) , MIDETre4) (3) There is a newsgroup about the Open Tools API: borland.public.delphi.opentoolsapi Known problems: (1) Compiler {$IFDEF} + {$ELSE} directives may be nested to 3 levels and break into blocks of code. After the third nested level the right indentation is not guaranteed. (2) After some options (align, adding line breaks) the indentation might be not correct. Rerunning DelFor can fix the problems. (3) It is tried to indent function directives after function declarations. In some cases this does not happen. (4) After formatting the positions of breakpoints and bookmarks are not changed (all bookmarks are moved and stacked at the end of the file) breakpoints are removed) If someone knows how to get and set the locations of these points I would be happy to hear from you.
来源: http://www.codeguru.com/Cpp/misc/misc/article.php/c321/ Environment: The demo was built with Microsoft Developer Studio 97 (Visual C++ 5.0) and has been tested with Windows 95, Windows 98 and Windows NT 4.0. Applications sometimes need to create a child process whose output may, by default, be written to a console window. Running a batch file is a good example of this. In many cases the console window goes away as soon as the child process exits. At times it would be useful if there was a way to redirect the output of the child process to a CEdit control. A good example of the desired behavior is the output generated by tools executed from the Tools menu of DevStudio. A tool's output is written to the Output window, and the user can copy and clear text from this window. Moreover, the output from the tool is displayed as it is being written, instead of all at once when the tool exits. It would be nice if Microsoft had provided an example of how to do this, but such is not the case. The only example that I could find in the Online Documentation is the one titled "Creating a Child Process with Redirected Input and Output". This example shows how to create a child process and redirect its standard output to an anonymous pipe. The parent process writes and then reads to the pipe, while the child process reads and then writes to the pipe, and that's it. This example falls short of demonstrating how to use a loop to read data from a pipe whenever data is available, and to exit the loop when the child process has exited. I spent a good amount of time searching newsgroups and web sites for examples but never found any that worked to my liking. Then I came across a post to the microsoft.public.vc.mfc newsgroup, submitted by Dima Shamroni, that provides the essential code. I have used this code as a basis for the following class, CRedirect. Post a comment Email Article Print Article Share Articles Digg del.icio.us Newsvine Facebook Google LinkedIn MySpace Reddit Slashdot StumbleUpon Technorati Twitter Windows Live YahooBuzz FriendFeed The class CRedirect creates a child process and redirects its standard output and standard error to a CEdit control. Both the command line for the process and the CEdit control are specified by the caller. Using the class is simple, as shown in the following: CRedirect Redirect("C:\\Temp\\sample.bat", m_Edit); Redirect.Run(); A fancier way of using this class is to dynamically allocate an object of CRedirect and provide a Stop button that allows you to terminate the child process, as shown in the following (an abbreviated version of what's used in the demo): CRedirectDemoDlg::OnButtonRun() { m_pRedirect = new CRedirect("C:\\Temp\\sample.bat", m_Edit); m_pRedirect->Run(); delete m_pRedirect; m_pRedirect = 0; } CRedirectDemoDlg::OnButtonStop() { if ( m_pRedirect ) { m_pRedirect->Stop(); } The CRedirect::Run() method blocks the caller. However, windows still receive messages because within the CRedirect::Run() method there is a loop that both reads data from a pipe and calls the method listed below to process window messages. CRedirect::PeekAndPump() { MSG Msg; while ( ::PeekMessage(&Msg, NULL, 0, 0, PM_NOREMOVE) ) { (void)AfxGetApp()->PumpMessage(); } }

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧