|
In c++ can an abstract base class have member variables apart from virtual functions ?
or
In c++ can an Interface have member variables apart from virtual functions ?
sorry if a stupid question.
thanks for your time
|
|
|
|
|
yes and no - in fact interface is just a nickname for struct . Therefore you can easily add members and non-virtual methods to the interface.
Problem is that then it's no interface anymore.
|
|
|
|
|
Unlike Java and C# these terms tend to have less meaning in C++, where the rules are far more flexible. The language designers of C++ aim was (and still is) 'you don't pay for what you don't use'. and in doing so they discovered many of the potential programmer pitfalls which more modern languages seek to avoid.
The answers to both questions is yes, you can do what you like - although most programmers now follow some general guidelines when preparing these types of classes.
A C++ abstract base class is any class that cannot be instantiated due to having one or more pure virtual functions. Some developers make the choice to flag this in other ways as well by making constructors and/or destructors private.
Standard C++ has no concept of an interface, however java-like interfaces can be implemented in two ways. One is through the use of a class with only virtual functions, the second is an ugly mess of templates which you generally need a code generator to assist with - this was described in a recent article of the C/C++ Users Journal see Heronfront for more information. Interfaces tend to provide a single concrete function (a virtual destructor), whilst avoiding data.
The presence of member variables for classes in these situations can have an impact on a number of factors, mainly to do with language implementation details (class sizes and v-tables). These issues are largely irrelevant, provided you aren't doing weird casts on classes and/or using multiple inheritance.
If you can keep you head when all about you
Are losing theirs and blaming it on you;
If you can dream - and not make dreams your master;
If you can think - and not make thoughts your aim;
Yours is the Earth and everything that's in it.
Rudyard Kipling
|
|
|
|
|
Hi there
I would like some help with this plz if posible.
I need to write the following class book and it's implementation, this is what i have done.
class book<br />
{<br />
public:<br />
<br />
book(const string title,const author,const char isbn[10]);<br />
<br />
void show_book();<br />
<br />
private:<br />
<br />
const string T;<br />
const string A;<br />
const char I;<br />
<br />
};<br />
Ok,the library class must be able to add books. some other stuff aswell.
class library<br />
{<br />
public:<br />
library()<br />
<br />
void add_book();<br />
void display()const;
<br />
private:<br />
<br />
vector<book *> book_list;<br />
<br />
};
The library class implementation
<br />
void library::add_book()<br />
{<br />
book * new_book = new book("Danger","Mike","102- RT34");<br />
<br />
book_list.push_back(new_book);<br />
}<br />
<br />
void library::display() const<br />
{<br />
<br />
for(int i = 0; i < book_list.size(); i++)<br />
{<br />
cout << book_list[i]->show_book() << "\n";<br />
}<br />
<br />
}<br />
First is there any thing wrong with this code?
Secondly is there a beter way of writing this code?
Thirdly how would i go about adding books at runtime and
iterate thru the vector to find a specific book?
excuse the english, Thanks in advance
I Never said you should like me.I say be yourself, and make a difference
|
|
|
|
|
the code looks ok, except maybe for the display method; the show_book method will do what ? no, it is declared as void , and cout will do nothing for it; or might not compile.
to find a book, you need to compare somthing, when iterating the list, you can compare one it with the search criteria.
for example ( pseudo code )
<br />
CString sCriteriaTitle;<br />
for(int i = 0; i < book_list.size(); i++)<br />
{<br />
if ( sCriteriaTitle.Compare( book_list[i]->get_book_title() ) == 0)<br />
{<br />
bFound = true;<br />
}<br />
}<br />
<br />
}<br />
Maximilien Lincourt
Your Head A Splode - Strong Bad
|
|
|
|
|
A Class is just a structure. Can youm write a structure with all these elements?
"Naked we come and bruised we go."
- James Douglas Morrison
Best Wishes,
ez_way
|
|
|
|
|
Once upon a time there was a thread:
UINT CIrisAdqDlg::CaptureImageThread(LPVOID pParam)
{
...
}
And one day I tried to launch it:
void CIrisAdqDlg::OnStart()
{
...
AfxBeginThread(CIrisAdqDlg::CaptureImageThread, this);
...
}
But VC++ didn't want:
...
error C2665: 'AfxBeginThread' : none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)'
...
I am sure it worked in the past, so I don't know what it's really happening now.
Thanks!
|
|
|
|
|
Hi
This occurs when you use Member functions as the parameter of AfxBeginThread.
Write the Thread function outside the class.
This http://www.codeproject.com/win32/callback_adapter.asp[^] may be of help to you.
Regards
The Best Relligion is Science.
Once you understand it, you will know God.
|
|
|
|
|
Or make the member function static.
If I write code in my sleep, does that make me brilliant, or just a lazy programmer?
My articles
www.stillwaterexpress.com
BlackDice - the programmer formerly known as bdiamond
|
|
|
|
|
Hi, I have a dll that runs as a process via dllHost.
My problem is i need to know when a Windows Shutdown event is being fired. I've never had this problem before because all my apps have a main window and receive this notification. However the dll just keeps working and terminates in an undetermined state.
I can't have the dll called from an app. I have no alternative but to use what i've got.
how can i register to receive a WM_SHUTDOWN event from my dll?
Please don't suggest i change the design of the solution. I'm confined to what i have.
thanks in advance
Carl
|
|
|
|
|
What about a creating an invisible hidden window for just receiving this message (under W2K+ you can use message only windows designed specially for these purposes - see HWND_MESSAGE for details)
|
|
|
|
|
thanks for the comment but firstly this needs to be a solution to all current windows platforms (excluding servers) and secondly i'm sure there must be a way to register for this event, even in a dll. hmmm
Regards
Carl
|
|
|
|
|
Yes instead though create a custom message
In both headers:
#define MY_MESSAGE (WM_USER+22)
Then trap it in defwinpro
"Naked we come and bruised we go."
- James Douglas Morrison
Best Wishes,
ez_way
|
|
|
|
|
I only have one binary, the dll. Hence only one header. I am running the dll as an application, through another process.
|
|
|
|
|
OK sorry, did your try force parm?
BOOL x = ExitWindowsEx(EWX_FORCE,NULL);
"Naked we come and bruised we go."
- James Douglas Morrison
Best Wishes,
ez_way
|
|
|
|
|
Hi
I started a thread to check for a file change using the below code
CWinThread* m_UpdateMonitorThread = AfxBeginThread(CheckFileUpdates,(LPVOID)this)
UINT CheckFileUpdates(LPVOID lp)
{
while( ReadDirectoryChangesW(
hDir, // handle to directory
&Buffer, // read results buffer
sizeof(Buffer), // length of buffer
TRUE, // monitoring option
FILE_NOTIFY_CHANGE_SIZE, // filter conditions
&BytesReturned, // bytes returned
NULL, // overlapped buffer
NULL// completion routine
))
{
//do the work
}
}
Now on I have abutton on my UI. Onclick of the button I want to stop the above thread.
I I tried to use
if(m_UpdateMonitorThread)
{
DWORD dwRetCode;
while (!GetExitCodeThread(m_UpdateMonitorThread->m_hThread, &dwRetCode));
TerminateThread(m_UpdateMonitorThread->m_hThread,dwRetCode);
m_UpdateMonitorThread = NULL;
}
and
if(m_UpdateMonitorThread)
{
m_UpdateMonitorThread->SuspendThread();
m_UpdateMonitorThread->Delete();
m_UpdateMonitorThread = NULL;
}
I am getting memory leaks in both the above cases. I tried to post a Stop Event, but where do I wait for this event inside the thread code as execution will not go inside the while loop code because of ReadDirectoryChangesW.
Can anyone please please suggest a way out to this problem.
Thanks in anticipation of information.
Madhavi
|
|
|
|
|
TerminateThread is guaranteed to leak every sort of resources the thread might have acquired. Do not use it ever for finalizing the thread, except in extreme circumstances.
So, try setting a stop event. In order to be able to handle this event while doing the ReadDirectoryChangesW stuff, you must perform the latter in overlapped mode (check the docs for ReadDirectoryChangesW ): basically, the op is done asynchronously, and its completion is signalled through an event, so you can fire the operation and wait in one single WaitForMultipleObjects for both completion or the stopping event, whatever comes first.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
Hi Joaquín
Thanks for your quick response. I am unable to use the overlapped mode. I mean I am not getting the correct usage syntax.
I simply used stop event as shown below.
UINT CheckFileUpdates(LPVOID lp)
{
//Get the document pointer
CLogViewerAppView* pView = (CLogViewerAppView*)lp;
CLogViewerAppDoc* pDoc = pView->GetDocument();
CFileException err;
CFileStatus st;
CFile::GetStatus(pView->m_LogFilePath,st);
LONG l1,l2;
CFile file;
if( !file.Open(pView->m_LogFilePath, CFile::modeRead|CFile::shareDenyNone, &err ) )
{
TCHAR szCause[255];
err.GetErrorMessage(szCause, 255);
AfxMessageBox(szCause);
return 0;
}
l1 = (LONG)file.GetLength();
file.Close();
CFile file1;
LONG size_1 = (LONG)st.m_size;
int len = pView->m_LogFilePath.GetLength()-pView->m_LogFileName.GetLength()-1;
CString dir = pView->m_LogFilePath.Left(len);
HANDLE hDir = CreateFile( dir, // pointer to the file name
FILE_LIST_DIRECTORY, // access (read/write) mode
FILE_SHARE_READ|FILE_SHARE_WRITE, // share mode
NULL, // security descriptor
OPEN_EXISTING, // how to create
FILE_FLAG_BACKUP_SEMANTICS, // file attributes
NULL // file with attributes to copy
);
FILE_NOTIFY_INFORMATION Buffer[1024];
DWORD BytesReturned;
while( ReadDirectoryChangesW(
hDir, // handle to directory
&Buffer, // read results buffer
sizeof(Buffer), // length of buffer
TRUE, // monitoring option
FILE_NOTIFY_CHANGE_SIZE, // filter conditions
&BytesReturned, // bytes returned
NULL, // overlapped buffer
NULL// completion routine
))
{
if ( WaitForSingleObject(pView->m_StopEvent, INFINITE) == WAIT_OBJECT_0 )
{
OutputDebugString(_T("\n KIIGSRETG \n"));
return 0; //end the thraed
}
CString fileName(Buffer[0].FileName);
fileName = fileName.Left(Buffer[0].FileNameLength / 2);
if(fileName.CompareNoCase(pView->m_LogFileName) == 0)
{
//Do the work
}
}
return 0;
}
But the m_StopEvent event is not caught beacuse the control is stuck at while( ReadDirectoryChangesW) I mean it not going inside the while loop unless ReadDirectoryChangesW returns true. Hence WaitForSingleObject is not checked at all.. Hope you got my point.
Can you please let me know how to write this using OVERLAPPED structure. I never did this before. I am really stuck at this point..unable to proceed further..this is very important for my assignment.
Thanks
Madhavi
|
|
|
|
|
Good Day.
Please, can you give me an example how can I change state of the button with some id? I do like this:
m_wndReBar.Create(this);
m_wndReBar.AddBar(&m_wndToolBar);
...
m_wndToolBar.SendMessage(TB_SETSTATE, ID, MAKELONG(TBSTATE_ENABLED, 0));
...
But nothing changes. What's wrong? Or what another ways exists to change the state?
Nulla dies sine linea !!!
|
|
|
|
|
And if I have a button in ReBar how can I change state of it?
Nulla dies sine linea !!!
|
|
|
|
|
To change the state of button on DialogBar I have used
for Disable
m_wndToolBar.GetDlgItem(IDC_BUTTON)->EnableWindow(FALSE);
for Enable
m_wndToolBar.GetDlgItem(IDC_BUTTON)->EnableWindow(TRUE);
For unPressed
m_wndToolBar.CheckDlgButton(IDC_BUTTON,BST_UNCHECKED);
For Pressed
m_wndToolBar.CheckDlgButton(IDC_BUTTON,BST_CHECKED);
I thing it will also work for Rebars
|
|
|
|
|
I am building an application; Along with the main SDI window it has a modeless dialog, SDI has system menu but the modeless dialog has no system menu icon; (upper left part of task bar), although I have selected system menu and taskbar options in the dialog properties.
How to add system menu / icon in a modeless dialog?
Please Help.
|
|
|
|
|
Is it a child and is that selected?
"Naked we come and bruised we go."
- James Douglas Morrison
Best Wishes,
ez_way
|
|
|
|
|
Thanks for reply;
I have created dialog in the OnCreate of CMainFrame like this;
mydlg.Create(IDD_MYDLG, this);
In the properties tab the style of dialog is POPUP not child;
making it child donot displays it.
|
|
|
|
|
Hi,
I want to execute the function for evry 3 minuts.How can I call the event multiple times in this regard?
Please help me.
dadsadasd
|
|
|
|