|
Michael Dunn wrote: I'm working on my psychic debugging powers
Hey Mike, I have a bug. How can I fix it?
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
|
|
|
Hi all,
my question is if I define a class myClass with destructor, assign operator (=) and copy constructor, etc etc.
If I later use my class like this:
myClass &a=*new myClass(whatever);
myClass &b=a;
Do I have to explicitly delete b? I've read you should only delete a class if you have instantiated it with a new. I know that behind the curtains b is created with a new call inside the dll, but do I have to delete it explicitly from my app?
thanks
|
|
|
|
|
gumi_r@msn.com wrote: myClass &a=*new myClass(whatever);
myClass &b=a;
since you've dynamically allocated memory for an istance of myClass , then you have to delete it somewhere. You can call either delete &b or delete &a (of course don't call both).
Cheers
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.
|
|
|
|
|
Ok thanks. That makes sense as I'm already deleting any pointers I need to inside my copy constructor.
Next step would be the assignment operator. Say for example y have:
myClass &a=*new myClass(whatever);
myClass &b=*new myClass(whatever);
myClass &c=a+b;
do I have to delete &c?
thanks for any replies.
|
|
|
|
|
gumi_r@msn.com wrote: myClass &c=a+b;
IMHO you should not do this, because you're trying to assign a reference to a temporary object. For instance, the following is noy allowed(the compiler complains about):
int &i= *new int(5);
int &j=i;
int &k=i+j;
BTW: Doing some experimentation?
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.
|
|
|
|
|
It is not illegal if you use a const reference.
Steve
|
|
|
|
|
Yeah, I didn't know it. Thank you (though seems rather clumsy...).
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.
|
|
|
|
|
I would like to give a command from inside my program to do a File New command.
I tried pApp->CWinApp::OnFileNew() but it tells me I can't access a protected member.
Fair enough, but is there some system command I can give instead?
Thanks,
Ilan
|
|
|
|
|
Try this:
::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), ID_FILE_OPEN, 0, 0);
or even
AfxGetMainWnd()-> PostMessage(ID_FILE_OPEN, 0, 0);
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Up front it looks like a good idea, but I put in break points and it doesn't reach them. So, I'm still stuck on the proper way to do it.
|
|
|
|
|
You're going to have to give more detail if you want help. WHERE did you put the breakpoints?
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Sorry, I didn't realize you continued to look at my posts.
I have break points at both OnNewDocument and OnOpenDocument
<br />
<br />
BOOL CCardiacDoc::OnNewDocument()<br />
{<br />
char buff[100];<br />
if( m_dataDirectory.GetLength() == 0 && _getcwd(buff, 98)) {<br />
m_dataDirectory = buff;<br />
m_dataDirectory += "\\*";<br />
}<br />
if (!CDocument::OnNewDocument())<br />
return FALSE;<br />
<br />
<br />
return TRUE;<br />
}<br />
<br />
BOOL CCardiacDoc::OnOpenDocument(LPCTSTR lpszPathName)<br />
{<br />
m_dataDirectory = lpszPathName;<br />
int j = m_dataDirectory.ReverseFind('\\');<br />
if( j > 0) {<br />
m_dataDirectory = m_dataDirectory.Left(j+1) + "*";<br />
}<br />
if (!CDocument::OnOpenDocument(lpszPathName))<br />
return FALSE;<br />
<br />
<br />
return TRUE;<br />
}<br />
I see that when I start the program it goes to OnNewDocument. Likewise, before I override the OnOpen, it goes to OnOpenDocument. The function I use is:
<br />
void CCardiacDoc::OnFileOpen()<br />
{<br />
CString path1;<br />
int flags;<br />
CFileDialog fdlg( true, NULL, "dummy entry");<br />
fdlg.m_ofn.lpstrTitle = "Choose directory";<br />
flags = fdlg.m_ofn.Flags;<br />
fdlg.m_ofn.Flags = flags;<br />
if( fdlg.DoModal() == IDOK)<br />
{<br />
path1 = fdlg.m_ofn.lpstrFile;<br />
int i = path1.GetLength();<br />
i -= (int) strlen(fdlg.m_ofn.lpstrFileTitle);<br />
m_dataDirectory = path1.Left(i) + "*";<br />
AfxGetMainWnd()->PostMessage(ID_FILE_NEW, 0, 0);<br />
}<br />
}<br />
It never again reaches OnNewDocument. If I would use ID_FILE_OPEN (which I tried), I would expect it to reach my overridden routine, which it doesn't. I really want it to go to OnNewDocument which I why I assume you had a small typo.
Thanks,
Ilan
|
|
|
|
|
Well see? That's completely different than what you originally asked.
In OnOpenFile(), instead of posting the ID_FILE_OPEN message, try this line instead:
OpenDocumentFile(path1);
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
I didn't explain the whole problem. I don't want to open the file the user gives, just to use the directory he gives (because it actually opens a whole bunch of files, not just a single file).
The problem is, what happens when the user "opens" the same file twice? (i.e. he points to the same directory and picks the same file). Windows says it is open and doesn't open it again. Again the problem is the directory structure can be complicated and I'll have to ask for more help, after he has chosen the directory.
In short, I want to use the New command and then there is no problem with the file already being open. The New command would solve the problem nicely (as it does when the program is started).
Thanks,
Ilan
|
|
|
|
|
If the FileOpen dialog is only used to select a folder, how does the user specify the filename to open? You're still not sufficiently describing your problem.
It looks like you need to completely redesign your file handling if you're having these kinds of issues. Since I don't know what you mean by "opens a whole bunch of files", I can't suggest an alternative file-handling mechanism.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
In fact the user need not specify a file. There is "dummy entry" as a default.
The whole bunch of files can represent different views of a cardiac study, which are all displayed. The trouble is that lately there can be multiple studies in the same directory and those I don't want to display together. I have to make a list and ask the user which one he wants, then I go pick up just the parts of the chosen study.
All this is already working. The only part which bothers me is if he goes back to the same directory to pick another study. If he happens to select the same file, then it is already "open".
(A bit of a lie here. The "dummy entry" doesn't work in the normal open file, as the file doesn't exist.)
I've been looking around and I found another suggestion on how to solve the problem. That suggests to use ID_FILE_NEW up front and inside OnNewDocument distinguish if it is the first time around or not. If not, add my dialog to change the directory. Someone suggested this way back in 1997.
Thanks for your suggestions. I still don't know why they don't work. It may have something to do with the fact that I am in the middle of an ID_FILE_OPEN command.
Ilan
|
|
|
|
|
ID_FILE_OPEN is not a message, it is a command. One could get away with posting a WM_COMMAND message with ID_FILE_OPEN as the menu item.
PostMessage(AfxGetMainWnd(), WM_COMMAND, MAKEWPARAM(0, ID_FILE_OPEN), 0);
You may be right
I may be crazy
-- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
I tried to compile a simple Dialog project with just a simple CARRAY declaration of a new class called CPoint3d.
I receive the following error
Generating Code...
Compiling...
MyTestDlg.cpp
c:\temp\mytest\mytestdlg.h(36) : error C2143: syntax error : missing ';' before '<'
c:\temp\mytest\mytestdlg.h(36) : error C2501: 'CArray' : missing storage-class or type specifiers
c:\temp\mytest\mytestdlg.h(36) : error C2059: syntax error : '<'
c:\temp\mytest\mytestdlg.h(36) : error C2238: unexpected token(s) preceding ';'
Generating Code...
Error executing cl.exe.
MyTest.exe - 8 error(s), 0 warning(s)
--------------------------------------------------
Here the declatation. What seems to be wrong here ?
----------------------------------------------------------
class CPoint3D
{
public:
CPoint3D ()
{
x = y = z = 0;
}
CPoint3D (int xPos, int yPos, int zPos)
{
x = xPos;
y = yPos;
z = zPos;
}
int x, y, z;
};
-------------------------------------------------------------------
class CMyTestDlg : public CDialog
{
// Construction
public:
CMyTestDlg(CWnd* pParent = NULL); // standard constructor
----> CArray<cpoint3d, cpoint3d&=""> array; ------------->ERROR
// Dialog Data
//{{AFX_DATA(CMyTestDlg)
enum { IDD = IDD_MYTEST_DIALOG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyTestDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CMyTestDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
-----------------------------------------------------------------------------
sdancer75
|
|
|
|
|
Try adding an
#include <afxtempl.h> to your header file.
-- modified at 7:02 Sunday 14th January, 2007
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
thanks, that was the solution.
Best Regards,
sdancer75
|
|
|
|
|
Please use the pre tag (above the emoticons) when you post code. And also, whe nyou have < or > characters in the code, replace them by the '<' tag (also just above the emoticons). In your case, the code is unreadable (because it uses templates that are discarded).
One possible solution: did you include the necessary header for the CArray ? Did you also include the header file from your CPoint3D class ?
(please edit your post and correct the formating).
|
|
|
|
|
view source won't work for me, so I can't read your code. Either way, I concur with the person who said that you probably don't have the headers included.
Have you tried std::vector ? It's like CArray, only good. You'll still need to include the header, but you'll be writing standard C++ and you'll find it will do a lot of stuff that CArray won't, if you ever need it to
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|