|
Dear all, I'm still struggling to display the book and borrower's details together.
Could someone help me?
regards,
grscot
|
|
|
|
|
I developed a simple object swap template. I simply do a binary swap of the class/structure contents. Yet I know that the developers of the STL libraries do not implement swaps in this way. Their implementation will make copies (via a copy constructor) and then use an assignment operator - "operator=()". The reason I want to do swaps in this manner is because of the obvious efficiencies. For instance, if I wanted to swap two branches of two very large tree structures, the copy and assignment operations become prohibative.
My question is in what instances does the code below NOT work?
template <typename _Ty>
void ObjectSwap(_Ty &obj1, _Ty &obj2)
{
// Don't swap on the same object
if (&obj1 != &obj2)
{
char tmp[sizeof(_Ty)]; // create a memory buffer
size_t sz = sizeof(_Ty);
memcpy((void *) tmp, (const void *) &obj1, sz);
memcpy((void *) &obj1, (const void *) &obj2, sz);
memcpy((void *) &obj2, (const void *) tmp, sz);
}
}
Scott K
|
|
|
|
|
I could see this working but it could lead to unexpected results if there are other pointers that point to the same object.
I'm confused though: why not just swap the pointers to the objects instead of the objects themselves? If you want to swap two branches of a large tree structure, you simply swap the pointers that point to those branches.
|
|
|
|
|
If an object contains one or more pointers to other objects, a simple bit-wise copy may not be what you want. Copy constructors and assignment operators let you do a so-called deep copy that addresses this. For example, suppose you have the following class:
class Stuff {
Stuff();
~Stuff();
int number;
char *string;
};
Stuff::Stuff()
: number(0),
string(new char[10])
{
strcpy(string,"Stuff");
}
Stuff::~Stuff()
{
delete []string;
} A bit-wise copy of one Stuff object to another simply copies the pointer to the string member. If one of the objects is then deleted, the other object's string member now points to freed memory. With a deep copy, you can ensure that each instance of the Stuff object has its own copy of the string:
class Stuff {
Stuff();
Stuff(const Stuff &stuff);
~Stuff();
int number;
char *string;
};
Stuff::Stuff()
: number(0),
string(new char[10])
{
}
Stuff::Stuff(const Stuff &stuff)
: number(stuff.number),
string(new char[10])
{
strcpy(string,stuff.string);
}
Stuff::~Stuff()
{
delete []string;
}
Software Zen: delete this;
|
|
|
|
|
Thanks, but my question was about swaping the contents of two objects. You are talking about copying an object - very different things. Using your example here is how you would manually swap the contents of two objects:
<br />
class Stuff <br />
{ <br />
Stuff(); <br />
~Stuff();<br />
void swap(Stuff &rhs):
<br />
int number;<br />
char *string;<br />
};<br />
<br />
Stuff::Stuff() : <br />
number(0),<br />
string(new char[10])<br />
{<br />
strcpy(string,"Stuff");<br />
}<br />
<br />
Stuff::~Stuff()<br />
{<br />
delete []string;<br />
}<br />
<br />
void swap(Stuff &rhs)<br />
{<br />
int tmpNumber = number;<br />
number = rhs.number;<br />
rhs.number = tmpNumber;<br />
<br />
char *tmpString = string;<br />
string = rhs.string;<br />
rhs.string = tmpString;<br />
}<br />
This will work 100% of the time if the object can guarantee that it manages the allocation and destruction of the string memory.
The code will obviuosly fail if the user somehow was able to assign stack allocated memory to the string pointer. In that case the Stuff destructor would try to delete a reference to stack memory - very bad thing to do.
My question then boils down to why would not the following swap code not work?
<br />
void swap(Stuff &rhs)<br />
{<br />
char tmp[sizeof(*this)];<br />
memcpy((void *)tmp, (void *)this, sizeof(*this));<br />
memcpy((void *)this, (void *)&rhs, sizeof(*this));<br />
memcpy((void *)&rhs, (void *)tmp, sizeof(*this));<br />
}<br />
|
|
|
|
|
Really what you are asking is, "Why can't you just change the memory location of an object?"
The answer is that other objects may be referencing it. If you use memcpy, it is not possible for the affected pointers to be fixed, like they could if you used copy constructors and/or assignment operators.
A linked-linked is a good example. If you swap nodes using memcpy, the list would become corrupted. Doing it the slow and safe way gives the objects the chance to reinsert themselves into the list correctly.
|
|
|
|
|
I have some(not actually few) html files that contain forms.I have a task to accomplish in this.
The task is, i get a string (typed by user while filling the form) and need to do a database lookup (through ODBC) and apply some algorithm(right now it is a dll written in VC++) to convert the data to the required form. The data typed by the user should go through this stage before getting submitted to the server.
What I want now is, I am struggling to find the correct way of doing it. Can it be an ActiveX control in VC? Then I need to sign it before using. Can I create a ATL HTML Control? But can other part of html call the function of this control (as like ActiveX)? I need to learn more abt that 'window.external', i suppose.
And what else can I do?
Thanx in advance,
Mahesh
|
|
|
|
|
I would be inclined to write an ISAPI DLL. That assumes, of course, that you have access to the server to put such a file in place.
|
|
|
|
|
Thanx for ur interest, DavidCrow,
The ISAPI DLL will be running in Server. right? For every string, (every field of form) it has to goto server and get it done. (U may argue that I need database look-up and its not possible to have that in every client. But this application is for Intranet and i am planning for Oracle server and all the clients connecting through the ODBC) If u assume there is no databse look-up, what would u be inclined to do?
Once again, Thank u so much,
Mahesh
|
|
|
|
|
Mahesh Perumal wrote:
The ISAPI DLL will be running in Server. right?
An ISAPI filter is a DLL that runs on an ISAPI-enabled HTTP server to filter data traveling to and from the server. The filter registers for notification of events, such as logging on or URL mapping. When the selected events occur, the filter is called, and you can monitor and change the data (on its way from the server to the client or vice versa). ISAPI filters can be used to provide enhanced logging of HTTP requests (for example, to track who is logging on to your server), custom encryption, custom compression, or additional authentication methods.
|
|
|
|
|
Thanx DavidCrow,
I thought something like "http://myserver/isapilib.dll?str='fieldvalue'" to get the output string.
You say that, without changing anything in the html, I can write my isapi dll that will catch all the strings and convert when the form is submitted. right? It seems good in the way that it doesn't require me to change the htmls. I look more into this and get back to u with the progress.
Thank u so much,
Mahesh
|
|
|
|
|
PLEASE EXCUSE MY BAD SPELLING IF THERE IS ANY
I am using a MFC application that is dialog based and a staticaly linked library (if I dont make it statically linked and a sherd DLL insted wene I transfer it to another computer and try to run the exe it has an error saying it is missing mfc70d.dll)
I wont to use #include <fstream.h> so I can use the variable typs ifstream and ofstream as file pointers but if I include fstream the build log will give me the following error's
NJROTC error LNK2005: "void __cdecl operator delete(void *,int,char const *,int)" (??3@YAXPAXHPBDH@Z) already defined in nafxcwd.lib(afxmem.obj)
NJROTC error LNK2005: "void __cdecl operator delete(void *,int,char const *,int)" (??3@YAXPAXHPBDH@Z) already defined in nafxcwd.lib(afxmem.obj)
NJROTC fatal error LNK1169: one or more multiply defined symbols found
I know the include fstream will werk with a sherd DLL but I have to get it to werk with a statically linked librery
CAN ANYONE HELP ME PLEASE
|
|
|
|
|
This looks like a problem with the libraries linked in the wrong order.
You need to force them to be linked in the right order by entering them in the linker "ignore" field, then entering them in the correct order in the "object/library modules" field. I think you should have nafxcw.lib first.
jhaga
|
|
|
|
|
Check the the code creation type matches the libraries being used. ie. If you are using multithreaded debug DLL's then you code must generate this as well. In VC6 this Code Generation in the Project Setup from memory.
Neville Franks, Author of ED for Windows. www.getsoft.com
Make money with our new Affilate program
|
|
|
|
|
I do not know if this will help.
When you transfer the program to another computer are you also installing the mfc70d.dll on that computer? If you are not, assuming it is not alread installed, then that explanes why you have to staticaly link the library. To use a non-staticaly linked .DLL libraray: You need to either create an istall program that not only installs your program but also installs the .dll. If you want to do a manual install then you will have to copy the mfc70d.dll into the System32 directory (if it does not already exist) before you run your program.
You may also need to install any other .DLL files that are not already on that other computer. The only thing I am not sure of is whether you will need to restart that other computer after installing the .DLL files.
I use InstallSheild to create installation programs, but I have not heard any thing bad about Wyse so either one should be very good for your purposes. There might even be a good shareware program out there that you can use.
Trust in the code Luke. Yea right!
|
|
|
|
|
Hope that anyone can help me. I am new to socket() programming. I try to connect to a tcp server. However my program fails when its get to gethostbyaddr() function. It keeps returning "null". My questions are:
1) Is there a difference between AF_INET and PF_INET?
2) The gethostbyaddr() returns "null" because it can't not see the tcp server IP on the network for some reason ?(eventhough I can ping or telnet to tcp server)
Many thanks!
peter.
unsigned int addr;
struct hostent *hp;
SOCKET conn;
conn=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
addr=inet_addr("10.23.50.133");
hp=gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);
|
|
|
|
|
well here are a couple of things:
1) Make sure you have initialized the winsock library. if you haven't that function would also return null. It sounds trivial but i have forgotten this many a time just to find out it was some simple mistake such as not loading the library.
Load it like this:
int nRet = 0;
WSADATA wsd;
memset( &wsd, 0, sizeof( WSADATA ) );
nRet = WSAStartup( MAKEWORD( 2, 2 ), &wsd );
2) There is a difference between AF_INET and PF_INET even though they are numerically the same. You should not rely on that fact because one day those numbers may change. The PF is a protocol family and the AF is an address family.
3) If all else fails you can have a look at this. I wrote this code awhile ago for my own library and use it frequently so i know it works.
BOOL ConnectServ( SOCKET* pSock, LPCSTR sServ, LPCSTR sPort, BOOL bOverlapped )
{
BOOL bConn = FALSE;
ADDRINFO* pAI = NULL;
int nRet = 0;
DWORD dwFlags = 0;
if( ! pSock ) {
return FALSE;
}
if( getaddrinfo( sServ, sPort, NULL, &pAI ) != 0 ) {
return FALSE;
}
if( bOverlapped ) {
dwFlags = WSA_FLAG_OVERLAPPED;
}
*pSock = WSASocket( PF_INET, SOCK_STREAM, 0, NULL, 0, dwFlags );
if( IsValidSocket( *pSock ) ) {
nRet = WSAConnect( *pSock, pAI->ai_addr, ( int )pAI->ai_addrlen, NULL, NULL, NULL, NULL );
if( nRet == SOCKET_ERROR ) {
CloseConn( *pSock );
}else{
bConn = TRUE;
}
}
freeaddrinfo( pAI );
Good luck.
Joseph Dempsey
joseph_r_dempsey@yahoo.com
"Software Engineering is a race between the programmers, trying to make bigger and better fool-proof software, and the universe trying to make bigger fools. So far the Universe in winning."
--anonymous
|
|
|
|
|
Hi, everyone!
I want to use VC together with Matlab to do some digital signal processing projects. They have something to do with basic field specific theory. Just concept proof projects.
So I want to know where can I find some materials about this topic? Forums? Maillist? Better some VC or Matlab programs of this field.
Can anyone help?
regards,
George
|
|
|
|
|
hi
i want to use three radio buttons i.e radio1, radio2 and radio3 in a group box. As i click radio1 and then i press a button, a message passed in this method AfxMessageBox("hello"); must be appear, plz tell me the logic.
|
|
|
|
|
please make more clear what you want
|
|
|
|
|
CButton::GetCheck
will give you the state of the radio button.
BN_CLICKED Notification will tell you when a button has been clicked.
Michael
'War is at best barbarism...Its glory is all moonshine. It is only those who have neither fired a shot nor heard the shrieks and groans of the wounded who cry aloud for blood, more vengeance, more desolation. War is hell.' - General William Sherman, 1879
|
|
|
|
|
yes handle the BN_CLICKED message with something like this:
void CMyClass::OnClickMyButton() //just example
{
if ( ((CButton*)GetDlgItem(IDC_RADIO1))->GetCheck() )
{
AfxMessageBox("radio1 checked");
} else
{
//check radio button 2 & 3
}
}
|
|
|
|
|
Hi friends,
i want to convert from bmp to 32 bit Gif file, any body help me
|
|
|
|
|
there is no such thing as a "32 bit GIF"
-c
|
|
|
|
|
I currently have visual c++ 6.0 pro, but I now need to use visual basic as well, i was wondering if i purchase the visual studio .net pro 2003 upgrade pack, would i get c++ .net, and vb .net?
|
|
|
|
|