Hi all,

I am creating an application that includes and calls upon someone else's code. I need to slightly change the functionality of a constructor in their code but the class I need to change is buried away very deep. I considered inheriting from the class so I could change the constructor but in order to redirect to the derived class I would need to inherit from another 10 classes or so and change their constructors as the offending class is not called directly from my program.

I have simply redefined the offending constructor in my cpp file, this is working fine but is it safe?
What should I do to make it safe? Bearing in mind that I cannot edit the offending code directly as it has already been distributed.

Thanks,
Andy

Edited 5 Years Ago by Colezy: n/a

I have simply redefined the offending constructor in my cpp file, this is working fine but is it safe?

No.

What should I do to make it safe? Bearing in mind that I cannot edit the offending code directly as it has already been distributed.

Don't try to pretend that you can change existing code without changing it.

In other words, if you want to change what it does, you have to change it, compile it, and distribute the new version--preferably under a different name so that it is not confused with the original.

The only exception to this rule is if the original author took into consideration that someone might want to change part of the code's behavior, and made provisions for doing so. One common method of making such provisions is to use inheritance; but there are others.

Just contact the author/distributer of the library and make your case for adding your functionality or fix to the code. If it doesn't change the interface and has good justification for the addition, the author will probably be happy to add it.

Otherwise, use an ad-hoc fix, because as arkoenig said, this is not safe or maintainable.

Amongst ad-hoc solutions, there are a few things that can be done. First, you can wrap the class in another one (not inheritance, but composition). This would not be possible if you need the modification to access protected members of the class, or if you must also have many other classes derived from this class to include the new functionality. In that case, if the class in question is a class template, you can use a dummy wrapper class for the template argument and specialize the class template for that argument. For example, I could reimplement the constructor of std::vector using a dummy allocator class:

template <typename Allocator>
class wrapper_dummy : public Allocator { };

namespace std {
  template <typename T, typename Allocator>
  class vector<T, wrapper_dummy<Allocator> > : public vector<T, Allocator> {
    public:
      vector() { 
        //add functionality here.
      };
  };

};

The above might not work as is, but that's the basic idea of it. Specializing templates is one classic way to hack your code in, legally and safely. But, this only works if the class is already a class template and if it is possible to specialize it without too much trouble.

The last-resort, before actually copying the entire code and making a new copy with your modifications, would be to copy only the one source file you need to modify, modify it, compile it, and re-link it to the main static/shared library (leaving out the original object file from the linking). This is somewhat of a maintainability nightmare (and probably won't be that easy to make it work either), but it's doable.

Hi, thanks for the ideas.
The authors won't make the changes as I wish to remove functionality as is is not needed in my program and will cause odd results if used.
The package I'm including is ROOT by CERN, it is already installed on the client machines and other apps are using the libraries as well. The source is freely available at root.CERN.ch if you are interested
Its not a template class and I will need to access private members so it looks like I am going to have to inherit to create a simplified version of the class.

Thanks for your help.
Andy

This article has been dead for over six months. Start a new discussion instead.