| |

VerySource

 Forgot password?
 Register
Search
View: 720|Reply: 3

A life cycle problem in a non-com environment

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-22 02:20:01
| Show all posts |Read mode
Class A has a member, which is a pointer to class B
A {
A (B * pB1) {pB = pB1;}
B * pB;
}

If the following piece of code
B * pB = new B ();
A oA = A (pB);
delete pB;
After the last sentence, pB in oA points to an invalid address. My question is in a non-com environment, how to maintain the relationship between A and his member B pointers to ensure that the pointer B in A is always valid. In the com environment, you can use reference counting to achieve the purpose. In the environment, isn't it dangerous for one class to hold a pointer to another class?
Reply

Use magic Report

0

Threads

57

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-1-31 17:54:01
| Show all posts
In C / C ++, this is a programmer's problem. In JAVA DotNet, this is a system problem.
Reply

Use magic Report

0

Threads

36

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-1-31 20:00:01
| Show all posts
Using boost :: shared_ptr can avoid this trouble.
www.boost.org/libs/smart_ptr/shared_ptr.htm
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-2-22 01:45:01
| Show all posts
What is the difference between shared_ptr and auto_ptr functions?
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list