473,763 Members | 7,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Destructor for const object

This sounds weird, but I am looking for separate behaviors for
destruction of a const and non-const object.

I am trying to develop a smart/auto pointer class for writing objects
to disk implicitly. The destructor of this class saves the object to
the disk if it is dirty. The problem comes in the following scenario
when a function returns an uncommitted pointer class because same
copies will be committed as two separate objects on disk. For example,

DbPtr< T > some_function( )
{
.....
DbPtr<T> pN; // uncommitted copy
return pN; // pN will be committed in the destructor but the
returned
// copy is not. I could define an assignment operator
// for DbPtr<T> as below but the commit operation
// on const object has problems as it has to modifies
// object
}

tempalte<class T>
class DbPtr : public some_base {
public:
~DbPtr<T>( ) { commit(); } // I need to recognize here if I am
destroying
// a const object to bypass commit.
void commit( );
void operator=( const DbPtr<T>& rP ) { *this = rP; }
void operator=( DbPtr<T>& rP ) { rP.commit(); *this = rP; }
}

Thanks in advance.
-- Virendr
Jul 22 '05 #1
3 2842
Virendra Verma wrote:
This sounds weird, but I am looking for separate behaviors for
destruction of a const and non-const object.

I am trying to develop a smart/auto pointer class for writing objects
to disk implicitly. The destructor of this class saves the object to
the disk if it is dirty.
I don't see what 'const' has to do with this.
The problem comes in the following scenario
when a function returns an uncommitted pointer class because same
copies will be committed as two separate objects on disk. For example,

DbPtr< T > some_function( )
{
.....
DbPtr<T> pN; // uncommitted copy
return pN; // pN will be committed in the destructor but the
returned
// copy is not. I could define an assignment operator
// for DbPtr<T> as below but the commit operation
// on const object has problems as it has to modifies
// object
}

tempalte<class T>
class DbPtr : public some_base {
public:
~DbPtr<T>( ) { commit(); } // I need to recognize here if I am
destroying
// a const object to bypass commit.
You seem to have a lot of <T> that I'm pretty sure you don't need.

Again, 'const' does not seem relevant. Your example doesn't include any
'const' objects, and this approach (if it was possible) does not seem
likely to solve your problem.
void commit( );
"Commit" is probably a non-modifying operation, so this should be

void commit() const;

Yes, you should do this even if it has to modify members of the object.
As long as the observable state of the object does not change (that is,
users of the class can't see a difference), the operation should be
const. Use 'mutable' members if you need to. This is why they exist.
void operator=( const DbPtr<T>& rP ) { *this = rP; }
void operator=( DbPtr<T>& rP ) { rP.commit(); *this = rP; }
I also don't understand what you hope to accomplish with the assignment
operators. Your example doesn't use them, either. And it looks like they
will infinitely recurse.
}


It sounds like you want to commit when the final copy is destroyed
(maybe -- it's not completely clear). In that case, you probably want a
reference-counting scheme. const objects have nothing to do with it, and
assignment operators are only related in the sense that they need to
update reference counts, and generally perform some action when a
reference count reaches 0.

Or maybe you want a 'commit_on_dest roy' flag in your class. When an
instance is copied, you could set it in one copy and clear it in the
other. But you have to remember to do this in the assignment operator
*and* the copy constructor.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #2
On 12 Apr 2004 08:35:33 -0700, vi********@hotm ail.com (Virendra Verma)
wrote:
This sounds weird, but I am looking for separate behaviors for
destruction of a const and non-const object.

I am trying to develop a smart/auto pointer class for writing objects
to disk implicitly. The destructor of this class saves the object to
the disk if it is dirty. The problem comes in the following scenario
when a function returns an uncommitted pointer class because same
copies will be committed as two separate objects on disk. For example,

DbPtr< T > some_function( )
{
.....
DbPtr<T> pN; // uncommitted copy
return pN; // pN will be committed in the destructor but the
returned
// copy is not. I could define an assignment operator
// for DbPtr<T> as below but the commit operation
// on const object has problems as it has to modifies
// object
}
There are no const objects in the example above. Do you mean temporary
objects? Presumably the commit only happens when the last reference
goes out of scope?

tempalte<cla ss T>
class DbPtr : public some_base {
public:
~DbPtr<T>( ) { commit(); } // I need to recognize here if I am
destroying
// a const object to bypass commit.
Do you mean whether you are destroying the last reference to an
object? Just use reference counting...
void commit( );
void operator=( const DbPtr<T>& rP ) { *this = rP; }
void operator=( DbPtr<T>& rP ) { rP.commit(); *this = rP; }
}

Thanks in advance.


If you need different behaviour for const DbPtrs, create a partial
specialization:

template <class T>
class DbPtr<T const> : public some_base
{
//specialization for const
};

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #3

"Virendra Verma" <vi********@hot mail.com> wrote in message
news:30******** *************** ***@posting.goo gle.com...
This sounds weird, but I am looking for separate behaviors for
destruction of a const and non-const object.

I am trying to develop a smart/auto pointer class for writing objects
to disk implicitly. The destructor of this class saves the object to
the disk if it is dirty. The problem comes in the following scenario
when a function returns an uncommitted pointer class because same
copies will be committed as two separate objects on disk. For example,

DbPtr< T > some_function( )
{
.....
DbPtr<T> pN; // uncommitted copy
return pN; // pN will be committed in the destructor but the
returned
// copy is not. I could define an assignment operator
// for DbPtr<T> as below but the commit operation
// on const object has problems as it has to modifies
// object
}

tempalte<class T>
class DbPtr : public some_base {
public:
~DbPtr<T>( ) { commit(); } // I need to recognize here if I am
destroying
// a const object to bypass commit.
void commit( );
void operator=( const DbPtr<T>& rP ) { *this = rP; }
void operator=( DbPtr<T>& rP ) { rP.commit(); *this = rP; }
}

Thanks in advance.
-- Virendr


I don't see what you are trying to do with DbPtr but I suspect that you
should be using a reference counting smart pointer to a real class.
These can be passed around as in your function example without commiting.
When the reference count reaches 0 the smart pointer commits the object it
points to and then
deletes it.
One advantage of separating the commit from the object itself is that you
can then use the object
both inside and outside the database.
Another approach that will work better with exceptions is a separate
transaction class .
Jul 22 '05 #4

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

Similar topics

6
2423
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk implicitly. The destructor of this class saves the object to the disk if it is dirty. The problem comes in the following scenario when a function returns an uncommitted pointer class because same copies will be committed as two separate objects on disk....
11
4368
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining it inline. Like this.
11
1993
by: AB | last post by:
Hi All, I've got an array of objects, during the execution of the program I'd like to assign a particular object to a certain element in the object array. The sample code's like this... class ClassA { public: ClassA()
5
3345
by: Frederick Gotham | last post by:
If we have a simple class such as follows: #include <string> struct MyStruct { std::string member; MyStruct(unsigned const i) {
5
11360
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget to pass all the necessary object files to the linker. Neither of those are my problem. Please bear with me as the question I ask is rather long and I think it's beyond a CS101 level of linker stupidity. If it is a stupid CS101 mistake I'm making...
7
2133
by: michael | last post by:
Hi All, I have written the following to illustrate a problem. I know I have some magic numbers etc please ignore them. What I do not follow is why the line marked results in a call to the destructor for the object. Can someone please explain it for me? #include <iostream>
1
1661
by: tom | last post by:
I have the section of code listed below, but when I return from main an exception is thrown from the library, because while returning, destructor is called on each of the object in the sequence below: step1: destroy m2 step2: destroy f step3: destroy m1 notice, "m1" and "m2" have the class type of Message, and will both use object "f" in the destructor. The problem happens when run to step3, it uses an already destroyed object f....
9
2056
by: itdevries | last post by:
Hi, I've ran into some trouble with an overloaded + operator, maybe someone can give me some hints what to look out for. I've got my own custom vector class, as a part of that I've overloaded the + operator by means of a friend function. Everything worked fine until I decided to use a variable array size (by using new/delete), now I get an error when a temporary object is deleted (e.g. after addition), the error occurs at the delete...
12
2357
by: mc | last post by:
Hi Experts, This may be obvious but I can't find anything one way or another. We have a class which is always allocated using new, e.g. Foo* foo = new Foo() so we came up with the idea of releasing the memory from the destructor as follows: Foo::Foo() { // Initialize stuff
0
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9937
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7366
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
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
3
2793
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.