Sponsored Content
Top Forums Programming Need to understand the overloaded assignment operator behavior Post 302781079 by Corona688 on Friday 15th of March 2013 01:19:18 PM
Old 03-15-2013
Quote:
Originally Posted by royalibrahim
I understand your point Corona688, but I am still unable to figure out the answer to my question Smilie, that is why the class A's assignment operator is invoked when instance of B is assigned when class A and B has no inheritance relation between them except containment object?
What method would you expect it to use, to overwrite B's contents of type A?

A's own operators are the only way the compiler has to do so. Even a default operator is still an actual function that gets called.
 

9 More Discussions You Might Find Interesting

1. Programming

what is diff b/w copy constructor and overloaded assignment operator

Helo i m new in c++. i m confuse about what is exact difference b/w copy constructor and overloaded assignment operator. Regards, Amit (3 Replies)
Discussion started by: amitpansuria
3 Replies

2. Shell Programming and Scripting

scalar variable assignment in perl + { operator

When reading over some perl code in a software document, I came across an assignment statement like this $PATH = ${PROJECT}/......./.... In this particular form of scalar variable assignment, what does the curly braces operators do ? Also, what is the benefit in doing scalar assignment this... (3 Replies)
Discussion started by: JamesGoh
3 Replies

3. HP-UX

ERROR: more than one instance of overloaded function "vprintf" has "C" linkage

Hi people! I've got this own library: -------------------------------------------- Personal.h -------------------------------------------- #ifdef __cplusplus extern "C" { #endif #include <stdio.h> #include <stdarg.h> #include <string.h> ... (0 Replies)
Discussion started by: donatoll
0 Replies

4. Programming

c++ assignment operator overloading

Hello everyone! Suppose that I have something like this A a; a.mem=new int; A b = a; where class A { public: int * mem; A() : mem(NULL) { } ~A() { if (mem!=NULL) delete mem; (1 Reply)
Discussion started by: bashuser2
1 Replies

5. Homework & Coursework Questions

Assignment Help

1. List commands to create the directory hierarchy $HOME/a/b/c in vi to replace all occurences of TMP with tmp in lines 1 through 10 in vi to replace first occurence of CPU_file with DISK_file at line 15 2. Explain with a very simple example, usage of "ls -a" 3. What do the... (2 Replies)
Discussion started by: jessesaini
2 Replies

6. Solaris

Looking to understand what this command $$ does in variable assignment?

Hi Folks, I'm looking to figure something out in an existing script I'm trying to understand. the command in question(on a Solaris Box using KSH) is: WORKDIR=/tmp/namereplaced.exec.$$.$RANDOM Now, I know it's setting the $workdir environmental variable... And I understand most of... (2 Replies)
Discussion started by: Marc G
2 Replies

7. UNIX for Dummies Questions & Answers

Need a little help with assignment

Hello all im currently working on this assignment and a little stump on how to check for an argument heres the instructions: Step 4: Modify your script so that if there is an argument called TestError you display the following error message with your usage statement. TestError found Example:... (1 Reply)
Discussion started by: bsn3971
1 Replies

8. Shell Programming and Scripting

Assignment operator without operand

Does anyone know how this line in bash works? local gotbase= force= nicelevel corelimit local pid base= user= nice= bg= pid_file= local cgroup= These lines are part of the daemon function inside the "functions" file at /etc/init.d in RH. (3 Replies)
Discussion started by: Rameshck
3 Replies

9. Shell Programming and Scripting

Bash variable assignment failure/unary operator expected

I have a little code block (executing on AIX 7.1) that I cannot understand why the NOTFREE=0 does not appear to be assigned even though it goes through that block. This causes a unary operator issue. #!/bin/bash PLATFORM="AIX" NEEDSPC=3000 set -x if ; then lsvg | grep -v rootvg | while... (6 Replies)
Discussion started by: port43
6 Replies
FBB::fswap(3bobcat)						Fast swap function					       FBB::fswap(3bobcat)

NAME
FBB::fswap - generic template fast swap function SYNOPSIS
#include <bobcat/fswap> DESCRIPTION
The information stored in objects frequently needs to be swapped. A well-known example is the swapping operation required when implementing an overloaded assignment operator. For example, the generic form of the operator assignment operator is: Class &operator=(Class const &other) { Class tmp(other); swap(tmp); return *this; } The swap functionality merely swaps the contents of the current object and another object. The standard std::swap function calls the class's operator= function to swap objects. Newer implementations might use move-operations to increase the speed of the swaping operation, but in both cases some form of the assignment operator must be available. Swapping, however, might be possible when assignemnt isn't. Classes having reference data members usually don't offer assignment operators but swapping might be a well-defined operation. It is well known that objects can be installed in a block of memory using placement new, using a block of memory the size of the object to construct the object it. This is the foundation of the template function FBB::fswap (fast swap). This swap function merely uses the memory occupied by objects to implement the swapping operation and it may therefore be used with classes having const data members, reference data members, ponters to allocated memory etc, etc. The function simply uses a spare block of memory the size of the object to be swapped. It then uses memcpy(3) to swap the information contained in the two objects, using the spare block of memory as a placeholder. The function uses partial specializations to optimize the swapping operation for objects of sizes 1, 2, 4 or 8 bytes. It uses memcpy(3) for objects of other sizes. NAMESPACE
FBB All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB. INHERITS FROM
- SWAP FUNCTION
o fswap(Type &lhs, Type &rhs): This template function swaps the contents of the two objects. It can be used with classes having const data members, reference mem- bers, pointer members or standard value-typed data members. EXAMPLE
#include <iostream> #include "../fswap" class Demo { std::ostream &d_out; size_t d_value; public: Demo(std::ostream &out = std::cerr, size_t value = 0) : d_out(out), d_value(value) {} void show(char const *msg) { d_out << msg << ". Value: " << d_value << ' '; } }; using namespace std; int main() { Demo d1; Demo d2(cout, 12); FBB::fswap(d1, d2); d1.show("This is d1"); // to cerr: 12 d2.show("This is d2"); // to cout: 0 } FILES
bobcat/fswap - defines the class interface SEE ALSO
bobcat(7), memcpy(3) BUGS
The fswap function should not be applied mechanically to swap objects of classes having pointer data members defining, e.g., a linked list. Consider a list of four objects like: A -> B -> C -> D fast-swapping B and C would result in the following corrupted list: +------+ | | A -> C -+ +-> B -+ +-> D | | +-------------+ However, classes implementing a data structure like a linked-list might still benefit from fast swapping operations: by implementing their own swap member they could first use fast swapping to swap the objects, followed by another fast swap to unswap their `next' pointers. The fswap function should also not be used for objects defining (back-)pointers to their own data. Consider the following objects using pointers to data and (back-)pointers to the original objects: Before fswapping: A B +--------+ +-----------+ +--------+ +-----------+ | | | | | | | | +--> *Aimp------> *A (back)--+ +--> *Bimp------> *B (back)--+ | | | | | | | | | | | | +--**Aimp | +-----------+ | +--**Bimp | +-----------+ | +--------+ <---------------+ +--------+ <---------------+ After fswapping: +-------------------------------+ +--|-------------------------------|-+ +-------------|--|-----------------+ | | | A | v | B | v | +--------+ | +-----------+ | +--------+ | +-----------+ | | | | | | | | | | | | +-----> *Bimp---+ | *A (back)--+ +---> *Aimp---+ | *B (back)--+ | | | | | | | | | | | | | +---**Bimp | +-----------+ | +---**Aimp | +-----------+ | | +--------+ <---------------+ | +--------+ <---------------+ +------------------------------------+ After the swap **Bimp should point to Bimp's address (now at A), but in fact it points to Aimp's address (now at B). Likewise, the back pointers still point at their original objects rather than at their swapped objects. All stream classes define such pointers and can therefore not be swapped using fswap. The bottom line being that fswap should only be used for self-defined classes for which it can be proven that fast-swapping does not cor- rupt the values of its pointer data. DISTRIBUTION FILES
o bobcat_3.01.00-x.dsc: detached signature; o bobcat_3.01.00-x.tar.gz: source archive; o bobcat_3.01.00-x_i386.changes: change log; o libbobcat1_3.01.00-x_*.deb: debian package holding the libraries; o libbobcat1-dev_3.01.00-x_*.deb: debian package holding the libraries, headers and manual pages; o http://sourceforge.net/projects/bobcat: public archive location; BOBCAT
Bobcat is an acronym of `Brokken's Own Base Classes And Templates'. COPYRIGHT
This is free software, distributed under the terms of the GNU General Public License (GPL). AUTHOR
Frank B. Brokken (f.b.brokken@rug.nl). libbobcat1-dev_3.01.00-x.tar.gz 2005-2012 FBB::fswap(3bobcat)
All times are GMT -4. The time now is 07:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy