Thread: Move-assignment operator on class with const-declared member variables

  1. #31
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Which I can understand… So I am not sure that there can be a better way than what I showed in my previous post
    Or perhaps there is ?
    Or maybe you just did it wrong? I don't know what to tell you; works for me.
    Code:
    #include <string>
    #include <vector>
    #include <iostream>
    
    
    class ArrayWrapper
    {
    public:
        ArrayWrapper(std::string label, std::initializer_list<double> inits = {})
        : lbl_(label), arr_(inits)    // constructor
        {
        }
        ~ArrayWrapper() = default;  // destructor
        ArrayWrapper(ArrayWrapper const &) = default;    // copy constructor
        ArrayWrapper& operator=(ArrayWrapper const &) = default;   // copy assignment
        ArrayWrapper(ArrayWrapper&&) = default;    // move constructor 
        ArrayWrapper&  operator=(ArrayWrapper&&) = default;    // move assignment
    
    
        ArrayWrapper(ArrayWrapper const & other, std::string label) 
        : ArrayWrapper(other)
        {
            lbl_ = label;
        }
    
    
        void
        dispContent()
        {
            std::cout << "Array \"" << lbl_ << "\" : ";
            for(const auto elem : arr_) {
                std::cout << elem << ", ";
            }
            std::cout << std::endl;
        }
    private:
        std::vector<double> arr_;
        std::string lbl_;
    };
    
    
    int main()
    {
        ArrayWrapper a1("a1", {1, 2, 3});
        ArrayWrapper a2(a1, "a2");
        a1.dispContent();
        a2.dispContent();
    }
    Maybe you can spot the difference between my listing and yours.

    Here is how I compiled:
    Code:
    -*- mode: compilation; default-directory: "c:/Users/jk/Desktop/" -*-
    Compilation started at Thu May 26 11:30:41
    
    
    g++ -std=c++11 galdor.cpp
    
    
    Compilation finished at Thu May 26 11:30:42
    
    g++ (GCC) 4.8.1
    Copyright (C) 2013 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    Last edited by whiteflags; 05-26-2016 at 09:50 AM.

  2. #32
    C++ rookie Galdor's Avatar
    Join Date
    Apr 2016
    Location
    Belgium
    Posts
    32
    Quote Originally Posted by whiteflags View Post
    Or maybe you just did it wrong? I don't know what to tell you; works for me.
    Yes indeed !

    I defined

    Code:
    ArrayWrapper(ArrayWrapper const & other, std::string label="")
    instead of

    Code:
    ArrayWrapper(ArrayWrapper const & other, std::string label)
    (notice the default parameter present in the first case which becomes a "mandatory" parameter in the second case)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 33
    Last Post: 09-28-2011, 05:17 AM
  2. Replies: 11
    Last Post: 05-02-2009, 09:23 AM
  3. Accessing a protected member declared in parent class
    By Canadian0469 in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2008, 03:50 PM
  4. cannot access private member declared in class
    By newme in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2008, 03:57 PM
  5. Replies: 2
    Last Post: 02-14-2008, 02:59 PM

Tags for this Thread