C++ assignment operator for ROOT class

149 views Asked by At

I'm using the ROOT frameowrk and I want to write a class the has a TMultiGraph member. I'm trying to write and assignment operator for my class but I fail due to compilaton issues. The class TMultiGraph keeps it's assignment operator as protected.

The header for my class:

#include "../include/clipper.hpp"
#include "TMultiGraph.h"
#include "TColor.h"
#include "RtypesCore.h"

using namespace ClipperLib;

class ClipperDraw : protected TMultiGraph {

public:
    ClipperDraw() {}

    ClipperDraw& operator=(const ClipperDraw &c);

private:
    TMultiGraph mg;

};

The .cpp is:

ClipperDraw& ClipperDraw::operator=(const ClipperDraw &c)
{
    mg = c.mg;
    return *this;
}

When compiling I get this message:

g++ -fPIC -Wall `root-config --cflags` -I./include -O2  -c -o obj/ClipperDraw.o src/ClipperDraw.cpp
In file included from src/../include/ClipperDraw.h:12:0,
                 from src/ClipperDraw.cpp:8:
/home/user/anaconda3/envs/deepjetLinux3/include/TMultiGraph.h: In member function ‘ClipperDraw& ClipperDraw::operator=(const ClipperDraw&)’:
/home/user/anaconda3/envs/deepjetLinux3/include/TMultiGraph.h:47:17: error: ‘TMultiGraph& TMultiGraph::operator=(const TMultiGraph&)’ is protected
    TMultiGraph& operator=(const TMultiGraph&);
                 ^
src/ClipperDraw.cpp:26:5: error: within this context
  mg = c.mg;
     ^
Makefile:19: recipe for target 'obj/ClipperDraw.o' failed
make: *** [obj/ClipperDraw.o] Error 1
1

There are 1 answers

8
NathanOliver On

The copy constructor and the copy assignment operator for TMultiGraph are both marked as protected. That means you cannot assign a TMultiGraph to another TMultiGraph. Inheritance won't help you as it doesn't change that fact.

What inheriting from TMultiGraph will do is allow to to make your own graph class that you can copy. That would look like

class MyMultiGraph : public TMultiGraph {
    //...
public:
    MyMultiGraph& operator =(const MyMultiGraph& rhs)
    {
        TMultiGraph::operator=(rhs);
        // assign MyMultiGraph member here
    }
};