|
Actually, you are not using virtual functions for vFunc. You are simply re-defining the base class function. To use virtual functions for this, you would need to modify your code to:
class CBadGuy {
public:
CBadGuy();
virtual ~CBadGuy();
virtual void vfunc() { cout << "Base class vfunc.\n"; };
protected:
unsigned int speed;
unsigned int height;
char* description;
};
class CSoldier : public CBadGuy {
public:
CSoldier();
virtual ~CSoldier();
virtual void vfunc() { cout << "Derived class vfunc.\n"; }
protected:
unsigned int numBullets;
};
int main() {
CBadGuy a;
CSoldier b;
a.vfunc();
b.vfunc();
return 0;
}
Even with this change you would get 'Base class vFunc' followed by 'Derived class vFunc'.
Often when using virtual functions it is desirable to not just "overide" a functions functionality, but to augment it. By doing this you can still use the base class implementation and only add what you want.
The following example demonstrates this.
class CBadGuy {
public:
CBadGuy();
virtual ~CBadGuy();
virtual void vfunc() { cout << "Base class vfunc.\n"; };
protected:
unsigned int speed;
unsigned int height;
char* description;
};
class CSoldier : public CBadGuy {
public:
CSoldier();
virtual ~CSoldier();
virtual void vfunc() { cout << "Derived class vfunc.\n"; CBadGuy::vFunc();}
protected:
unsigned int numBullets;
};
int main() {
CSoldier b;
b.vfunc();
return 0;
}
This will result in 'Derived class vFunc' followed by 'Base class vFunc'. This can be very useful if you are creating your objects at run-time (ie new CSoldier) and passing them as pointers to CBadGuy); The following example demonstrated this.
class CBadGuy {
public:
CBadGuy();
virtual ~CBadGuy();
virtual void vfunc() { cout << "Base class vfunc.\n"; };
protected:
unsigned int speed;
unsigned int height;
char* description;
};
class CSoldier : public CBadGuy {
public:
CSoldier();
virtual ~CSoldier();
virtual void vfunc() { cout << "Derived class vfunc.\n"; }
protected:
unsigned int numBullets;
};
int main() {
CSoldier* pSolider = new CSoldier;
TestObject(pSoldier);
return 0;
}
void TestObject(CBadGuy* pTest)
{
pTest->vFunc();
}
In this scenario you would get the 'Dervice class vFunc' as output. This can be extremely powerful when you have a "class" of objects which share basic functionality and purpose but need to work in different ways.
|
|
|
|
|
virtual functions are useful when we use base class pointer to access derived class objects.
class CBadGuy {
public:
CBadGuy(){};
virtual ~CBadGuy(){};
virtual void vfunc() { cout << "Base class vfunc.\n"; };
protected:
unsigned int speed;
unsigned int height;
char* description;
};
class CSoldier : public CBadGuy {
public:
CSoldier(){};
virtual ~CSoldier(){};
virtual void vfunc() { cout << "Derived class vfunc.\n"; }
protected:
unsigned int numBullets;
};
int main() {
CBadGuy *pBadGuy;
CSoldier b;
pBadGuy = &b;
pBadGuy->vfunc(); //This will call the derived class vfunc()
//If the vfunc() is not declared as virtual in the base class,
//the above call will call the base class vfunc() function.
|
|
|
|
|
Hi
I have a CListctrl & Iam intersted in creating a tooltip in the form f a PopUp window when I select any item in the CListCtrl . Can anybody explain how I can do this ?? Any Sample code would be very useful.
The ClistCtrl is placed in a dialog box. When I try to implement the OnLButtonDown to know whether the left button of mouse is clicked inside the control, it doesn't respond but actually, the control is being trapped by the parent window ( Dialog Box ). Eventually the HitTest member function of the CListCtrl fails.
Thanks
regards
Sankar
|
|
|
|
|
Handle LVN_ITEMCHANGING (or LVN_ITEMCHANGED) to be notified before (after) the selection changes.
--Mike--
http://home.inreach.com/mdunn/
#include "buffy_sig"
|
|
|
|
|
I have the following code, which looks fine but every time the connection fails... the database is an Access 2000 database in the same directory as the VC project:
void CTest9Dlg::OnButton1()
{
_ConnectionPtr m_pConnection = NULL;
char* m_strConnection = _T("Provider=Microsoft.Jet.OLEDB.4.0; Data Source = db1.mdb");
m_pConnection.CreateInstance(__uuidof(Connection));
try
{
HRESULT hr;
hr = m_pConnection->Open(_bstr_t(m_strConnection), "", "", NULL);
if(hr == S_OK) {
TRACE("Connected");
MessageBox("Connected");
}
}
catch(_com_error &e)
{
TRACE("Connect Failed.");
MessageBox("Failed!");
}
}
Thnaks for your help
|
|
|
|
|
The 2 most common mistakes I make is to not call 'OleInitialize(NULL)' (or some variant ie. AfxOleInit()). Also, is the MDB file in your project directory of your build directory (ie. Debug/Release). If is is your project directory you may need to reference the DB like
char* m_strConnection = _T("Provider=Microsoft.Jet.OLEDB.4.0; Data Source = ../db1.mdb");
|
|
|
|
|
I'm working on an application (let's call it SCONES) which can be used as an OLE server, so that its results can be embedded in Word docs, for example. Now, I know I need to implement the Serialize() function for all my data objects for OLE purposes. On the other hand I don't want to use MS's proprietary file format for saving SCONES files to disk when it's not being an OLE server - I want to use XML. Do I have to write two separate sets of code for file saving and loading, or is there some smart way of doing both tasks in one? I can't be the first to encounter this problem, surely?
Peter Robinson
|
|
|
|
|
Hello Dear.
I have windows 98. I'm trying to execute a chatter application which is avaialbe in MSDN. But it is displaying the message & not executing. Message is giving below:
"This Application or Dll can not be loaded on windows 95 or on windows 3.1. It takes advantages of unicode features only available on windows NT."
When I press OK button, it display an other error message "The MFC42UD.DLL file cannot start. Check the file to determine the problem."
When I press ok on this button it terminates my application.
Pls replty me soon. I'll be thankful to you.
|
|
|
|
|
Can't tell you much more than they are - this program requires Unicode, and therefore requires Windows NT or 2000. It won't run under W98.
Christian
#include "std_disclaimer.h"
People who love sausage and respect the law should never watch either one being made.
The things that come to those who wait are usually the things left by those who got there first.
|
|
|
|
|
i want to store key value pairs and access them ,
i want a return type of function to be CMap or something like it,
but this is not possible, = operator not overloaded
is there any alternative ?
parag
|
|
|
|
|
The STL map collection has assignment operation and copy constructor, so you can return a std::map from a function.
|
|
|
|
|
It doesn't make sense to return map by value - it involves creating temporary object and this operation may be costly depending on the size of your map. You should pass the map object by reference:
void foo(CMyMapClass & map);
Tomasz Sowinski -- http://www.shooltz.com.pl
|
|
|
|
|
Why this code doing me troubles ???????????
----------------------------------------------
void CFilesDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CString str(50);
str = "22223333333333222";
CFile file;
if(file.Open("string.data",file.modeWrite|file.modeNoTruncate|file.modeCreate))
file.Write(&str,sizeof(str));
}
void CFilesDlg::OnButton2()
{
// TODO: Add your control notification handler code here
CString str(50);
CFile file;
if(file.Open("string.data",file.modeRead))
file.Read(&str,sizeof(str));
MessageBox(str);
}
---------------------------------------------------------
mimi
|
|
|
|
|
You need to tell us what's going wrong. Compile errors? Not giving the right output when you run it?
--Mike--
http://home.inreach.com/mdunn/
#include "buffy_sig"
|
|
|
|
|
1/ I don't see any CString constructors that take a number
2/ you want to use str.GetLength() instead of sizeof(str).
3/ If the 50 was meant to be a size, you put less than 50 characters into the buffer before writing.
Christian
#include "std_disclaimer.h"
People who love sausage and respect the law should never watch either one being made.
The things that come to those who wait are usually the things left by those who got there first.
|
|
|
|
|
I changed little my code but the problem remained !
It is ok in compilation but write nonsense in the file and same in the messageBox.
Why ??????
--------------------------------------------------------------------------
void CFilesDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CString str("222233772");
CFile file;
if(file.Open("string.data",file.modeWrite|file.modeNoTruncate|file.modeCreate))
file.Write(&str,str.GetLength());
}
///////////////////////////////////////////////////////////
void CFilesDlg::OnButton2()
{
// TODO: Add your control notification handler code here
char str[50];
CFile file;
if(file.Open("string.data",file.modeRead))
file.Read(str,10);
MessageBox(str);
}
---------------------------------------------------------------------
|
|
|
|
|
void CFilesDlg::OnButton1()
{
CString str(50);
str = "22223333333333222"; There's no such constructor for CString. Use
CString str("22223333333333222");
instead
CFile file;
if(file.Open("string.data",file.modeWrite|file.modeNoTruncate|file.modeCreate))
file.Write(&str,sizeof(str));
&str doesn't return a pointer to the characters contained in the CString, and sizeof(str) isn't the string length. To write the contents of the CString instead of pointers, reference counts and other useless information:
file.Write(str, str.GetLength());
}
void CFilesDlg::OnButton2()
{
CString str(50); Same error as before, use
CString str;
CFile file;
if(file.Open("string.data",file.modeRead))
file.Read(&str,sizeof(str));
What you need here is:
{
int len = file.Read(str.GetBuffer(50), 50);
str.ReleaseBuffer(len);
}
MessageBox(str);
}
|
|
|
|
|
now it is ok !
you are explain excellentand very clear.
thank you!
|
|
|
|
|
&str is your problem.
just use str.
CString has a (char *) overload
|
|
|
|
|
Hello
Here is a situation where in i need to display the gif's and jpgs in VC++ MFC application
Need some help regarding this
regadrs
Dwaraka
|
|
|
|
|
There are articles on the site on how to do this if all you need is to show them. GDI+ lets you load and save them, and manipulate them - so does paintlib ( www.paintlib.de )
Christian
#include "std_disclaimer.h"
People who love sausage and respect the law should never watch either one being made.
The things that come to those who wait are usually the things left by those who got there first.
|
|
|
|
|
|
Hi,
I have to work on a project related to xemacs. Now my code loads some dll's from within xemacs and my prof wants me to unload them before xemacs is shut.
So I have to find out exactly which part of the code is executed just before exit, and I dont really know how to do this.
Can anyone tell me if there are any tips to follow when trying to understand the code of a big project with loads of files? And more specifically can anyone tell me which code (also which file) is executed just before exit?
Thanks in advance.
Mayank.
PS - I am using xemacs-21.1.4, source can be found at
http://ftp.xemacs.org/xemacs-21.1/xemacs-21.1.4.tar.gz
|
|
|
|
|
I have a problem in refreshing the dialogbar.
What I did:
1)
A standard procedure to add a dialog bar in a simple single document program
as follows (in CMainFrame::OnCreate(...))
CDialogBar m_wndMyDialogBar;
if (!m_wndMyDialogBar.Create(this, CG_IDD_MYDIALOGBAR,
CBRS_RIGHT | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_HIDE_INPLACE,
CG_ID_VIEW_MYDIALOGBAR))
{
TRACE0("Failed to create dialog bar m_wndMyDialogBar\n");
return -1; // fail to create
}
2)
When added following code to enable dialog bar docking:
m_wndMyDialogBar.EnableDocking(CBRS_ALIGN_RIGHT | CBRS_ALIGN_LEFT);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndMyDialogBar);
there appears another control bar, an "AfxControlBar" on top of the dialog
bar, which can be seen easily using Spy++.
Problem
When I want to add some other controls, like a popup menu inside the dialog
bar, the covered area will not refresh (or InvalidateRect) properly. Using
Spy++ I found out that it is not the area in the "dialog", but the one in
the unexplanable "AfxControlBar", that does not refresh properly, because
this AfxControlBar does not receive any message except "WM_SETCURSOR".
When I delete the code in part 2), there'll be no AfxControlBar covering my
dialog bar, and hence no such problem, but also no docking feature
available.
My understanding in MFC is very shallow. Can anyone explain to me about
this? Thanks a lot!
sunny
|
|
|
|
|
Hello
Can I use CreateFiber function on Win2K? The Microsoft documentation says yes, but I can't use it, because I get "undeclared identifier" error but I don't know why? I naturally use the "windows.h" include file.
Thanks
|
|
|
|