1

I would like to define a copy-constructor which just assigns the object to another one:

Header:

 #ifndef TESTCLASS_HPP
#define TESTCLASS_HPP

#include <boost/math/distributions/geometric.hpp>

class Testclass {
public:
    Testclass();

    virtual ~Testclass();
private:
    Testclass(const Testclass& orig);
    int alpha;
    boost::math::geometric_distribution <> geometricboost;

};

#endif  /* TESTCLASS_HPP */

Implementation:

   #include "Testclass.hpp"

Testclass::Testclass() : geometricboost(0) {
}

Testclass::Testclass(const Testclass& obj_ref) {
     *this = obj_ref;
}

Testclass::~Testclass() {
}

The class itself doesn't contain any pointers, but eventually an object. IS this actually possible to do?

If not, what would be the easiest way just to just assign?

This gives the error:

Testclass.cpp: In copy constructor ‘Testclass::Testclass(const Testclass&)’: Testclass.cpp:13:46: error: no matching function for call to ‘boost::math::geometric_distribution::geometric_distribution()’ Testclass::Testclass(const Testclass& obj_ref) {

Qohelet
  • 1,459
  • 4
  • 24
  • 41
  • 2
    If your class does not contain any pointers or other objects where default copy semantics don't work, then you don't need to write your copy assignment at all – Maksim Solovjov Aug 26 '15 at 14:04
  • This would invoke the copy-assignment operator (either the default one or an overriden version). Hence the usual caveats on calling methods from constructors apply. In particular: don't expect any `virtual` to work as it should. – dhke Aug 26 '15 at 14:05
  • It's less about what's necessary. I'm interested if and how this could work – Qohelet Aug 26 '15 at 14:06
  • @Qohelet As mentioned it works by calling the assignment operator, either the compiler generated one, or your own definition. – πάντα ῥεῖ Aug 26 '15 at 14:09
  • @πάνταῥεῖ I thought so, but I don't know how to deal with the error I get: `no matching function for call to OBJECT`. – Qohelet Aug 26 '15 at 14:13
  • 3
    @Qohelet please post an [mcve] that generates that error. – NathanOliver Aug 26 '15 at 14:16
  • @NathanOliver - done so – Qohelet Aug 26 '15 at 14:22
  • 1
    http://stackoverflow.com/questions/2639017/calling-assignment-operator-in-copy-constructor is at least somewhat relevant. – Alan Stokes Aug 26 '15 at 14:36

2 Answers2

1

If you implement your own copy constructor then you should initialize all members in the constructor's initializer list. Otherwise the default constructor is called for every member of a not built-in type before the assignment operator is executed.

boost::math::geometric_distribution seems to have no default constructor. That's why you get the compiler error. You would fix it by using the copy constructor of geometric_distribution:

Testclass::Testclass(const Testclass& obj_ref)
    : alpha(obj_ref.alpha), 
      geometricboost(obj_ref.geometricboost)
{
}

According to the rule of three you should consider to also implement the copy-assignment operator (, move constructor, move assignment operator).

Stephan
  • 4,187
  • 1
  • 21
  • 33
  • Ok, got it. After adding the initialisation the errors are gone... But there is one thing that puzzles me. I had several classes - in some of them the error occurred as described above - in some it didn't. Even though they have the same members and functions? Do the errors depend on the usage of the class also – Qohelet Aug 26 '15 at 16:16
1

When your copy constructor runs it first tries to default initialise geometricboost (because you don't specify any initialisation for it) and only then calls the assignment operator. This doesn't work because the geometric distribution does not have a default constructor.

You would be better off implementing assignment from copy (using the copy then swap idiom) rather than vice versa, as suggested at Calling assignment operator in copy constructor.

Alternatively you could just delete your copy constructor completely - the compiler generated one will work just fine.

Community
  • 1
  • 1
Alan Stokes
  • 18,815
  • 3
  • 45
  • 64
  • Actually - does the error only appear if there is a special initialisation needed? – Qohelet Aug 26 '15 at 16:23
  • Yes. If all members can be default constructed then you wouldn't get an error from your code. That doesn't mean it's a good idea though. – Alan Stokes Aug 26 '15 at 17:42