Browse DevX
Sign up for e-mail newsletters from DevX


Modify Your Base Class Interface in Derived Classes-3 : Page 3




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Name Injection
To fix this problem, you have to explicitly inject the name File::Open into the scope of NetworkFile::Open(). This way, any additional function called Open() that you declare in NetworkFile won't hide File::Open. To do so, use a using-declaration like this:

class NetworkFile : public DiskFile { public: using File::Open; //inject a name into current scope int Open(const wstring & path); //.. };

When you add a using-declaration of this kind, make sure that it appears in the public section of the class. Now you can use both overloaded versions of Open() in the derived class:

string path="\\usr\\image.bmp"; wstring wpath=L"\\usr\\image.bmp"; NetworkFile nf, nf2; nf.Open(path); //OK, File::Open(const string &) nf2.Open(wpath); //OK, NetworkFile::Open(const wstring &)

You may add as many overloaded versions of Open() as you like to NetworkFile:

class NetworkFile : public DiskFile { public: using File:Open; //inject name into current scope int Open(const wstring & path); int Open (const char * path); int Open (const wchar_t * path); //.. };

Likewise, any class that inherits from NetworkFile can use the same technique to add even more overloaded functions:

class UnixNetworkFile : public NetworkFile { public: using NetworkFile:Open; //inject all overloaded versions int Open(int descriptor); //.. }; UnixNetworkFile nf1, nf2, nf3; nf.Open(path); nf.Open(wpath); nf.Open(2); //opens stderr

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date