Answer the question
In order to leave comments, you need to log in
What is the error in the source code, and is there one?
Hello. There is a simple source code:
class TLibrary{
HINSTANCE hModule;
TLibrary( const TLibrary &){}
TLibrary(){}
public:
TLibrary(const char *LibName){
if( ( hModule = LoadLibrary(LibName) ) == NULL )
throw Exception("Can't load library");
}
virtual FARPROC getProcAddress(const char *ProcName){
FARPROC ptr = GetProcAddress(hModule, ProcName);
if( ptr == NULL )
throw Exception("Can't find func");
return ptr;
}
virtual ~TLibrary(){
FreeLibrary(hModule);
}
};
http://cppcheck.sourceforge.net/cgi-bin/democlient.cgi
Cppcheck 1.84
[test.cpp:3]: (warning, inconclusive) Member variable 'TLibrary::hModule' is not initialized in the constructor.
[test.cpp:6]: (style) Class 'TLibrary' has a constructor with 1 argument that is not explicit. Such constructors should in general be explicit for type safety reasons. Using the explicit keyword in the constructor means some mistakes when using the class can be avoided.
[test.cpp:1]: (warning) The class 'TLibrary' has 'copy constructor' but lack of 'operator='.
Done!
Answer the question
In order to leave comments, you need to log in
I understand that this is C ++ (it would be better to put it in the tags).
Judging by the code, you just want to hide a couple of constructors. To do this, it is enough to declare them in the private part of the class, but it is not necessary to define them. Change it to this and you'll be happy:
TLibrary( const TLibrary &);
TLibrary();
[test.cpp:3]: (warning, inconclusive) Member variable 'TLibrary::hModule' is not initialized in the constructor.
Depends on how this class will be used. But there are potentially errors:
1. When copying a class object, the original object and the copy will have the same hModule value.
And if double freeing can and will work relatively painlessly, then there is no function call for the freed handle.
2. Exception from the constructor. It will not lead to anything bad here, but potentially when expanding the functionality and as an approach in general, it is very bad
3. The fact that the class field can potentially be uninitialized is also understandable.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question