473,563 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1722
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_v alue)
//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_v alue);

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,referen ced ...
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
1763
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 from Stroustrup, 3rd ed., The C++ Programming Language p. 344 I'm using MSDev 6.0 in case that's an issue. Here's the source...
14
2517
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 pointers in some cases... TIA
4
1672
by: key9 | last post by:
Hi All simple question: 1.==================================== { int* p_1 = new int(); }; //will p1 leak?
8
2389
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 (or better a const reference). for eg, const PointRange* points = cc.points(ptAligned);
0
7583
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7885
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7638
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7948
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6250
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
923
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.