c++ assignment operator overloading


 
Thread Tools Search this Thread
Top Forums Programming c++ assignment operator overloading
# 1  
Old 12-18-2010
c++ assignment operator overloading

Hello everyone!

Suppose that I have something like this
Code:
A a;
a.mem=new int[100];
A b = a;

where
Code:
class A {
public:
 int * mem;
   A() : mem(NULL) { 
   }

  ~A() {
     if (mem!=NULL)
    delete mem;
   }
}

Of course ,because destructor is called two times, I will get a seg fault.
How can i implement an operator overloading function inside class A to allocate memory before copy and avoid seg fault?

I hope you understand my question...

---------- Post updated at 11:37 AM ---------- Previous update was at 11:19 AM ----------

sorry. next time I will use code tags Smilie

Last edited by Scott; 12-18-2010 at 12:32 PM.. Reason: Use code tags, please...
# 2  
Old 12-18-2010
Since the class doesn't know how much memory is allocated to mem, you can't. You have to store the size somewhere.

I'd do it like this:

Code:
#include <stdio.h>
#include <string.h>

class A {
public:
   int * mem;
   int size;
   A(int isize=0) : mem(NULL), size(0) {
      if(isize > 0)
      {
        mem=new int[isize];
        size=isize;
      }
   }

   A &operator=(const A &o)
   {
      if(size > 0) // Free existing memory
      {
         delete mem;
         size=0;
      }

      if(o.size > 0) // Copy other memory if it exists
      {
         mem = new int[o.size];
         size=o.size;
         memcpy(mem, o.mem, o.size * sizeof(int));
      }

      return(*this);
   }

  ~A() {
     if (mem!=NULL)
     {
       printf("Freeing %p\n", mem);
       delete mem;
     }
   }
};

int main(void)
{
    A a(100), b;

    b=a;
}


Last edited by Corona688; 12-18-2010 at 10:39 PM.. Reason: forgot the return statement
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C++ operator overloading error

hi, I defined my own String class and overloaded the output operator with friend ostream& operator<<(ostream& os, const myString& str); //overloads the << operator so it can be used to output values of type myString which works fine. Until I try to execute the following statement: // +... (6 Replies)
Discussion started by: milhan
6 Replies

2. 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

3. 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

4. Programming

Need to understand the overloaded assignment operator behavior

Hi, In the following code, class A { public: void operator=(const A& rhs) { if (this == &rhs) cout << "self-assigned"; } }; class B { A a; // should not be a pointer member, (i.e) A* a }; int main() { B b; b = b; // Ans: self-assigned } I am really... (5 Replies)
Discussion started by: royalibrahim
5 Replies

5. UNIX for Dummies Questions & Answers

mysqld overloading cpu of VPS

Hi bros I have a VPS 512mb (Burst 2GB) with Kloxo installed and hosting few sites on it with not much traffic I am facing high cpu load for the last few days and seems mysqld is overloading the cpu Any suggestion will be appreciated Regards Rizwan Top output is as under top -... (2 Replies)
Discussion started by: rizwan65
2 Replies

6. Programming

Delete operator overloading with multiple arguments.

Hi, I have an requirement to overload the delete operator in C++, but it should also accept the sizeof() the object that is to be deleted. Actually I am trying to built a custom memory allocator and deallocator like a pool, which makes me to overload the delete operator. Small example of the... (1 Reply)
Discussion started by: kapilkumawat
1 Replies

7. Programming

C++ Optr Overloading

Hi All, In C++ one operator can be overloaded, but it can't be overloaded by it's own derieved class Please let me know operator. Thanks, Naga:cool: (1 Reply)
Discussion started by: Nagapandi
1 Replies

8. AIX

xlC compilation error when dealing with operator overloading

Hi, I have a piece of C++ code that can be compiled using g++, but reports an error when compiled with xlC: xlC -DHAVE_CONFIG_H -I../SRC -I../include -DNoChange -DSPRNG_MPI -q64 -DLONG64=long -I/usr/lpp/ppe.poe/include -DLONG64=long -c -o libsprng_a-bignum.o bignum.cpp "bignum.cpp",... (1 Reply)
Discussion started by: luop0812
1 Replies

9. 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

10. 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
Login or Register to Ask a Question