|
Hi,
I've created a class containing a virtual function. Using pointer, I want to replace virtual function with a global function, but it gives memory access violation. So my question is that
whether it is possible to replace virtual function with global function?
How can we achieve this?
Please note that I am new to C++ and using MS VC++.
Here is the code...
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
typedef void(*Fun)();
void f(void *pThis)
{
printf("other function\n");
}
class Test {
public:
virtual void fn() {
cout << "I m virtaul fn" << endl;
}
virtual ~Test() {}
};
int main() {
Test obj;
Fun pFun = (Fun)*(int*)*(int*)(&obj);
pFun();
//replacing virtual function.
int *pInt = (int*)&obj;
*(int*)*pInt = (int)f; // gives memory voilation
pFun = (Fun)(int*)*(int*)*(int*)(&obj);
pFun();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
Thank you
|
|
|
|
|
Why do you want to do something like that ? It doesn't make any sense...
|
|
|
|
|
If you want to use global functions, then why you got hang up with virtual functions.
Global function and virtual functions are two different entities in programming, what the need do such a replacement.
Just define more so that we can understand what really want to achieve.
Величие не Бога может быть недооценена.
|
|
|
|
|
Hi,
First of all, I don't have any specific need. This is for the sake of curiosity. I just want to know - is it possible? Can we do in this way? If yes, then how? My code gives access violation, however, I am replacing correct address with correct address. What is the problem?
|
|
|
|
|
One of your casts is likely failing and returning a null pointer. Improper casts create difficult to track down memory errors in large systems.
//For instance C++ will let you cast an integer to an enum.
enum_X x=(enum_X) y;
If the value cannot be casted, you will get a memory error. You are lucky if you actually get an error message so that you can see the problem.
You should not try casting types where the conversion is not defined unless you want to spend many unnecessary hours debugging.
|
|
|
|
|
You cannot replace a virtual function in code like this, as it has the potential to compromise your app and so you are prevented from doing so. If you want a function pointer you need to create a global or local one and use that as necessary. I don't know what you are trying to achieve in your main() function but the code looks a total mess; I'm surprised it even compiles.
|
|
|
|
|
abhay4u wrote: Please note that I am new to C++ and using MS VC++
Then take your time to familiarize with them, and please note, the aim of programming is not fooling the compiler...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
It's not possible if the compiler/linker are correctly putting vtables into sections marked as read-only.
I suppose Detours[^] might be able to do something. But why on earth do you want to do such a thing?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Thank you very much guys. I got the answer.
|
|
|
|
|
Hello everybody !
Now I want to add the proxy module in a dialog based program,just like the Microsoft's MSN,you can set the network environment.I search it anywhere,but never konw how to set username and password :
I used stuct INTERNET_PROXY_INFO, could set the IP Address and Port:
INTERNET_PROXY_INFO proxy;
proxy.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
proxy.lpszProxy = m_strIPAndPort;
proxy.lpszProxyBypass = NULL;
But in the MSDN, there is no more information about the parameter of "lpszProxyBypass":
How to use the parameter "lpszProxyByPass" to set the proxy username and password ?
Thanks for your reply !
Best Reguard !
|
|
|
|
|
Hello Friends
I m using getSaveFilename on some button and I am also using OPENFILENAME structure also to provide InitialDirectory.
And same thing doing on some other button too.
and both are having different Initial Directories.
Now, the Problem is when I open save dialog through first button,it is opening in right folder using ofn.lpstrinitialdir.
But If i open second save dialog after opening of first one then it is not opening in its own folder ,it is opening the directory tht i used to open first save dialog.I checked when I open second dialog the value of of.lpstrinitialdir is right but it is taking fist dialog value only.
Please Suggest me something.
Thanks & Regards
Yogesh
|
|
|
|
|
It looks like a new 'improved' (!?! ) behaviour introduced with Windows 7 .
From MSDN [^]:
lpstrInitialDir
Pointer to a NULL terminated string that can specify the initial directory. The algorithm for selecting the initial directory varies on different platforms.
Windows 7:
1. If lpstrInitialDir has the same value as was passed the first time the application used an Open or Save As dialog box, the path most recently selected by the user is used as the initial directory.
2. Otherwise, if lpstrFile contains a path, that path is the initial directory.
3. Otherwise, if lpstrInitialDir is not NULL, it specifies the initial directory.
4. If lpstrInitialDir is NULL and the current directory contains any files of the specified filter types, the initial directory is the current directory.
5. Otherwise, the initial directory is the personal files directory of the current user.
6. Otherwise, the initial directory is the Desktop folder.
i.e. No luck!
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
it is a known "special feature" aka Bug.
SCNR
Press F1 for help or google it.
Greetings from Germany
|
|
|
|
|
|
What do you mean exactly by converting ? What do you have to convert to text file ?? Your question is totally unclear, please rephrase it properly.
Or do you mean that your wav file is in fact somebody speaking and you have to convert that into text ?
|
|
|
|
|
|
eswar pothula wrote: in my application if any person recorded voice sending fax directly.
This doesn't make any sense...
Anyway, if I more or less understood what you are trying to achieve, you have a wav file containing a speech from somebody. And you would like your program to transform this speech into text.
Well honnestly, this is far from being a trivial task, it's not like you call one function and it is done. There are some "Text-to-speech" libraries but I don't know for this opposite, because I think it is far more complex. Your best option is to google for "speech to text" library and pray to find something usefull.
|
|
|
|
|
eswar pothula wrote: i have convert wav file to text file.
What?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Probably yet another "Liquid Nitrogen" candidate
|
|
|
|
|
|
|
Hi ,
I am using the below code for retrieving the length of a file. I have list of file names in a CStringArray object.
bool Search(CString szBinName,CString szBinSize,CStringArray& arr)
{
int nSize = arr.GetSize();
CString str,temp;
bool bRet;
bRet = false;
for(int i=0;i
|
|
|
|
|
I believe CFile::shareExclusive|CFile::shareDenyNone don't go together.
|
|
|
|
|
I have tried putting only one of the CFile::shareExclusive|CFile::shareDenyNone , still it crashes.
|
|
|
|
|
What does ex.GetErrorMessage() give you?
|
|
|
|