1

I was trying to overload assignment operator for this Class. How to do this for class containing Structures and enumerators?

class Config
{
public:
    Config() { SetDefaults(); }
    Config(const std::string& path);

    enum FeatureType
    {
        kFeatureTypeHaar,
        kFeatureTypeRaw,
    };

    enum KernelType
    {
        kKernelTypeLinear,
        kKernelTypeGaussian };

    struct FeatureKernelPair
    {
        FeatureType feature;
        KernelType kernel;
        std::vector<double> params;
    };

bool quietMode;    
std::string sequenceBasePath
int frameHeight;
std::vector<FeatureKernelPair>  features;

friend std::ostream& operator<< (std::ostream& out, const Config& conf);

private:
    void SetDefaults();
    static std::string FeatureName(FeatureType f);
    static std::string KernelName(KernelType k);
};

This is what i had tried. This is the general way to do it,right..?

Config & operator=(Config const&c) {
    if(this != &c){
            quietMode = c.quietMode;
            sequenceBasePath = c.sequenceBasePath;
            frameHeight = c.frameHeight;
            features = c.features;
        }
    return *this;

}
MrWalker
  • 11
  • 1
  • Have you ever heard of the "[copy swap idiom](https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom)"? – Cory Kramer Aug 11 '15 at 19:53

1 Answers1

0

This is one way to do it (assuming that you've defined the operator as member).

The copy swap idom suggested by CoryKramer in the comments is certainly a better approach (as long as you both have a copy operator and define a swap function).

Nevertheless, in your specific case, the Config class contains only members which already have an copy-assignement operator. So you don't need to provide your own: the default one generated by your compiler will organise a member by member copy.

Christophe
  • 54,708
  • 5
  • 52
  • 107