Thread: overloading derived class

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    overloading derived class

    Hello

    I want to overload copy assignment operator of base class in derive class. For instance:
    Class base and derive have some copy assignment operator.

    Heres the class definition:

    Code:
    class derive : public base {
    public:
      base() { }
      base(int n) : num(n) { }
      base& operator= (const base& source) {
         if (this == &source) return *this; //self-assignment check
         num = source.num;
         return *this;
      }
    private:
      int num;
    };
    This will only copy num to new instance if I do:

    derive instance;
    derive instance2 = instance;

    What should I do so that copy assignment operator from derived class will be used?
    Last edited by l2u; 12-14-2007 at 03:53 AM.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    OK, I'm extremely confused by your post. Why does base derive from derived, instead of the other way round, as the names suggest?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by CornedBee View Post
    OK, I'm extremely confused by your post. Why does base derive from derived, instead of the other way round, as the names suggest?
    Sorry, I mixed it up.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Still messed up. Now the derived class tries to define constructors using base as the name. The assignment operator uses base as the argument and return type. And the usage example might also not be what you intended.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by l2u View Post
    This will only copy num to new instance if I do:

    base base_instance;
    base instance2 = base_instance;

    What should I do so that copy assignment operator from derived class will be used?
    How can the code above call anything from a derived class if you're only creating base class objects?

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    630
    I fixed the mistake now, hope its correct now
    Thanks for help

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    class derive : public base {
    public:
      derive() { }
      derive(int n) : num(n) { }
      derive& operator= (const derive& source) {
         if (this == &source) return *this; //self-assignment check
         num = source.num;
         return *this;
      }
    private:
      int num;
    };
    I fixed up the "mixed".

    In this class, you don't actually need an assignment operator, the default one will do exactly the same job.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The self-assignment check is quite unnecessary here, too. Whatever you gain from not assigning a variable to itself in a few cases comes at the price of an additional check every single time.

    Avoiding self-assignment is critical if you for example freed the currently held memory first, only to discover that you have just deleted the pointer whose contents you wanted to copy.

    But then, this design is problematic too: it is better not to free old resources before you have successfully acquired new ones.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't know if you can override a base operator? I think I'm going to try.
    Well, looks like you can IF the base operator is NOT defined or is pure virtual.
    Last edited by Elysia; 12-14-2007 at 05:34 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by matsp View Post
    Code:
    class derive : public base {
    public:
      derive() { }
      derive(int n) : num(n) { }
      derive& operator= (const derive& source) {
         if (this == &source) return *this; //self-assignment check
         num = source.num;
         return *this;
      }
    private:
      int num;
    };
    I fixed up the "mixed".

    In this class, you don't actually need an assignment operator, the default one will do exactly the same job.

    --
    Mats
    I want the default assignment operator (in base class) to do the job and beside that the assignment operator in derive class should also copy num to the other instance.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To do such a thing you need to overload a new assignment operator for Derived. It will be called when you assign Derived objects. In case you want to base class to handle all the functionality, you can call the Base class's assignment operator (Base:perator = (source)).
    But really, there's no need since all members are copied to the new object by default (shallow copy) when you do assignment, so in this case you don't need to overload operator =.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    630
    I tried doing:

    Code:
    class derive : public base {
    public:
      derive() { }
      derive(int n) : num(n) { }
      derive& operator= (const derive& source) {
         base::operator=(source);
         num = source.num;
         return *this;
      }
    private:
      int num;
    };
    But it seems like it only copies 'num' to new instance, not the objects in base class.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    ...not the objects in base class...
    What does that mean?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Elysia View Post
    What does that mean?
    It seems like it doesnt call the copy assignment operator in base class.

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Can we have a complete test case, please? Something like this:

    Code:
    #include <iostream>
    
    class base
    {
    public:
    	int i1;
    
    	base(int a1) : i1(a1) {}
    	base(const base &o) : i1(o.i1) {}
    	base &operator =(const base &o) { i1 = o.i1; return *this; }
    };
    
    class derived : public base
    {
    public:
    	int i2;
    
    	derived(int a1, int a2) : base(a1), i2(a2) {}
    	derived(const derived &o) : base(o), i2(o.i2) {}
    	derived &operator =(const derived &o) {
    		base::operator=(o);
    		i2 = o.i2;
    		return *this;
    	}
    };
    
    int main()
    {
    	derived d1(10, 15);
    	derived d2(20, 25);
    	d1 = d2;
    
    	std::cout << "d1: " << d1.i1 << ", " << d1.i2 << "\n";
    }
    This does exactly what is expected. How does your code differ?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. derived class
    By BKurosawa in forum C++ Programming
    Replies: 9
    Last Post: 08-09-2007, 02:18 PM
  2. Inheritance: assign base class to derived class
    By MWAAAHAAA in forum C++ Programming
    Replies: 15
    Last Post: 01-22-2007, 04:31 PM
  3. Derived class can't declare
    By hdragon in forum Game Programming
    Replies: 13
    Last Post: 03-21-2006, 02:48 PM
  4. base class pointer to derived class objects
    By curlious in forum C++ Programming
    Replies: 4
    Last Post: 09-28-2003, 08:39 PM
  5. Replies: 4
    Last Post: 12-29-2002, 12:29 AM