|
led mike wrote: Well you better get busy telling the rest of the industry that because there is a common design pattern known as "copy on write" that calls for doing just that.
Copy-on-write creates a copy of the resource when you try to change it. Most of the time it is recommended to use a reference count when using COW to prevent both excessive copies and invalid memory accesses. The post you were responding to was correct, IN GENERAL, it is a bad idea to simply copy the addresses. There are special cases and techniques that allow for doing such without problems, but they are special cases, not the general case.
led mike wrote: At one time, maybe still... i don't know, CString used that design.
Not CString, the STL string class does this. CString has always had a private copy of the data (when you copy a CString, it actually copies the bytes to a new buffer). STL-string uses a reference count on the buffer and only creates a private buffer if you try to change it. This is also the reason that it is recommended to use vector<char> if you are dealing with passing strings between threads.
To answer the original post more thoroughly, a shallow copy is what is done for you if no copy constructor nor copy-assignment operators are declared. Basically, pointers in your objects are copied (that is, not the data they point to). A deep copy is something you have to write yourself by implementing the copy-constructor/assignment operator to actually allocate its own memory and copy the bytes from the source to the destination (that is, your new object). The following code segments are examples:
<br />
class Shallow<br />
{<br />
private:<br />
char* m_MyString;<br />
public:<br />
Shallow() { m_MyString = new char[20]; }<br />
~Shallow() { delete [] m_MyString; }<br />
};<br />
<br />
class Deep<br />
{<br />
private:<br />
char* m_MyString;<br />
public:<br />
Deep() { m_MyString = new char[20]; }<br />
Deep(const Deep& d) { memcpy(m_MyString, d.m_MyString); }<br />
~Deep() { delete [] m_MyString; }<br />
const Deep& operator= (const Deep& d) { memcpy(m_MyString, d.m_MyString); }<br />
};<br />
<br />
void main()<br />
{<br />
Shallow a, b;<br />
Deep c, d;<br />
<br />
a = b;
<br />
c = d;
}<br />
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
|
|
|
|
|
Zac Howland wrote: IN GENERAL, it is a bad idea to simply copy the addresses.
Not sure what your point is. It seemed to me that statement suggests that something is inherently wrong with a "shallow-copy" design. If that is what is intended then I disagree.
Zac Howland wrote: Not CString,
Still not sure on that but you can find all sorts of reference to CString implementing copy-on-write on the net. Also the VC6 Strcore.cpp has a CString member function called "CopyBeforeWrite" that is called in many places like MakeUpper()/Lower() and MakeReverse() to name a few. So at some point it seems someone at least thought CString used a copy-on-write design.
"What classes are you using ? You shouldn't call stuff if you have no idea what it does" Christian Graus in the C# forum
led mike
|
|
|
|
|
hi,
i used template class and in this class i used map eith function pointer and string with value and key respective.
with this map i find my function address that i saved previosly and call my function but in this code i have problem with find()..
find return iterator which is map.end() so loop is break ...
in that casse only two function pointer is working but after that it gives end of map..
so pls help to find out this problem..
also i attached my code pls go throught it .
// map of operation methods
typedef bool (CLbxMorphCompareImpl<tbaseclass>::*OperationMethod)(const MorphUtil::CParsedMorph *, const MorphUtil::CParsedMorph *, const std::vector<unicode::ustring> &, long);
typedef std::map<unicode::ustring, operationmethod="" ,="" stdext::equstr=""> OperationMap;
OperationMap m_mapOperations;
template < typename TBaseClass>
CLbxMorphCompareImpl< TBaseClass>::CLbxMorphCompareImpl()
{
unicode::ustring ustr1 = unicode::ustring::create_from_asciiz("agree");
m_mapOperations[ustr1] = &CLbxMorphCompareImpl<tbaseclass>::OperationAgree;
unicode::ustring ustr2 = unicode::ustring::create_from_asciiz("not-agree");
m_mapOperations[ustr2] = &CLbxMorphCompareImpl<tbaseclass>::OperationNotAgree;
unicode::ustring ustr3 = unicode::ustring::create_from_asciiz("strict-agree");
m_mapOperations[ustr3] = &CLbxMorphCompareImpl<tbaseclass>::OperationStrictAgree;
unicode::ustring ustr4 = unicode::ustring::create_from_asciiz("strict-not-agree");
m_mapOperations[ustr4] = &CLbxMorphCompareImpl<tbaseclass>::OperationStrictNotAgree;
}
template<typename tbaseclass="">
long CLbxMorphCompareImpl<tbaseclass>::CompareEx(uint16_t * bstrLeftReference, uint16_t * bstrRightReference, uint16_t * bstrOperation)
{
bool bRet = false;
//TYPENAME must required in template..
typename DataTypeStockImpl::CLbxMorphCompareImpl<tbaseclass>::OperationMap::const_iterator itMethod = m_mapOperations.find(strEachOperation);
}
thanx a lot.
rakesh baldha
Thanks & Regards
rakesh
|
|
|
|
|
post work and no work example
typename must required?
Kuphryn
|
|
|
|
|
BKhandelwal wrote: with this map i find my function address that i saved previosly and call my function but in this code i have problem with find()..
have look at simple example of MAP::find here :-
How to Find All Values of Key from MultiMap?[^]
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
I'm trying to output a double using the CString::Format member.
I'm trying to prevent trailing zeros on the right side but I want it to act like the type "f" where it does not switch to output like the "g,G" types where it uses "e" style formatting. I blew the dust off my old C books and dug though MSDN but I'm not sure if I can do that without formatting the string and then right trimming any zero's off.
Examples of desired output:
Number = 0.9873645876
Output = 0.987364
Number = 0.070000
Output = 0.07
Number = 1.7980
Output = 1.798
Does anyone remember if there is a way to do this without having to work it outside the format string?
Thanks either way!
|
|
|
|
|
i had the same problem with my VisualCalc[^] IDE. i finally decided to trim the trailing zeros manually, so i'd be curious to know if there is an automatic way to do it by formatting...
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
|
|
|
|
|
Thanks for the info.
Well here goes another trip into the ugly tree...
CString RightTrimDouble(double dfInput)
{
CString sTemp="";
sTemp.Format("%#f",dfInput); // Force the decimal
sTemp.TrimRight("0");
sTemp.TrimRight(".");
return sTemp;
}
Isn't it ugly? Not even a mother would be proud of that!
O'well. It happens
-- modified at 12:35 Tuesday 23rd May, 2006
|
|
|
|
|
don't do that, because if you have a pure integer (no decimal digits), you can alterate the value of the number.
see my CString CVisualCalcDlg::RemoveTrailingZeros(const CString& strResult) function to see how i do it...
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
|
|
|
|
|
The %#f in the format string always forces the decimal to be placed which prevents that. (At least that's what I intended) I was trying to protect against integer input.
Let me know if I'm still missing something obvious.
Either way, thanks for the heads up.
|
|
|
|
|
What's wrong with std::ostringstream?
CString str;
double mydbl = 0.17000;
std::ostringstream ss;
ss << mydbl;
str = ss.str().c_str();
MessageBox( str);
"What classes are you using ? You shouldn't call stuff if you have no idea what it does" Christian Graus in the C# forum
led mike
|
|
|
|
|
I'm still a C programmer thats been locked in a C/C++ Hybrid mode for quite a while and I haven't fully embraced 90% of what is C++ ish except for OOP of course. I haven't really gotten to know any of the C++ libraries as the C libraries had usually been sufficient (except today).
I keep meaning to learn the C++ standard library and templates and such but my little detour down MFC lane in the late 90's really messed with that intention.
It's embarrassing to say I've never seen "ostringstream" before (I've been a C programmer for 16 years and every day I feel like a newbie and another day behind the rest)
Thanks for the direction!
|
|
|
|
|
No need to be embarrassed, I do not know if anyone else here has been down the same road you are following, but I followed that exact same road. I recommend that you think of a personal project, preferably the development of a template that you can develop on your own at home. You just need to make the requirement that it is written entirely in standard C++ (no Microsoft extensions or dependency allowed), taking full advantage of the STL (Standard Template Library). Just pretend you are back writing code for DOS. I recommend template development because that requires a better understanding of C++, than non-template development.
One last note: MFC messed me up as well, since I was bypassing the basics of C++ and locking myself in that world. Get back to your roots, the way you learned C in the first place.
INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
|
|
|
|
|
Hi,
I have to read and write Windows NT data streams in my project. I have successfully read and write Alternate Data stream, Security stream,Encrypted stream, Quota stream, Reparse stream and Object Id stream. But not able to read or write Extended attribute data stream and Property data stream. Can any one tell me how i can read/write these streams in NTFS enviroment.
Touseef Afzal
"I only fail when i give up "
|
|
|
|
|
I'm not much of an MFC/C++ guy but I know C. I'd seen in some forum here, that we can mix MFC with Console apps. I dunno whether he meant console app to be C++ or not!
My qstn is how to use C in an MFC [exe] project. What extra things should I do to mix C inside so that I too can do some MFC stuff and porting some of my progs to GUI?
Thanks
|
|
|
|
|
blacksalt wrote: My qstn is how to use C in an MFC [exe] project
It depends what do you mean by "C". If you simply have source files (with C code) that you want to add to your project, then you can simply add them to the project and call the functions. Basically C++ is just an improvement of C.
Cédric Moonen
Software developer
Charting control
|
|
|
|
|
1. Include any external C source code files in the project, the same as including a C++ file. The compiler knows the difference between the two file types base on the file extension.
2. Enclose the prototypes for the C functions in the header file or anywhere that a C prototype is provided [in a C++ file], using the extern “C” declaration as follows:
extern "C"
{
void prn();
}
3. Now you are set to do what ever you want as far as MFC development is concerned.
There is one other possibility; copying all your C files along with their headers into the project folder and changing the extension to ‘.cpp’. I generally do not recommend this if you have other C programs that depend on the same sources, as it is usually better to avoid having multiple copies of the same sources. Then again doing this makes the sources more specific to the C++ project being developed.
Any way you get the general idea of how it works.
Oh, you will have to change any code that is dependent on user interaction via standard input/output or any other non-Windows method. The difficulty of that depends on how you wrote the original code to begin with.
Good Luck!
INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
|
|
|
|
|
OK I tried adding all C and .h files to the MFC project. There are many errors.
I'll change all the printf() calls to Afxmssg type. Now one of the main culprit is directory fns & variables. I think those also need to be replaced with MFC part. With code properly arranged out I'll get back to you.
As of now there are no calls from MFC code to any of the C fns. I understand that this has to be done within externC.
Thanx for the reply.
|
|
|
|
|
This is the first time I have been back since I posted my answer to you, but it appears you get the idea. Please do not use synonyms or Web acronyms when talking about coding, use the complete word so every one knows what you are talking about (assuming fns means function).
During the process I would be surprised if you did not receive many errors. Most of those should be related to I/O, always try to separate the I/O from the code that actually does the work as much as possible. Your ultimate goal is to write a solution (program) that has a back end (which does the work) and a front end (which show it to the user). If you do it right, then the back end will be portable (at least in theory).
INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
|
|
|
|
|
Please help me!!! Can any one tell me how to delete a line in text file. Please tell me how to do it in C (not MFC, C++)
Thanks...
If you have faith in the cause and
the means and in God, the hot
Sun will be cool for you.
|
|
|
|
|
You can't. What you have to do is read the file in memory, erase the line and write the complete file back. An alternative is to open a temporary file, write line by line in it (except for the line you want to remove), delete your original file and then rename your new file.
Cédric Moonen
Software developer
Charting control
|
|
|
|
|
I cannot take big data inside memory because I may have 1 Lakh records to be written to the file. I cannot also use temp file because I may to exchange data from original file to temp file alteast 50,000 times.
If you have faith in the cause and
the means and in God, the hot
Sun will be cool for you.
|
|
|
|
|
|
There is no way of doing that.
Mmmh, I think there is problem with your design: if you have to update files so many times, I don't think this is the right choice. Can't you use a database for that ?
What is the purpose of this file exactly ?
Cédric Moonen
Software developer
Charting control
|
|
|
|
|
Requirement is we have to query data from database and modify the recordsets and write the data into the text file.
If you have faith in the cause and
the means and in God, the hot
Sun will be cool for you.
|
|
|
|