|
Lets say I have a class with quite a few public members, each time one of these functions is called I need to check a variable (by calling another function) before I can allow the code to proceed. Obviously I could insert the function call into each public function, but is there an alternative? What I'm worried about is somebody subclassing and forgetting to add the call into their extended function.
|
|
|
|
|
Private functions
A cynic is a man who, when he smells flowers, looks around for a coffin. -H.L. Mencken
|
|
|
|
|
hey thats a great idea, I will declare all of the class functions as private, that will stop those hackers from subclassing my class. One small drawback though, it won't work.
|
|
|
|
|
waldermort wrote: hey thats a great idea, I will declare all of the class functions as private, that will stop those hackers from subclassing my class. One small drawback though, it won't work.
JWood's answer was too short. He probably meant something like:
class Base {
public:
void doSomething() {
checkVar();
doSomethingImp();
}
private:
virtual void doSomethingImp() {
}
};
class Derived : public Base {
public:
private:
virtual void doSomethingImp() {
}
}
-- modified at 17:43 Thursday 7th September, 2006
see also: http://www.gotw.ca/publications/mill18.htm[^]
|
|
|
|
|
This probably isn't what wanted either. Just in case, if you coded in managed C++, you could use "sealed" modifier to a public function which you don't want to be derived from.
Best,
Jun
|
|
|
|
|
Are these public members variables or functions?
If these are functions, you can use something like the Pimpl-Idiom to hide the implementation a bit, so that if they subclass it, they won't have access to the "meat" of the object. For example:
class _MyClass;
class MyClass
{
public:
MyClass();
~MyClass();
void someFunc();
private:
_MyClass* m_pImpl;
};
class _MyClass
{
public:
_MyClass()
{
}
void someFunc()
{
}
};
MyClass::MyClass() : m_pImpl(new _MyClass)
{
}
MyClass::~MyClass()
{
delete m_pImpl;
}
void MyClass::someFunc()
{
m_pImpl->someFunc();
}
That way, deriving from MyClass would allow them to call the MyClass public/protected methods, but have no access to the actual data and can't circumvent (at least not easily) any requirements you are trying to place on the class.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
That's an excellent idea, I must remember this method. But in my current instance, I have simply added the function call to each function. If anybody does decide to override the methods, they are just going to have to read the docs.
|
|
|
|
|
Hello all,
can somenone explain to me the mean of
preprocessor directive: #pragma pack( pop, packing ) ?
Moreover can you tell me what the header pshpack1.h helps for?
thanx a lot
|
|
|
|
|
Did you see here?
"Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
it pops packing directives off the stak until it finds one that matches the value of 'packing'. the MSDN's page on 'pragma pack' might help.
|
|
|
|
|
|
Hello,
I'm working to define a toolbar. I would like to modify the handle.
I have drawn a picture, put it inside the toolbar as handle but I could manage the picture as a handle. I Used a CStatic and then loaded a Bitmap into it.
Does somebody know how to get the picture ID to after allocate it as handle ?
titi
titi
|
|
|
|
|
I surprised myself, just coded the whole project in an ANSI environment, switched to unicode and only found 3 errors. Anyway, I have a _tprintf() call, which is returning -1. GetLastError() is not being set either, so I haven't a clue why it's not working.
TCHAR tmp[5];
if (i < 10)
_tprintf(tmp,_T("_00%d"),i);
else if (i < 100)
_tprintf(tmp,_T("_0%d"),i);
else
_tprintf(tmp,_T("_%d"),i);
_tcscat(newNames[i],tmp);
It works fine in an ANSI build.
|
|
|
|
|
First of all, have you tried doing this instead:
_tprintf(tmp, _T("_%03d"), i);
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
yes,
TCHAR tmp[10];
memset(tmp,0,10);
if (i < 10)
_tprintf(tmp,_T("_%03d"),i);
Adding the memset has stopped it from returning -1, now returns 0. I have also tried removing all formating, but with no effect.
wprintf(tmp,L"Hello");
It just doesn't want to work
|
|
|
|
|
My bad, I was using the wrong function
_stprintf(tmp,_T("_00%i"),i);
|
|
|
|
|
Do'h! I should have caught that one, too. But you are still not using the formatting that the printf-family gives you. If you want 3 digit base-10 numbers with 0's prepended, use %03d.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Oh, I never realised that one before, thanks for the tip
|
|
|
|
|
StrCchPrintf[^]
"Alot of the people on this forum are incredibly stupid, thinking that the internet is real" Score: 1.0 in the Soap Box
led mike
|
|
|
|
|
hi
i want to delete files past a certain date
for example if the file is older than a certain number of days(nDays) remove it
im using the below, but it doesnt seem to be working
can anyone see whats wrong ??
thanks
Si
WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile (_T (sPath+sExt), &fd);
CString name = fd.cFileName;
CTime tmCreate(fd.ftCreationTime);
CTime ctNow = CTime::GetCurrentTime();
if(tmCreate < (CTime::GetCurrentTime() - CTimeSpan(nDays,0,0,0)))
{
CString strFileName = sPath+"\\"+ (CString)fd.cFileName;
if(::DeleteFile(strFileName))
}
|
|
|
|
|
si_69 wrote: can anyone see whats wrong ??
Probably someone stepping through that in debug mode would be able to see whats wrong
"Alot of the people on this forum are incredibly stupid, thinking that the internet is real" Score: 1.0 in the Soap Box
led mike
|
|
|
|
|
si_69 wrote: im using the below, but it doesnt seem to be working
which part isn't working ?
|
|
|
|
|
Not sure if this applies in your case, but according to MSDN:
In rare cases, file attribute information on NTFS file systems may not be current at the time you call this function. To obtain the current NTFS file attributes, call GetFileInformationByHandle.
Karl - WK5M
PP-ASEL-IA (N43CS)
<kmedcalf@ev1.net>
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
|
|
|
|
|
Chris Losinger wrote: which part isn't working ?
the if statement.
the date of the file i wish to delete is 2/5/2005
which gives tmCreate {m_time=1157539835 }
and i want to delete files which are older than 11 days olds which gives me a time of {m_time=1156699696 }
so the if statement reads
if(tmCreate < (CTime::GetCurrentTime() - CTimeSpan(nDays,0,0,0)))
i.e {m_time=1157539835 } < {m_time=1156699696 }
but how can i file older have a m_time value higher than the time of 11 days ago perhaps thats where its going wrong
any ideas ?
|
|
|
|
|
si_69 wrote: the date of the file i wish to delete is 2/5/2005
which gives tmCreate {m_time=1157539835 }
No, it doesn't. It gives you: 1107590400
-----------------------------
Get trial copy of FeinViewer - an integrated GDI objects viewer for Microsoft Visual Studio .NET 2005 (8.0) Visual C++ Debugger, and other convenience and productivity tools for Microsoft Visual Studio .NET at at www.FeinSoftware.com
|
|
|
|