473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Basic Confuse : about pointer & reference in class

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.
Feb 28 '06 #1
7 1710
I V
key9 wrote:
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
}

Feb 28 '06 #2
* 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?
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 28 '06 #3
"I V" <wr******@gmail.com> 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.
Feb 28 '06 #4

"Alf P. Steinbach" <al***@start.no>
:
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 ^_^
Feb 28 '06 #5
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?
Feb 28 '06 #6
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.
Feb 28 '06 #7
key9 wrote:
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
--
I'm not just a number. To many, I'm known as a string...
Feb 28 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: NKOBAYE027 | last post by:
FIRST POST Hi All: I'm trying to write a simple specialization before moving on to something a bit more complex - always a good idea in my case, at least. :o) I'm trying to adapt the example...
14
by: bo | last post by:
And why and where one should use one vs. the other? Verbally, it seems like semantics to me--but obviously there is some actual difference that makes references different and or preferable over...
4
by: key9 | last post by:
Hi All simple question: 1.==================================== { int* p_1 = new int(); }; //will p1 leak?
8
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.