简体   繁体   中英

Extending a class, delegating operators and assignment?

In a nutshell... in my app we use boost::filesystem::path a whole lot. It mostly works very well, EXCEPT if somebody decides to be cute and refer to a non-unicode filename in windows (say, for some reason I cannot fathom, somebody has a Shift-JIS filename).

As the say, ignorance is bliss and in mine, I thought I might be able to get around this by doing something along the lines of (Does this even make sense BTW?):

namespace fs = boost::filesystem;

class utf8Path : public fs::path {

    public:
    utf8Path () : fs::path () {};
    utf8Path (std::string path) : fs::path(UnicodeUtil::convertToUTF8(path)) {};
}

Of course, I wasn't taking into account all the various assignment and such operators.

Supposing what I wrote above makes sense and is not broken code... is it possible to extend this approach to other versions of the constructor, assignment operators and such?

It is usually a bad design choice to inherit when composition is equally good or when you don't override any virtual methods. In the provided example, it hardly justifies a new class in the first place. Simply call the conversion in the places where it is needed instead of using the 'utf8path'.

Although im not familiar with this, there appears to be some support for codec_cvt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM