473,789 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Assignment " through base class

Dear all

thanks to Scott Meyers (ME C++, item 33) we know assignment operators
should be protected in the base class.

Is there a common pattern (similar to virtual construction via `clone()')
that allows me to have the following:
(std::auto_ptr and covariant returns omitted for simplicity.)

class Base {
protected:
Base(const Base& rhs);
Base& operator=(const Base& rhs);
public:
...
Base* clone() const = 0;
};

class Leaf : public Base {
public:
...
Base* clone() const;
};

void save(Base& base)
{
Base* backup = base->clone();
try {
write(base); // may modify `base', throw in case of an error
}
catch (...) {
// restore original status of `base'

base = *backup; // <-- does (intentionally) not work

}
}

Of course the following

class Base {
public:
void backup() = 0;
void restore() = 0;
};

solves the problem, but is there a common way for such assignments?

Stephan Brönnimann
br****@osb-systems.com
Open source rating and billing engine for communication networks.
Jul 22 '05 #1
2 1553
On 8 Aug 2004 23:14:27 -0700, Stephan Br?nnimann <br****@hotmail .com>
wrote:
Dear all

thanks to Scott Meyers (ME C++, item 33) we know assignment operators
should be protected in the base class.

Is there a common pattern (similar to virtual construction via `clone()')
that allows me to have the following:
(std::auto_ptr and covariant returns omitted for simplicity.)

class Base {
protected:
Base(const Base& rhs);
Base& operator=(const Base& rhs);
public:
...
Base* clone() const = 0;
};

class Leaf : public Base {
public:
...
Base* clone() const;
};

void save(Base& base)
{
Base* backup = base->clone();
Base* backup = base.clone();
try {
write(base); // may modify `base', throw in case of an error
}
catch (...) {
// restore original status of `base'

base = *backup; // <-- does (intentionally) not work
}
delete backup; }


Wouldn't it be easier to call write on the copy if you are worried about
modifying the original?

void save(Base& base)
{
Base* copy = base.clone();
try {
write(copy);
}
catch (...) {
}
delete copy;
}

Maybe I'm missing the point.

john
Jul 22 '05 #2
"John Harrison" <jo************ *@hotmail.com> wrote in message news:<opscgcpfl k212331@androni cus>...
On 8 Aug 2004 23:14:27 -0700, Stephan Br?nnimann <br****@hotmail .com>
wrote:
[snip]
Wouldn't it be easier to call write on the copy if you are worried about
modifying the original?

void save(Base& base)
{
Base* copy = base.clone();
try {
write(copy);
}
catch (...) {
} // success: write back to original
base = *copy; // won't work delete copy;
}

Maybe I'm missing the point.

john


In the simple situation that I have described: yes that's the solution.

Things however are more complicated: if the write operation succeeds
the object that was stored must survive. This requires the assignment
of the copy to the original ...

Stephan Brönnimann
br****@osb-systems.com
Open source rating and billing engine for communication networks.

BTW: thanks for the corrections.
Jul 22 '05 #3

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

Similar topics

17
3069
by: Tobiah | last post by:
Ok, I miss the idiom that my other languages use, something like: while( foo = getmore()){ process(foo); }
1
3918
by: J. Muenchbourg | last post by:
The following line in part of my asp-sql block: Set rs = Server.CreateObject("ADODB.Recordset") creates an application error: "Set" assignment statements are no longer supported (in this particular block): <% Dim rs, strSQLd
12
4890
by: Charlie Zender | last post by:
Hi, I am unable to compile a large body of code with extremely pedantic compile time checks activate, so that warnings cause errors. With GCC 3.3.1, I do this with gcc -std=c99 -pedantic -Wall -Wunused -Werror -W -Wmissing-prototypes -Wconversion -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -c -o foo.o foo.c
1
3921
by: oeillet | last post by:
hi every one iwant ahelp in this assignment(program ,code in c++) its as follows; aprogram that contain all operation that performed by linked list ; also contain amethod that calculate how many elements are there in the linked list & print them..... then at the same program write aclass called queue that perform two operation <en equeue& de equeue> ..finally write aclass called stack that perform operation<empty,push,pup>. note: this must...
1
8032
by: Java Guy | last post by:
I'm trying to view a web page. IE tells me there are (Java?) errors on the page. Here they are: Line: 15 Char: 7 Error: Wrong number of arguments or invalid propert assignment Code: 0 URL: http://(address.of.my.webcam):port/LiveView.html and
10
1747
by: hall.jeff | last post by:
As a relative new comer to Python, I haven't done a heck of a lot of hacking around with it. I had my first run in with Python's quirky (to me at least) tendency to assign by reference rather than by value (I'm coming from a VBA world so that's the terminology I'm using). I was surprised that these two cases behave so differently test = ,] x = test x = 5 test
11
2525
by: markryde | last post by:
Hello, Followed here is a simplified code example of something which I try to implement; in essence , I want to assign a value to a return value of a method is C. I know, of course, that in this example I can get this by newskb->iph = iphdr (this also appears in a commented line in the example below) ; but I want to achieve the same where the left side is : ip_hdr(newskb). Alas, if I try this , I get a compilation error about line 25....
0
10408
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10139
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
9983
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...
0
9020
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
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
6769
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
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
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
2909
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.