Confused on syntax

Hello, I confused over the following syntax.
Could someone please explain to me exactly
what is happening here.
((CEditView*)m_viewList.GetHead())->SetWindowText(NULL);
I understand the casted type to the m_viewList.GetHead( ) function,
but the   ->SetWindowText    call on the end is what I've never seen
done like this?  Is somehow the return pointer from GetHead pointing
to the SetWindowText function ? But why would this be? Could not
SetWindowText just be called on it's own ?
0
RB
7/3/2008 11:01:28 AM
vc.mfc 33608 articles. 0 followers. Follow

6 Replies
745 Views

Similar Articles

[PageSpeed] 8

On 3 jul, 13:01, "RB" <NoMail@NoSpam> wrote:
> Hello, I confused over the following syntax.
> Could someone please explain to me exactly
> what is happening here.
> ((CEditView*)m_viewList.GetHead())->SetWindowText(NULL);
> I understand the casted type to the m_viewList.GetHead( ) function,
> but the =A0 ->SetWindowText =A0 =A0call on the end is what I've never see=
n
> done like this? =A0Is somehow the return pointer from GetHead pointing
> to the SetWindowText function ? But why would this be? Could not
> SetWindowText just be called on it's own ?

Well, that's exactly what it is doing, take the head of the list, cast
it to CEditView* and call its SetWindowText.

If you call SetWindowText directly, you are calling the SetWindowText
member of the class you are calling from (or its base class), if it is
CWnd derived or has a SetWindowText. So if you are calling it from,
say, a CDialog derived class, you would be calling
CDialog::SetWindowText (or CMyDialog::SetWindowText if you override
it).

Another option would be to call ::SetWindowText(HWND, LPCTSTR), but
that's WIN32 API, not MFC. That's what MFC does, wrap all those API
calls so that you don't have to get the HWND and call the API (and
much more, of course).

Hope this helps
0
mikel.luri (90)
7/3/2008 11:28:15 AM
RB wrote:
> Hello, I confused over the following syntax.
> Could someone please explain to me exactly
> what is happening here.
> ((CEditView*)m_viewList.GetHead())->SetWindowText(NULL);
> I understand the casted type to the m_viewList.GetHead( ) function,
> but the   ->SetWindowText    call on the end is what I've never seen
> done like this?  Is somehow the return pointer from GetHead pointing
> to the SetWindowText function ? But why would this be? Could not
> SetWindowText just be called on it's own ?

RB;

the code is equivalent tt

CEditView* pEditView = (CEditView*)m_viewList.GetHead();
pEditView->SetWindowText(NULL);

which I prefer, actually. The compiler will optimize away the temporary anyway.

Actually, SetWindowText() is a method of CWnd, so the cast should not be necessary.

In fact, though, don't you have to use GetEditCtrl() to get the underlying Edit 
control before you call SetWindowText()? Like this

pEditView->GetEditCtrl().SetWindowText(NULL);

in which case you do need the CEditView cast. Or does CEditView::SetWindowText() 
perform some trickery to send the text to the edit control?

-- 
David Wilkinson
Visual C++ MVP
0
no-reply8010 (1791)
7/3/2008 12:40:55 PM
Writing a piece of code like this is typically indicative of a serious design error.  You
should not be trying to set the text of a view from outside the view, and certainly it
doesn't make sense to be doing something like this.

This is calling SetWindowText on the view, nothing more.  Parse it.  The result of
m_viewLlist.GetHead() is the first element of a list (and it is not at all clear what
happens if the list is empty).  The question then is "what is the type of the list
element?", a little fact you omitted to tell us.  I presume it is CView*.  So it is cast
to a CEditView*, because only CEditView supports SetWindowText, and the result is the same
as writing

CView * view = m_viewList.GetHead();
CEditView * eview = (CEditView *)view;
eview->SetWindowText(NULL);

But ultimately it is very questionable as to why a piece of code like this would be
written.  I find it a common design error that "if you include enough header files and it
compiles, it must be right" without asking the question "Why do you need to include this
header file?"  (For example, the HTML Editor example in the MSDN is the latest example I
have of egregiously poor programming on the part of Microsoft, because it contains exactly
this sort of code)
					joe

On Thu, 3 Jul 2008 07:01:28 -0400, "RB" <NoMail@NoSpam> wrote:

>Hello, I confused over the following syntax.
>Could someone please explain to me exactly
>what is happening here.
>((CEditView*)m_viewList.GetHead())->SetWindowText(NULL);
>I understand the casted type to the m_viewList.GetHead( ) function,
>but the   ->SetWindowText    call on the end is what I've never seen
>done like this?  Is somehow the return pointer from GetHead pointing
>to the SetWindowText function ? But why would this be? Could not
>SetWindowText just be called on it's own ?
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15973)
7/3/2008 2:03:41 PM
"Joseph M. Newcomer" wrote in message 
> Writing a piece of code like this is typically indicative of a serious design error.  You
> should not be trying to set the text of a view from outside the view, and certainly it
> doesn't make sense to be doing something like this.
> 
The code of previous question came right of out of an MFC generated app for the 
OnNewDocument, with Doc/View support and CEditView being the base class.  I probably
should have show the whole sequence to eliminate ambiguity. But anyhow if I understand you 
correctly then the code is calling 
((CEditView*)m_viewList.GetHead()) so as to point to the correct window to set text ?
See this is one of the main areas of my confusion so far. I'm used to dealing with window
handles. I'm still struggling with how to connect with the correct window without a hwnd.
But I think you have helped me along my way with some more practice and printouts
!!! While !!!
  I'm here can you guys give me some insight on where I should declare an instance
of a generic class that I created ? Does it matter or should I instance it in the 
MainFrame.cpp.? I'm somewhat confused with a WinMain function. If it does not
need global scope then I can instance it where ever I want correct ?
Thanks to all replies, as usual all you all have been a tremendous help to a struggling 
learner of C++ and MFC. 
0
RB
7/3/2008 4:04:36 PM
"RB" <NoMail@NoSpam> wrote in message news:g4itan0145c@news5.newsguy.com...
> The code of previous question came right of out of an MFC generated app 
> for the OnNewDocument, with Doc/View support and CEditView being the base 
> class.  I probably
> should have show the whole sequence to eliminate ambiguity. But anyhow if 
> I understand you correctly then the code is calling 
> ((CEditView*)m_viewList.GetHead()) so as to point to the correct window to 
> set text ?
> See this is one of the main areas of my confusion so far. I'm used to 
> dealing with window
> handles. I'm still struggling with how to connect with the correct window 
> without a hwnd.
> But I think you have helped me along my way with some more practice and 
> printouts

In MFC a window is accessed with a pointer to the associated CWnd-derived 
object, instead of using window handles.  If you peek inside an MFC function 
like CWnd::SetWindowText you will see a little one-line function like 
::SetWindowText(m_hWnd, ...), so MFC is merely hiding the handle (m_hWnd) 
inside a C++ object.

> !!! While !!!
>  I'm here can you guys give me some insight on where I should declare an 
> instance
> of a generic class that I created ? Does it matter or should I instance it 
> in the MainFrame.cpp.? I'm somewhat confused with a WinMain function. If 
> it does not
> need global scope then I can instance it where ever I want correct ?
> Thanks to all replies, as usual all you all have been a tremendous help to 
> a struggling learner of C++ and MFC.

Yes, you can add a generic class anywhere you want.  Making it a member of 
the CDocument is probably the most common place, because views have easy 
access to the associated CDocument.  That's the place for "data."

-- 
Scott McPhillips [VC++ MVP] 

0
Scott
7/3/2008 4:47:10 PM
Yes, code like this *would* be found under the hood of MFC, because it has to handle all
kinds of things that users should never be attempting.  Code out of context is
unintelligible; we have no idea if it is part of the application, some third-party
library, the MFC implementation, etc.  The assumption is that unless you say otherwise it
is code you wrote.

You cannot connect to a window without an HWND.  A CWnd* contains an m_hWnd member which
has the HWND.

The HWND is not defined until the Create member is called; so when you have a CWnd
declaration, such as

CEdit stuff;

it has no HWND at all.  Eventually, one of two things will happen:

You will call
	stuff.Create(...);
to create the window, or someone will call
	stuff.Attach(handle_of_some_window);

which will set the m_hWnd member.  The above line is what is executed when you do a
DDX_Control call in a DoDataExchange (the result of creating a control member variable)

You need to create the instance at a point where you want the window to be known.  The
lifetime of the variable must be at least as long as the desired lifetime of the window,
because when the variable is destructed (CWnd::~CWnd) the DestroyWindow API is called.

So if you do

class MyThingView : public CView {
      	CEdit c_Name;

then c_Name will exist throughout the lifetime of the view.  When the view is destroyed,
the child windows are implicitly destroyed.

If you did

BOOL CMyForm::OnInitDialog()
   {
    ....
    CEdit name;
    name.Create(...);
    ... other stuff 
   }

then when you leave OnInitDialog, the CEdit control is destructed, and the window
disappears in a puff of greasy blue smoke.

It is very rare that you would declare such variables in the main frame or app classes
because they are then not accessible to anyone who needs them.  And the "toss #include
directives and casts at the code until it compiles" is not the correct solution.  Only the
app-class.cpp file should have access to the app-class.h file, and only the mainfrm.cpp
file should include mainfrm.h.  To do otherwise is a serious design error.

WinMain is irrelevant to MFC programmers; it is completely hidden from you and you don't
care about it.  THerefore, you would not declare it.  THere is no need to declare it, and
declaring it would be an error because you would get multiply-defined symbols.

You would be creating your application using the MFC AppWizard, so none of this is a
concern to you.  TYpically, you will not worry about creating windows until you need them,
and that is actually fairly rarely; MFC handles most of this, nearly all the time.  You
would have to explain more about why you think you need to create them at all.
						joe

On Thu, 3 Jul 2008 12:04:36 -0400, "RB" <NoMail@NoSpam> wrote:

>
>"Joseph M. Newcomer" wrote in message 
>> Writing a piece of code like this is typically indicative of a serious design error.  You
>> should not be trying to set the text of a view from outside the view, and certainly it
>> doesn't make sense to be doing something like this.
>> 
>The code of previous question came right of out of an MFC generated app for the 
>OnNewDocument, with Doc/View support and CEditView being the base class.  I probably
>should have show the whole sequence to eliminate ambiguity. But anyhow if I understand you 
>correctly then the code is calling 
>((CEditView*)m_viewList.GetHead()) so as to point to the correct window to set text ?
>See this is one of the main areas of my confusion so far. I'm used to dealing with window
>handles. I'm still struggling with how to connect with the correct window without a hwnd.
>But I think you have helped me along my way with some more practice and printouts
>!!! While !!!
>  I'm here can you guys give me some insight on where I should declare an instance
>of a generic class that I created ? Does it matter or should I instance it in the 
>MainFrame.cpp.? I'm somewhat confused with a WinMain function. If it does not
>need global scope then I can instance it where ever I want correct ?
>Thanks to all replies, as usual all you all have been a tremendous help to a struggling 
>learner of C++ and MFC. 
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15973)
7/3/2008 4:53:21 PM
Reply:

Similar Artilces:

i am so confused
As of day before yesterday, i was recieving and sending mail like i should, but now it says my username and/or password are inncorrect, and they are stored in the com, and my phone. i retyped them, and it says the same thing??? i just tried again, and it said the following: The connection to the server has failed. Subject 'hi', Account: 'pop.att.yahoo.com (1)', Server: 'smtp.att.yahoo.com', Protocol: SMTP, Port: 465, Secure(SSL): Yes, Socket Error: 10061, Error Number: 0x800CCC0E Pleas help.... anyone!!!!! i am lost without my e-mail!! Your settings look ...

when writing an IF statement what is the syntax for "Not Equal to. #2
what is the syntax for writing an If statement for NOT EQUAL TOO Use <> for not equal to. Example: =IF(A1<>32, "error",A1) "NEEDTOKNOW" wrote: > what is the syntax for writing an If statement for NOT EQUAL TOO ...

CListCtrl confusion
I am confused about the meaning of selected (LVIS_SELECTED) and focused (LVIS_FOCUSED). What is the difference? The above can be set/read with Set/GetItemState(). The there is also GetSelectionMark() and its set. When you get the selection mark, is this the one that is selected? Or does it mean something else? What I am able to do is click on a row and then execute my method, doit(). Now I want to simply use the up and down arrow keys to move the selection and focus up and down the list, each time calling doit() for the new row. The problem I am running into is that I get recursive calls to...

Confused on syntax
Hello, I confused over the following syntax. Could someone please explain to me exactly what is happening here. ((CEditView*)m_viewList.GetHead())->SetWindowText(NULL); I understand the casted type to the m_viewList.GetHead( ) function, but the ->SetWindowText call on the end is what I've never seen done like this? Is somehow the return pointer from GetHead pointing to the SetWindowText function ? But why would this be? Could not SetWindowText just be called on it's own ? On 3 jul, 13:01, "RB" <NoMail@NoSpam> wrote: > Hello, I confused over the following...

(more!) elementFormDefault confusion
Hi all, From what I can read, it doesn't look like I'm the only one getting confused with the elementFormDefault and attributeFormDefault properties of XML schemas. I am wanting to get a full understanding of these properties, as I want to be able to generate XML that conforms to these settings via the XmlTextWriter. The two main reference articles I can find are: http://geekswithblogs.net/dmillard/archive/2004/10/20/12935.aspx http://blogs.msdn.com/ebattalio/archive/2006/03/03/543154.aspx However, I have a couple of questions regarding this: 1. In Minty Fresh's example, w...

Syntax for attaching file via manual SMTP
I'm developing an in-house app in Access that needs to send an e-mail with a file attachment via our Exchange server (which I have configured to allow relays from the LAN), but it will be running on a server that has no Exchange client installed (an terminal/app server on which I do not really want to install Outlook). I'm currently testing a script that just Shells out to telnet and issues the port 25 connection commands. I got as far as DATA so that I can insert the text of the message, but I cannot find the syntax for attaching a file to the message. Brian <Brian@discuss...

Confused
I am using a shared computer at work. I have access help to resolve a simple problem and the answer is there. The big problem is that options it describes are not there for example how to set-up a signature some option names are not there and options it says should be available are different. Can someone help ?????? With the amount of missing information that you posted, probably not until the crystal ball has a nice vacation to rest up from overuse here. --� Milly Staples [MVP - Outlook] Post all replies to the group to keep the discussion intact. Due to the (insert latest virus na...

LDAP Syntax
Hi! I want that my IMAP users (Outlook 2003 clients) can access Global Address List in Exchange 2003. I read that I should create a LDAP address book connection in Outlook. my ldap server: gc my domain: mycompany.net my organizational unit: myusers my user: muster What ist the exactly syntax and configuration for that user in Outlook 2003? Is it: uid=muster,ou=myusers,dc=gc,dc=mycompany,dc=net? How about with search base? Should I use for it dc=root? Best Regards Mustafa ...

Still confused on whether this will work
Hi, I hope you can help with my question, and thanks in advance! In my use of MSMoney Plus Premium, I do bill payer through a bank directly (Wachovia) and download transactions directly using my web site passwords directly to those sites (T Rowe Price, Wachovia, Am Express for example). I am thinking this will still work after Feb 2011 (and my license goes that long), that I will still be able to get transactions automatically downloaded daily like I do now, and I can use the bank for billpayer through MSMoney? So the only thing I will lose is the stock price updates as they come fr...

confused???
I can't seem to figure out my windows email system. it asks for sever numbers and such??? if any one knows how to do it then let me know. Tisha S. wrote: > I can't seem to figure out my windows email system. it asks for sever > numbers and such??? if any one knows how to do it then let me know. ======================================= Maybe the following article will get you started: Windows Vista - Windows Mail: Setting up an account from start to finish http://tinyurl.com/yzf3t42 or..... http://windows.microsoft.com/en-US/windows-vista/Windows-Mail-sett...

OWA confusion.....
I am trying to figure out how to make OWA work behind my firewall.. I have referenced KB259240 (How to configure OWA to connect to exchange through a firewall) and therein lies my confusion.... It says to open 3 ports. 2 of them have to be statically configured in the registry. the first one located (added) at : HKLM\system\CurrentControlSet\Services\MSExchangeDS\parameters But that location does not exist on my system( Server 2003 / Exchange 2003 - SP1 I do have HKLM\system\CurrentControlSet\Services\MSExchangeDSAccess but the only keys under that are Diagnostics and performance. NOT P...

Confusing Email Problem
Sorry to bother you guys I have an email problem that is bugging me. I can recieve emails ok but when I send them, no one gets them. It appears to send and they even appear in the sent items folder but no one gets them. If I add large attachment the email will send in seconds. Something odd is going on. I use mail.onetel.net.uk. Port 25. When I called their call centre in India they said my account was active and they sent a test mail using their web based email system. They say it must be an outlook express problem. Yet I sent myself a test email using Telnet and still didn't ge...

confused
I am totally confused. I have written a few win api C apps that actually compiled without errors and ran successfully. Now I'm trying to learn MFC. I having a bit of a problem getting a call to GetParent to work. Below is the offending function and compilation errors BOOL CMfcPipesApp::InitInstance() { // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. CMfcPipesApp *pModelessDlg = new CMfcPipesApp(); CWnd *ParentWnd; ...

Product confusion (cont.)
Further question to my earlier post: Is this correct: MoneyCentral changed to MSN Money. Then Money changed from free, to a purchased product. The free version no longer supports downloads from brokers and many of the charting options. To get broker downloads and many charting options, I need to purchase Money 2005. Is that correct? PS: Cal, thanks for your earlier reply. -- Bill In microsoft.public.money, Bill wrote: >Further question to my earlier post: >Is this correct: >MoneyCentral changed to MSN Money. I think it has gone under both names for a good while. > Th...

Confused...
I thought Exchange 2007 / 12 was only going to be 64-bit? Am I missing something? it is...what's confusing you? -- Susan Conkey [MVP] "JPAUL" <jpaul34@msn.com> wrote in message news:#Zdg6PVwGHA.1296@TK2MSFTNGP02.phx.gbl... > I thought Exchange 2007 / 12 was only going to be 64-bit? Am I missing > something? > > A 32-bit version is available for testing/training. It is not and will not be supported in production environments. The 64-bit version is the only one that will be supported in production once it is released. -- Bharat Suneja MVP - Excha...

confused about create and manual events
i made and activated a work-rule with these characteristics: entity type=account event=create the rule: when an account is created the territory is assigned based upon the owner. the test: i converted 3 leads into accounts. and then i waited 30 miuntes. the result: none of the three new accounts have their territory assignments. however, i then manually executed the rule and the assignments were made. the question: why didn't it make the proper assignment when the account was created via converting the lead to an account? and why would an 'account create' rule work manually...

Confusion with forms and related subforms
Hello, I'm trying to figure out the concept of embedding subforms into main forms. I have main tables, 4 junction tables, and a few look-up tables (not all look-ups are listed below). If I'm reading it correctly, the junction tables which relate to the main tables should be in the form of a subform placed within the main form they relate to? I'm lost on the fact that I believe I have a subform which appears should be on another subform which should then be on a form. If this is correct, the form looks extremely busy and not user friendly, and just plain incorre...

CONFUSED!
i am using publisher to make a brochure... i have created the first part, now how do i start doing the inside of the brochure??? Insert, page... -- Mary Sauer MSFT MVP http://office.microsoft.com/ http://msauer.mvps.org/ news://msnews.microsoft.com "katie" <katie@discussions.microsoft.com> wrote in message news:E34CA105-D474-4A14-9143-6C605E4F7193@microsoft.com... >i am using publisher to make a brochure... i have created the first part, now > how do i start doing the inside of the brochure??? ...

Time syntax help
I have a query that uses this lookup: Expr1: DLookUp("Timer_Elapsed","QRY_Run_Reveal_Timings","Run_waypoint_List_ID = " & [Run_waypoint_List_ID]) the problem is that I get these kind of numbers returned: Expr1 -9.25925924093463E-05 -2.31481462833472E-05 -4.62962998426519E-05 -3.47222157870419E-05 but the [Timer_Elapsed] field that the lookup references, returns these correctly formatted numbers: Timer_Elapsed 00:00:08 00:00:02 00:00:04 00:00:03 00:00:02 00:00:03 00:00:02 00:00:01 00:00:01 00:00:03 The problem is that you're using a Date/Time fi...

Confused
I have the following macro in my Personal.xls workbook. Sub ShowFormulae() ActiveWindow.DisplayFormulas = Not ActiveWindow.DisplayFormulas ' test line End Sub I was able to add the comment ' test line to the macro without unhiding the workbook. I did not think this was possible. Windows | Unhide shows that it is hidden. Where am I confused? Brian Tozer Brian, Hiding a workbook has only effect on the "normal" (i.e. non VB view) In the VBE you can always see you workbooks. This also applies to hidden sheets in a workbook. (NB In the VBE it is possible that you ...

interfaces...I'm confused!
Hi. I have read tons of blog posts and articles about how and when to use interfaces. I have seen some people use them to define every object they create and I have seen some people say to not ever use them. Other people say not to use them unless you have some actions that multiple objects will use, an example is like this: You can have an interface that tells all breeds of cats that they must eat their food, sleep or even meow. Now, you have different kinds of cats: house cat, farm cat, tiger, lion, panther. All of these cats do the exact same thing, just in very different w...

Very confused
I have a live account. a while back in outlook it seems like somehow this account was added. I dont remember. My problem right now is that if I click a link in a web page to email, it opens up windows live mail in an outlook type format screen. the problem is it is sending from my yahoo and I've apparently entered the wrong server information. How do I fix it? If you go to Tools/Accounts menu item then look for your Yahoo account, select it then hit the properties button you can change the settings. Also here, if you wish you can set another account to be your default mai...

Confused,simple math formula
If I have a 379 liter tank (K3) filled with water and I need 1.0 grams per liter (D3) to have the correct amount in the tank. If I'm over (B3) lets say 1.9 grams per liter. I need to decrease (G3) by .9 grams per liter. Since it's already mixed,how would I get J3 to show the amount of Gals removed to bring it back to (D3) 1 gram per liter. I thought k3*g3/d3/3.78 would work but if it's 2.0 grams over(B3) then is says to add 100 gals. 100 gals is the entire tank size. If I did that I'd need to add 1 gram per liter. Confused!? Thanks so much for your help!!! In article <2b2b...

Error linking to my ATL dll
I'm using VS2005, building a WM5 application. The dll is a ATL com dll providing network services to my app. I've tested the dll with a simple dialog app, but now I try to link it to my application I get .... io 8\VC\ce\atlmfc\include\atliface.h(591) : error C2143: syntax error : missing ',' before '.' which points to virtual HRESULT STDMETHODCALLTYPE ShowUI( /* [in] */ DWORD dwID, /* [in] */ IUnknown *pActiveObject, /* [in] */ IUnknown *pCommandTarget, /...

Syntax error when using names with apostrophe
i'm getting syntax error message for this button that tries to get name (of building) with apostrophe. Any idea how to solve this? Note that i must use name with apostrophe e.g. O'neal: Private Sub Command76_Click() On Error GoTo Err_Command76_Click Dim stDocName As String Dim stLinkCriteria As String stDocName = ChrW(1506) & ChrW(1491) & ChrW(1499) & ChrW(1503) & ChrW(32) & ChrW(1508) & ChrW(1512) & ChrW(1496) & ChrW(1497) & ChrW(32) & ChrW(1502) & ChrW(1489) & ChrW(1504) & ChrW(1492) stLin...