473,387 Members | 1,423 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,387 software developers and data experts.

ownership style question


I've been using code such as the following, whereby I call the
constructor of a class with an object allocated on the heap. The
class is responsible for tidying up the memory.
class Base{};
class A : public Base {};
class B : public Base {};
class MyClass
{
private:

Base* m_pBase;

public:

MyClass(Base* pB):m_pBase(pB){}

~MyClass(){delete m_pBase;}
};
void SomeClass::SomeMethod()
{
//m_pMC is a member variable of type MyClass*

m_pMC = new MyClass(new A());

}

Intuitively, this seems like bad style. I feel that each object should
be responsible for the creation and deletion of objects it uses. Is
this intuition correct?

And, if so, how should I change the design? I can't do something like

void SomeClass::SomeMethod()
{
Base* pB = new A();

m_pMC = new MyClass(pB);

delete pB;
}

because m_pBase must be valid outside the scope of the class method.

How can I improve this design?

Thanks for any help and I hope this made sense!
Jul 22 '05 #1
4 2385
"tarmat" <ta****@btopenworld.com> wrote in message
news:90********************************@4ax.com...
|
| I've been using code such as the following, whereby I call the
| constructor of a class with an object allocated on the heap. The
| class is responsible for tidying up the memory.

The C++ standard library includes a smart pointer class that
was designed to express transfer of ownership: auto_ptr.

| class MyClass
| {
| private:
|
| Base* m_pBase;
|
| public:
|
| MyClass(Base* pB):m_pBase(pB){}
|
| ~MyClass(){delete m_pBase;}
| };
....
| How can I improve this design?

This could become:

class MyClass
{
private:
std::auto_ptr<Base> m_pBase;

//NOTE: for safety, you want to disable these 2 default ops here:
MyClass(MyClass const&); // not implemented
void operator=(MyClass const&); // not implemented

public:
MyClass(std::auto_ptr<Base> pB):m_pBase(pB){}
~MyClass() { /*auto-deleted*/ }
};
Note that there is a debate on whether std::auto_ptr parameters
should be passed by value (as above) or by reference.
The former is dangerous when a function has multiple parameters.
The latter is less explicit about the ownership transfer,
and can be cumbersome (requires the initialization of
an intermediate variable).
Regards,
Ivan
--
http://ivan.vecerina.com
Jul 22 '05 #2


Ivan Vecerina wrote:
"tarmat" <ta****@btopenworld.com> wrote in message
news:90********************************@4ax.com...
|
| I've been using code such as the following, whereby I call the
| constructor of a class with an object allocated on the heap. The
| class is responsible for tidying up the memory.

The C++ standard library includes a smart pointer class that
was designed to express transfer of ownership: auto_ptr.


You need to be careful with auto_ptr particularly with version 2
implementations which can leave you with dangling pointers. Version
three is supposed to reset the pointer to 0 after the transfer which
leads to better respect for the thing. You are probably better off with
something a little more robust like a shared_ptr.

Jul 22 '05 #3
"lilburne" <li******@godzilla.com> wrote in message
news:bp*************@ID-179504.news.uni-berlin.de...
|
| Ivan Vecerina wrote:
| > "tarmat" <ta****@btopenworld.com> wrote in message
| > news:90********************************@4ax.com...
| > |
| > | I've been using code such as the following, whereby I call the
| > | constructor of a class with an object allocated on the heap. The
| > | class is responsible for tidying up the memory.
| >
| > The C++ standard library includes a smart pointer class that
| > was designed to express transfer of ownership: auto_ptr.
|
| You need to be careful with auto_ptr particularly with version 2
| implementations which can leave you with dangling pointers. Version
| three is supposed to reset the pointer to 0 after the transfer which
| leads to better respect for the thing. You are probably better off with
| something a little more robust like a shared_ptr.

But shared_ptr expresses shared ownership, where both the caller
and callee may keep using the owned object. It does not express
ownership transfer, and serves a very different purpose IMO.

Pre-standard versions of std::auto_ptr had problems, true.
But these are easy to avoid (assume that it is UB to use
an auto_ptr after it has been assigned to another instance).
And hopefully these outdated libraries are about to disappear
from developer's systems by now.

Yes, IIRC, MSVC6 still shipped with an awfully outdated
version of the standard library. But users who can't afford
updating their platform (nor purchasing the library update)
definitely should switch to the free STLport (www.stlport.org).
(at least if they care to use the standard C++ library at all).
Regards,
Ivan
Jul 22 '05 #4
Ivan Vecerina wrote:
"lilburne" <li******@godzilla.com> wrote in message
news:bp*************@ID-179504.news.uni-berlin.de...
|
| Ivan Vecerina wrote:
| >
| > The C++ standard library includes a smart pointer class that
| > was designed to express transfer of ownership: auto_ptr.
|
| You need to be careful with auto_ptr particularly with version 2
| implementations which can leave you with dangling pointers. Version
| three is supposed to reset the pointer to 0 after the transfer which
| leads to better respect for the thing. You are probably better off with
| something a little more robust like a shared_ptr.

But shared_ptr expresses shared ownership, where both the caller
and callee may keep using the owned object. It does not express
ownership transfer, and serves a very different purpose IMO.

I agree that the semantics of ownership transfer is a good
reason to use auto_ptr. One that resets the pointer to 0
after transfer is a far more solid implementation.
Pre-standard versions of std::auto_ptr had problems, true.
But these are easy to avoid (assume that it is UB to use
an auto_ptr after it has been assigned to another instance).
And hopefully these outdated libraries are about to disappear
from developer's systems by now.


Personally I don't like UB that can appear to work, because
it doesn't discourages misuse. I suspect that a few will get
a nasty surprise with V3 auto_ptr's.

Jul 22 '05 #5

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

Similar topics

1
by: Ryan | last post by:
We have a DTS package developed on our development PC's (SQL 7). It runs fine. When we schedule it on the server (SQL 7), it fails. We have been able to find that this is a known issue down to the...
11
by: Jacob | last post by:
I am trying to find the best way of documenting (in code and comments) ownership of secondary contained objects. This is my current status, and I would appreciate feedback on it: Case 1: ...
0
by: Wells Fargo | last post by:
<html dir=3D"ltr"> <head> <meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dwindows= -1252"> <title>New Page 1</title> </head> <body>
14
by: Howard | last post by:
Hi, I recently had a problem where I decided to store objects in a vector. (Previously, I had always stored pointers in vectors). Well, naturally, when storing an object in a vector, using...
2
by: Benden Ziyade | last post by:
Hello; I want to write a C program that check file ownership in /bin directory(ls, mkdir...). But I don't know how I start. I'm happy with your helping.
9
by: Andrew | last post by:
Apologies for the double-post.. I'm new, just getting used to this.. and should have posted this way in the first place.. How does one go about taking ownership of a registry key using C# & .NET...
7
by: Stephen Engle | last post by:
I am trying to allow for user account to take ownership of an Active Directory object. I have assigned the Modify Owner permission to the user on the AD object - a distribution list in this case. ...
0
by: Stodge | last post by:
Hi folks, new to Boost Python and struggling to build a prototype at work. I thought I'd start with a conceptual question to help clarify my understanding. I already have a basic prototype working...
0
by: Svend Jensen | last post by:
Leo wrote: A synonym will do. /Svend
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.