|
-
March 4th, 2005, 06:18 PM
#1
about "auto_ptr"
Code:
class A
{
public:
A(int a,int b):a1(a),a2(b){}
void output(){cout<<"a1="<<a1<<";a2="<<a2<<endl;}
private:
int a1;
int a2;
};
template<class T>
class auto_ptr {
public:
auto_ptr(T *p = 0): ptr(p) {}
~auto_ptr() { delete ptr;}
T* operator ->(){return ptr;}
private:
T *ptr;
};
class B
{
public:
B(int a,int b):ca(new A(a,b)){}
void output() {ca->output();}
private:
const auto_ptr<A> ca;
};
void main()
{
B b(3,4);
b.output();
}
In B, its member data is pointer which point to a class A. with the use of auto_ptr, the point will be delete automatically after object b goes out of range. my compiler doesn't support auto_ptr in standard libary, so I write it by myself.
my quest is, the program can not be compiled, but if I get of the "const" before auto_ptr<A>, it can be compiled and run right. why?
Last edited by greghua; March 4th, 2005 at 09:07 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|