|
George_George wrote: I am wondering the default implementation of assignment operator (e.g. when we do not implement assignment operator in user defined class, what will be returned? temporary object? reference or const reference?
A reference. (*this ).
George_George wrote: deep copy or shallow copy is used in default assignment operator?
Shallow copy.
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.
|
|
|
|
|
Thanks CPallini,
My question is answerd! Cool!
have a nice weekend,
George
|
|
|
|
|
George_George wrote: have a nice weekend
Thank you. Have a nice weekend you too.
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.
|
|
|
|
|
Hello everyone,
This is my understanding of non-const reference, const reference and their relationships with lvalue/rvalue. Please help to review whether it is correct and feel free to correct me. Thanks.
1. A const reference can be binded to a rvalue, for example, a temporary object. And the "life" of the temporary object is guaranteed to be extended and we can safely operate through the const-reference.
2. A non-const reference can not binded to a rvalue, I think the reason is rvalue is not addressable? And we can not change the rvalue through its reference? Are there any other reasons? I am not quite sure whether my understanding is fully correct. Since there are some non-modifiable lvalues (so we do not always need to modify values through its reference). I am still studying what is the reason in essence in compiler why a non-const reference can not be binded to a rvalue.
3. Both const and non-const reference can be binded to a lvalue.
thanks in advance,
George
|
|
|
|
|
I've just answered your question. If you remember, you asked this question yesterday.
I've admit, todays question is better phrased, but still...
Have *SOME* patience.
Iain.
|
|
|
|
|
I am sorry for any inconvenience, Iain! I am new to this topic.
have a nice weekend,
George
|
|
|
|
|
Don't worry - some of your questions are much less simple than they look, and can be fun to answer.
I've now gone through all my operator= and copy constructors and const'ed them! They were about 50/50 depending on my mood when writing. If I tell you I can't see any good reason for them not to be consted, and good ones why they should, then I must do it to my code too!
ps, did my long answer to the "previous" question help?
Iain.
|
|
|
|
|
Sure, Iain!
I have read it and I want to read it again after dinner to have some further think, which is the time I have a clear mind.
Sometimes, we are working on upper layer technologies, like XML and we may forget the basic building blocks' technologies, and this is what I am interested.
regards,
George
|
|
|
|
|
I'm having an issue when I attempt to do this:
testone.h:
#include "testtwo.h"
class testone
{
public:
testtwo* test;
}
testtwo.h:
#include "testone.h"
class testtwo
{
public:
testone* test;
}
There is a complaint about the pointer's type missing and the class definition is not valid. How would I be able to solve this problem?
I'm using Embedded Visual C++ 4. Is this a bug with C++ support?
-Steven Hicks CPACodeProjectAddict
|
|
|
|
|
Think about what you're written.
I'll define testtwo, which needs to know about testone, which needs to know about testtwo, which needs to know about testone, which needs to know about testtwo, which needs to know about testone, which needs to know about testtwo, which needs to know about testone, which needs to know about testtwo, which needs to know about testone, which needs to know about testtwo, which needs to know about testone, which needs to know about testtwo, which needs to know about testone, which needs to know about testtwo, which needs to know about testone, which needs to know about testtwo, which needs to know about testone, which needs to know about testtwo,.....
Ok, I'm fed up - and so was your compiler.
Which is probably which you use'd #pragma once, or
#ifndef TESTTWO_H
#define TESTTWO_H
class testtwo
... etc
#endif
But that just means...
I'll define testone, which needs about testone. So I'll include testone.h - which needs to know about testtwo. so, I'll include testone.h... which appears to be empty! whaaah!
So what you do is....
#ifndef TESTTWO_H
#define TESTTWO_H
class testone;
class testtwo
{
....
testone *one;
};
#endif
What we've done is tell the compiler "I'll get round to telling you all about testone later, but I'll tell you now that there is such a class."
And do vice versa in testone.h
Now you need to have both headers included into your .C file before you can actually DO anything useful with the pointer, but you've escaped your circular dependency!
Iain.
|
|
|
|
|
Hello all,
I am trying to track down the root cause of an out of memory error when using CString GetBuffer(). I appreciate any insight or constructive criticisms of poor methods.
The CString resides in an object of the "grp_vals" class:
class grp_vals{
public:
CStringA Name;
float Vals[MAX_VALS];
float& operator [] (int Idx){
return Vals[Idx];
}
};
The code where the problem arises is:
LPSTR p;
grp_vals *tmpvals;
...
tmpvals = new grp_vals;
...
p = tmpvals->Name.GetBuffer(32);//HERES WHERE THE PROBLEM STARTS
fgets(p,32,FileHandle);
tmpvals->Name.ReleaseBuffer();
tmpvals->Name.Remove('\n');
This code "works" for many iterations but eventually the error occurs and was traced to the "if( pNewData == NULL )" below (from the standard atlsimpstr.h)...sure enough, it's NULL but I'm not sure why only in this one case... the pOldData does not *seem* to vary from previous iterations that "worked".
ATL_NOINLINE void Fork( __in int nLength )
{
CStringData* pOldData = GetData();
int nOldLength = pOldData->nDataLength;
CStringData* pNewData = pOldData->pStringMgr->Clone()->Allocate( nLength, sizeof( XCHAR ) );
if( pNewData == NULL )
{
ThrowMemoryException();
}
....
modified on Friday, December 14, 2007 9:38:20 PM
|
|
|
|
|
My understanding from the MSDN docs on ReleaseBuffer() is that the memory that "p" points to is freed (invalidated), but when running in debug I can see that this is not the case. Is it possible that I am filling up memory by not explicitly freeing the memory that "p" points to before assigning it to a new string via GetBuffer()? Or is this a side-effect of running in debug mode?
|
|
|
|
|
Okay... the pointer is to the CString data, not a copy of it, so back to square one...
Am I the only one with nothing better to do on this fine Friday night?
|
|
|
|
|
How are you handling string length?
If the data you read into the string buffer is NULL terminated, you're OK.
If the data is not NULL terminated, then you should be specifying the actual
string length when you call ReleaseBuffer().
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks,
Fortunately it was as simple as replacing '\n' with '\0' in "p" before ReleaseBuffer(). Unfortunately, my problem has just moved somwhere else, which I'm guessing means that I have other memory leak issues to deal with...
That's ok, I'm sure it builds character or something like that...
|
|
|
|
|
nadiric wrote: That's ok, I'm sure it builds character or something like that...
I like to think so
Cheers,
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I have written several Windows applications in C++, and spent time and care on the necessary accompanying helps (---.HLP). I was thus very annoyed to find that Windows Vista has dropped its old help system and replaced it by an incompatible new help system. Please where is software (ownloadable or able to be bought) that can make help files for this new help system?
Why did Windows Vista drop the old help system?
|
|
|
|
|
Hi,
I Don`t know why they removed this.
But using HTML WorkShop You can make *.chm help file.
Create an ID in the Accelerator, then Create an ON_COMMAND(ID, Memfunc())
Call the *.chm help file using ::ShellExecute();
Thanks and Regards.
SANTHOSH V
|
|
|
|
|
Sorry: I am very new to Visual C++: please how do I find the Accelerator, and how do I create an ID in it?
|
|
|
|
|
(vs2003)
choose Resources from View menu
rightclick yout project in resource panel
choose add resource->accelerator
|
|
|
|
|
Hi,
In Resourse View, you can see Dialogs, Icons, Cursors, etc..
There in the Resource Heading right click and select Insert. One Dialog will open there you have to select Accelerator, its a Table having some IDs, Keys and Type.
Give a New ID like IDD_APPHELP, Select Key(F1) from the Combo Box, Selet Type as VIRKEY.
Then use ON_COMMAND(IDD_APPHELP, OnAppHelp())
void CMyApp::OnAppHelp()
{
::ShellExecute( HelpFilename);
}
///
//In the Header add this line.
afx_msg void OnAppHelp();
Thanks and Regards.
SANTHOSH V
|
|
|
|
|
They actually dropped the old help system years ago. Support for it was kept around for
a long time to give us a chance to convert
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
can someone pls help me!!
I am tring to diplay all file in a directory into a text box, but i am getting nothing i dont know what i am doing wrong..I mae a new project and got it to display a txt box, and tried to display fils in it but didnt work
void PlayerDlg::DoDataExchange(CDataExchange* pDX)
{
DDX_Text(pDX, IDC_AUDIOLIST,m_AudioList);
}
BOOL PlayerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
CFileFind finder;
bool bFound;
CListBox *m_AudioList = (CListBox*)this;
static const TCHAR szFileToFine[] = _T("d:\\songs\\*.mp3");
bFound = finder.FindFile(szFileToFine);
if(bFound) {
bFound = finder.FindNextFile();
m_AudioList->AddString(finder.GetFileName());
while(bFound) {
bFound = finder.FindNextFile();
if(bFound) {
m_AudioList-> AddString(finder.GetFileName());
}
}}}
please tell me where am going wrong pls....
Gpat
|
|
|
|
|
void PlayerDlg::DoDataExchange(CDataExchange* pDX)
{
DDX_Text(pDX, IDC_AUDIOLIST,m_AudioList ); <-- what is m_AudioList?? Calling DDX_Text()? Why?
}
BOOL PlayerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
CFileFind finder;
bool bFound;
CListBox *m_AudioList = (CListBox*)this; <-- Casting a dialog pointer to a CListBox pointer...bad
static const TCHAR szFileToFine[] = _T("d:\\songs\\*.mp3");
bFound = finder.FindFile(szFileToFine);
if(bFound) {
bFound = finder.FindNextFile();
m_AudioList->AddString(finder.GetFileName());
while(bFound) {
bFound = finder.FindNextFile();
if(bFound) {
m_AudioList-> AddString(finder.GetFileName());
}
}}}
Maybe try making m_AudioList a member variable of the dialog class:
CListBox m_AudioList;
Then use it something like this:
void PlayerDlg::DoDataExchange(CDataExchange* pDX)
{
DDX_Control(pDX, IDC_AUDIOLIST, m_AudioList);
}
BOOL PlayerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
static const TCHAR szFileToFine[] = _T("d:\\songs\\*.mp3");
CFileFind finder;
bool bFound = finder.FindFile(szFileToFine);
while (bFound)
{
bFound = finder.FindNextFile();
m_AudioList->AddString(finder.GetFileName());
}
}
Mark Salsbery
Microsoft MVP - Visual C++
modified on Friday, December 14, 2007 6:10:47 PM
|
|
|
|
|
GPat24 wrote: CListBox *m_AudioList = (CListBox*)this;
It has problem.
GPat24 wrote: DDX_Text(pDX, IDC_AUDIOLIST,m_AudioList);
And also this line.
How did you make listbox control why you dont declare a variable for it and use of it?
|
|
|
|