learning auto_ptr

N

newsock

1. What to do to force an auto_ptr release the object it pointing at before
reaching the end of the scope it resides?

2. If a class is defined as:

class Student
{
int nAge;
int nID;
char sAddress[100];
};

And two Student objects are created: student1 and student2.

Using auto_ptr, what should do to make "student1" and "student2" point to
the same address?
 
G

Gregg

1. What to do to force an auto_ptr release the object it pointing at
before reaching the end of the scope it resides?

Is this a homework assignment? Look up auto_ptr in your documentation.
One of the member functions has a name that makes it obvious which one
does what you want.
2. If a class is defined as:

class Student
{
int nAge;
int nID;
char sAddress[100];
};

This has only private members.
And two Student objects are created: student1 and student2.

You can't do that because the constructor is private.
Using auto_ptr, what should do to make "student1" and "student2" point
to the same address?

You said student1 and student2 were Student objects, not Student pointers
pointers, so they can't point to anything. It is not clear how you are
using auto_ptr here anyway.

Gregg
 
K

Kevin Saff

newsock said:
Using auto_ptr, what should do to make "student1" and "student2" point to
the same address?

This is a bad idea. The point of auto_ptr is that it autmatically deletes
the object when it goes out of scope. So if two auto_ptr's point to the
same address you will probably get an access violation.
 
N

newsock

Gregg said:
1. What to do to force an auto_ptr release the object it pointing at
before reaching the end of the scope it resides?

Is this a homework assignment? Look up auto_ptr in your documentation.
One of the member functions has a name that makes it obvious which one
does what you want.
2. If a class is defined as:

class Student
{
int nAge;
int nID;
char sAddress[100];
};

This has only private members.
And two Student objects are created: student1 and student2.

You can't do that because the constructor is private.
Using auto_ptr, what should do to make "student1" and "student2" point
to the same address?

You said student1 and student2 were Student objects, not Student pointers
pointers, so they can't point to anything. It is not clear how you are
using auto_ptr here anyway.

Gregg

This is not a homework. That's why the question I posted was not
well-thought one. I just want to use an example here to make sure I know
how to use auto_ptr? Thanks for your help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top