// Example 2: Using an auto_ptr // void g() ...{ T* pt1 =new T; // right now, we own the allocated object // pass ownership to an auto_ptr auto_ptr<T> pt2( pt1 ); // use the auto_ptr the same way // we'd use a simple pointer *pt2 =12; // same as "*pt1 = 12;" pt2->SomeFunc(); // same as "pt1->SomeFunc();" // use get() to see the pointer value assert( pt1 == pt2.get() ); // use release() to take back ownership T* pt3 = pt2.release(); // delete the object ourselves, since now // no auto_ptr owns it any more delete pt3;
}// pt2 doesn't own any pointer, and so won't // try to delete it... OK, no double delete
// Example 3: Using reset() // void h() ...{ auto_ptr<T> pt( new T(1) ); pt.reset( new T(2) ); // deletes the first T that was // allocated with "new T(1)" }// finally, pt goes out of scope and // the second T is also deleted
拷贝过程中被拷贝的对象(_Right)都会被调用release来放弃所包括的动态对象的所有权,动态对象的所有权被转移了,新的auto_ptr独占了动态对象的所有权。也就是说被拷贝对象在拷贝过程中会被修改,拷贝物与被拷贝物之间是非等价的。这意味着如下的代码是错误的(例程来自 Exceptional C++ Item 37):
// Example 6: Never try to do work through
// a non-owning auto_ptr // void f() ...{ auto_ptr<T> pt1( new T ); auto_ptr<T> pt2;
pt2 = pt1; // now pt2 owns the pointer, and
// pt1 does not pt1->DoSomething();
// error: following a null pointer }
同时也不要将auto_ptr放进标准库的容器中,否则在标准库容器无准备的拷贝行为中(标准库容器需要的拷贝行为是等价的),会导致难以发觉的错误。(请参考Exceptional C++ Item 37获得更多信息)
创建Web工程,使用eclipse ee创建maven web工程 1.右键项目,选择Project Facets,点击Convert to faceted from 2.更改Dynamic Web Module的Version为2.5.(3.0为Java7的,Tomcat6不支持). 如果提示错误,可能需要在Java Compiler设置Compiler compl
最近一直在看python的document,打算在基础方面重点看一下python的keyword、Build-in Function、Build-in Constants、Build-in Types、Build-in Exception这四个方面,其实在看的时候发现整个《The Python Standard Library》章节都是很不错的,其中描述了很多不错的主题。先把Build-in Fu
学习函数式编程
package base;
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) {
// Integer a = 4;
// Double aa = (double)a / 100000;
// Decimal
Java中的泛型的使用:1.普通的泛型使用
在使用类的时候后面的<>中的类型就是我们确定的类型。
public class MyClass1<T> {//此处定义的泛型是T
private T var;
public T getVar() {
return var;
}
public void setVa