Some Code

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2007
Posts: 290
Reputation: kv79 is an unknown quantity at this point 
Solved Threads: 7
kv79 kv79 is offline Offline
Posting Whiz in Training

Some Code

 
0
  #1
Jan 7th, 2008
Hi ,i just have a fhew code witc i do not understand clear.Can you make some explain.
It is category from C++ Win32 API.
  1. typedef std::basic_string<TCHAR> ustring;
  2. inline int ErrMsg(const ustring& ); What this mean ustring& ?

  1. ustring classname=_T("SIMPLEWND");
  2. ErrMsg(_T("Failed to register wnd class");

// And i did not typed (sorry )
  1. inline int ErrMsg(const ustring& s)
  2. {
  3. return MessageBox(0,s.c_str(),_T("ERROR"),MB_OK|MB_ICONEXCLAMATION);
  4. }
s.c_str() how it can be this ?
Thank you for your effort of reading.
Last edited by kv79; Jan 7th, 2008 at 10:03 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,781
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: Some Code

 
0
  #2
Jan 7th, 2008
>s.c_str() how it can be this ?
c_str is a member function of the basic_string class. Perhaps it would make more sense to you if I said that the std::string class is actually a typedef:
  1. typedef std::basic_string<char> string;
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 290
Reputation: kv79 is an unknown quantity at this point 
Solved Threads: 7
kv79 kv79 is offline Offline
Posting Whiz in Training

Re: Some Code

 
0
  #3
Jan 7th, 2008
Do you want to say that when i write
typedef std::basic_string<TCHAR> ustring;

i add a ustring to class basic_string who all ready exist with headers.
#include <tchar.h> or what?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,781
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: Some Code

 
0
  #4
Jan 7th, 2008
>i add a ustring to class basic_string who all ready exist with headers.
No, you add an instantiation of basic_string that holds TCHARs and call it ustring. basic_string already exists in the <string> header.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 290
Reputation: kv79 is an unknown quantity at this point 
Solved Threads: 7
kv79 kv79 is offline Offline
Posting Whiz in Training

Re: Some Code

 
0
  #5
Jan 7th, 2008
Thank you very much.
And one more question.
Why author use
ustring& Why he need bitwise operator on the end and know what mean on begin?
Can you clear me inline?
I know that it is insert a copy of body of called function.
So were is the difference between normal call?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,781
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: Some Code

 
0
  #6
Jan 7th, 2008
>Why he need bitwise operator on the end and know what mean on begin?
That's not the bitwise AND, it's the syntax for a reference type.

>So were is the difference between normal call?
There's no difference. The inline function might execute faster, or it might not. inline is really something you should forget about for now.
Last edited by Narue; Jan 7th, 2008 at 11:13 am.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 290
Reputation: kv79 is an unknown quantity at this point 
Solved Threads: 7
kv79 kv79 is offline Offline
Posting Whiz in Training

Re: Some Code

 
0
  #7
Jan 7th, 2008
>That's not the bitwise AND, it's the syntax for a reference type.

I got it ,
it is a constructor string( const string& s)

And he can't have a same name as a constructor
so he put a string&.
am i right?


omg but i have ustring&.
Last edited by kv79; Jan 7th, 2008 at 11:39 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,781
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: Some Code

 
0
  #8
Jan 7th, 2008
>am i right?
Uh, no. Read up on references.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 290
Reputation: kv79 is an unknown quantity at this point 
Solved Threads: 7
kv79 kv79 is offline Offline
Posting Whiz in Training

Re: Some Code

 
0
  #9
Jan 7th, 2008
Can you gave me some link, i spent hours to find what i want?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,829
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Some Code

 
1
  #10
Jan 7th, 2008
The difference between
  1. int ErrMsg(ustring& )

which is what you have, and

  1. int ErrMsg(ustring )


is that in the first function, the function is looking for a parameter that is "passed by reference". The second function is looking for a parameter that is "passed by value". The '&' character signifies "pass by reference" and basically stands for "address of".

C++ has pointers and addresses and the word "reference" and "dereference" come up often, as do the symbols '*', standing for "pointer to" and '&', meaning "address of".

The "ustring&" signifies "pass by reference". This means that any changes that the function makes to the parameter passed to it will also change the value of the original variable passed to it (they actually ARE the same variable I believe). Changing a "pass by value" parameter within the function will NOT change the original value passed to it. As for a link, googling "C++ pass by reference example" or "C++ pointer tutorial" gives quite a few good links. Here's an example I wrote up:

  1. #include <string>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. void PassByReferenceFunction ( string& animalName);
  6. void PassByValueFunction (string animalName);
  7.  
  8.  
  9. int main ()
  10. {
  11. string animal = "dog";
  12. cout << "Animal is a " << animal << endl;
  13. PassByReferenceFunction (animal);
  14. cout << "Animal is a " << animal << endl;
  15. PassByValueFunction (animal);
  16. cout << "Animal is a " << animal << endl;
  17. system ("PAUSE");
  18. return 0;
  19. }
  20.  
  21.  
  22. void PassByReferenceFunction (string& animalName)
  23. {
  24. animalName = "lion";
  25. }
  26.  
  27.  
  28. void PassByValueFunction (string animalName)
  29. {
  30. animalName = "tiger";
  31. }


The result is that the variable changed after calling the "pass by reference" function. but not after calling the "pass by value" function. That function changed the value to "tiger" but went "out of scope" afterwards so the original animal stayed as "lion".

The result therefore is:

dog
lion
lion

rather than

dog
lion
tiger

I took out the "const" part of your function since I wasn't sure how it was being used or why.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC