Thread: c++ primer exercise 15.26

  1. #1
    Registered User
    Join Date
    Sep 2015
    Location
    Italy
    Posts
    38

    Question c++ primer exercise 15.26

    Hello everybody!
    This is my first post here so I hope that all will be ok, otherwise please be patients
    I've trouble defining the copy-control move assignment of a derived class, it should be trivial however I don't know how to call the base class move assignment (should I?) the code I wrote is here.
    The concerned part is:
    Code:
    // Move assignment
    Bulk_quote& Bulk_quote::operator=(Bulk_quote&& q) noexcept
    {
        if(this != &q)
        {
            *this=Quote::operator=(q); // <---- !!!???
            min_qty=q.min_qty;    discount=q.discount;
            cout    << "Class: "    << (typeid(*this).name())    << ", Move assigment"    << endl;
        }
        return *this;
    }
    Could anybody give me some hints? Thank in advance

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To call the base class operator =, all you need to do this is write
    BaseClass:perator = (q);
    In this case, BaseClass would be Quote. It tells the compiler to call operator =, but by default, it calls the one in the current class you're defining it in (e.g. Bulk_quote), so you need to prefix it with the base class's name to distinguish it.
    If you want to call the base class's move assignment operator, you need to write
    BaseClass:perator = (std::move(q));

    Do not assign the return value to *this. The base class's operator = already moves or copies the state into *this. That's its job.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2015
    Location
    Italy
    Posts
    38

    Red face

    Quote Originally Posted by Elysia View Post
    The base class's operator = already moves or copies the state into *this. That's its job.
    Yes, because Quote object is returned as reference in the copy/move assignment operator. I apologize, it was really easy to understood. Thanks Elysia.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with an exercise from the book "C++ Primer"
    By vizbasic2010 in forum C++ Programming
    Replies: 3
    Last Post: 06-07-2012, 07:27 AM
  2. After C Primer Plus... for fun
    By Huncowboy in forum Programming Book and Product Reviews
    Replies: 7
    Last Post: 11-02-2010, 12:12 PM
  3. C++ Primer
    By gin in forum C++ Programming
    Replies: 2
    Last Post: 06-27-2008, 09:23 AM
  4. C++ Primer 4th Edition, Problem with Exercise
    By Kaidao in forum C++ Programming
    Replies: 4
    Last Post: 07-15-2006, 11:13 AM
  5. C++ Primer vs. Plus
    By gL_nEwB in forum C++ Programming
    Replies: 14
    Last Post: 05-09-2006, 07:01 PM