For those who do not know, LPWSTR is a point to wchar_t or wchar_t *.
Well in theory, wchar_t * wins in performance simply because a pointer is almost always smaller than something huge like a string. They aren't really comparable though.
using wstring as a parameter will slower the proccess then
It will if you are passing by value: wstring generate_string(wstring happy) as you have to copy an object of wstring
but if you pass by reference it shouldn't: wstring generate_string(const wstring &happy)
i think out i can not use LPWSTR as a function return type, it is not allowed
LPWSTR is a pointer to wchar_t so you can use it as all other basic types and you can use it as return type
btw, * and & also means pass by address?
don't * means pass by value?
* and & have different meanings:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Function declaration
int f1 ( int i ); //Pass by value
int f2 ( int &i ); //Pass by reference
int f3 ( int *i ); //Pass by address
// Function call
int number = 123;
int *pointer = &number;
f1 ( 456 ); // pass by value
f1 ( number ); //pass by value
f1 ( *pointer ); //pass by value
f2 ( number ); // pass by reference
f2 ( *pointer ); // pas by reference
f3 ( &number ); //pass by address
f3 ( pointer ); // Here you are passing 'pointer' by value but its value is a memory address
what is the differnece between "pass by reference" and "pass by Value"?
When passing an argument by value, the parameter of the function is a copy of the argument passed.
When passing by reference, the parameter works on the same memory space as the argument. The result is that all the changes you do to the parameter will affect the value of the argument passed. ( If you pass by const reference const TYPE & you avoid copying the argument but it won't be modified ) http://www.cplusplus.com/doc/tutorial/functions2/
what is pass by address?
Is when you use a pointer as parameter
Pass by value:
1 2 3 4 5 6 7 8 9 10 11 12 13
void f ( int param )
{
param++;
cout << param;
}
f ( 1 ); // valid
int x = 5;
f ( x ); // valid, x is not changed by the function
int *ptr = newint;
f ( *ptr ); // valid, the value of the dynamically allocated int is not increased
Pass by reference:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void f ( int ¶m )
{
param++;
cout << param;
}
f ( 1 ); // not valid
int x = 5;
f ( x ); // valid, x is changed by the function: all the changes to the parameter will affect the argument
// x == 6
int *ptr = newint;
f ( *ptr ); // valid, the value of the dynamically allocated int is increased
Pass by address
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void f ( int *param )
{
(*param)++;
cout << param;
}
f ( 1 ); // not valid
int x = 5;
f ( x ); // not valid
f ( &x ); // valid, &x returns the address of 'x' and the value of x is changed by (*param)++.
//The value shown by cout << param is the hex representation of the memory address of 'x'
// x == 6
int *ptr = newint;
f ( ptr ); // valid, the value of the dynamically allocated int is increased
f ( NULL ); // valid but will cause runtime error as (*param)++ will try to change a value which doesn't exist
let's say you have the int variable 'x' at position 0xFEEF1E in memory with value 123, you have func_ref which takes an argument by reference, and func_add which takes the argument by address. func_ref ( x ) the parameter will be an int with value 123 at position 0xF00 -exacly the same as 'x' func_add ( x ) the parameter will be a pointer ( int* ) with value 0xFEEF1E at position 0xF0EF00
main()
{
int x =123; //memory position = 0xFEEF1E
func_ref ( x ) ;
func_ref ( x )
}
func_ref ( & x ) // in ref (), x value = 123 , memory position = 0xF00
{ }
func_add( * x ) // in add(), x value = 0xFEEF1E , it's a pointer ( int* ), memory position = 0xF0EF00
{ }
no matter pass by reference &
or
pass by address *,
Forget my last post, I've edited it wrong and I had to go.
Here is the right version of it:
let's say you have the int variable 'x' at position 0xFEEF1E in memory with value 123, you have func_ref which takes an argument by reference, and func_add which takes the argument by address. func_ref ( x ) the parameter will be an int with value 123 at position 0xFEEF1E -exacly the same as 'x' func_add ( &x ) the parameter will be a pointer ( int* ) with value 0xFEEF1E at position 0xF0EF00
You have to use & in the function parameter (the function prototype) when passing by reference, in the argument (the function call) when passing it by address
int main()
{
int x =123; // x value = 123 , memory position = 0xFEEF1E
func_ref (x) ;
func_add(&x) ;
}
func_ref (&x) // in ref (), x value = 123 , memory position = 0xFEEF1E
{ }
func_add(&*x) // in func_add , x value = 0xFEEF1E , memory position = 0xF0EF00
{ }
func_add which takes the argument by address.
func_add ( &*x ) the parameter will be a pointer ( int* ) with value 0xFEEF1E at position 0xF0EF00
You have to use &
in the function parameter (the function prototype) when passing by reference; (called function)
You have to use &
in the argument (the function call) when passing arugment by address
pass by refernce will not use one more address, the value is memory address~ ~
so what i need to do for using x ? if i want to calculate b= x*1000
If x is a variable ( int x; ) and you are passing by address, you should use &x:
&variable = memory address
*pointer = value
here is an example:
1 2 3 4 5 6 7 8 9 10 11
int variable = 1234;
int *pointer;
pointer = &variable; // set pointer to the value of the memory address of 'variable'
cout << *pointer; // display the value of 'variable'
cout << *variable; // Wrong: * unary operator doesn't exist for the type int
cout << &pointer; // display the memory address of pointer
cout << &variable; // display the memory address of variable