Basic Confuse : about pointer & reference in class

K

key9

Hi ALL.

Basic Question.

sample:


class MyClass
{
....

MyClass(int* int_p_) : (int_p_ = p_int_) ;

private:
int* p_int_;
};


==========================

//somewhere on system; I have

int real_value; // may be = 100


//so after I instance a MyClass;

MyClass mc_(&real_value);

//and deleted it

delete mc_;


//the real_value will also be deleted.and leave me a hang prt.I don't want
that.
//any suggestion? Use auto_prt or reference?
//what's the right expression?

//Sorry ,new c++er.
//Thank you very much.
 
I

I V

key9 said:
int real_value; // may be = 100

//so after I instance a MyClass;

MyClass mc_(&real_value);

You are creating mc_ on the stack here, so you can't delete it. Did you
mean:

MyClass* mc_ = new MyClass(&real_value)
//and deleted it

delete mc_;

//the real_value will also be deleted.and leave me a hang prt.I don't want
that.

Deleting a class containing a pointer does not delete the object
pointed to, unless you explicitly do so in a destructor. So shouldn't
have a problem with a dangling pointer here. The problem would arise if
you did something like this:

MyClass some_function()
{
int real_value;

mc = MyClass(&real_value);

return mc; // oops - real_value goes out of scope at the end of
the
// function, so the pointer in mc is no
longer valid
}
 
A

Alf P. Steinbach

* key9:
sample:


class MyClass
{
...

MyClass(int* int_p_) : (int_p_ = p_int_) ;

private:
int* p_int_;
};


==========================

//somewhere on system; I have

int real_value; // may be = 100


//so after I instance a MyClass;

MyClass mc_(&real_value);

//and deleted it

delete mc_;

That will not compile.

'mc_' will be destroyed when the program execution exits the scope it's
declared in, or if it is a namespace scope variable, when the program
execution leaves 'main'.

//the real_value will also be deleted.
No.


and leave me a hang prt.
No.


I don't want that.
//any suggestion? Use auto_prt or reference?
//what's the right expression?

Right expression for what?
 
K

key9

Deleting a class containing a pointer does not delete the object
pointed to, unless you explicitly do so in a destructor. So shouldn't
have a problem with a dangling pointer here. ...


OK I know where is my problem.
I tested in 2 general int point and assume that delete point in a class
will delete "object" as a matter of course .

thank you very much.
 
K

key9

Alf P. Steinbach said:
Right expression for what?

a class container a pointer ,when delete instance of that class, will the
object that pointer pointed to also be deleted?

and I've find the answer. It will NOT.

also thanks a lot ^_^
 
K

key9

BTW:
After known these,confuse again:

MyClass
{
public:
MyClass(){
p_i_ = new int();
}
private:
int* p_i_;

};

MyClass* mc_ = new MyClass();
delete mc_;

Is that means
p_i_ occupied a memory block sample "0x43214321"
will not be freed after delete mc_?

So I have to write code on dtor, or use some tech like auto_prt?
 
K

key9

OK I consider just now
Is that means concept "scope" I wroted:

What ever you created on a "scope"
{
};

after it finished, it will delete whatever created on this scope.

If you did not write code manually, these "objects" pointed,referenced ...
which not belonged to this scope will not be deleted?


YES/NO?

thank you very much.
 
B

Ben Pope

key9 said:
BTW:
After known these,confuse again:

MyClass
{
public:
MyClass(){
p_i_ = new int();
}
private:
int* p_i_;

};

MyClass* mc_ = new MyClass();
delete mc_;

Is that means
p_i_ occupied a memory block sample "0x43214321"

I'm not sure what you mean,
will not be freed after delete mc_?

The memory allocated by p_i_ = new int() will not be freed.
So I have to write code on dtor, or use some tech like auto_prt?

You could use an auto_ptr, or call delete in the destructor.

Either way, you will need to also provide a copy-constructor and
assignment operator for MyClass. (start by declaring them private and
not defining them to prevent copying of the class).

Ben Pope
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top