|
No it shouldn't be cout << valueReference
You miss my point completely, I wanted to show that value1 wasn't changed because this time a pointer was passed to changeValue. Pointers are passed by value, whereas references are passed byref, hence the different result when a pointer is used instead of a reference.
|
|
|
|
|
then change
this:
int valueReference = value1;<br />
changeValue(&valueReference);<br />
<br />
to:<br />
<br />
int *valueReference = &value1;<br />
changeValue(valueReference);
value1 didn't change, because you didn't pass address of value1 to pointer valueReference.
|
|
|
|
|
Tanks for your answer, my question was a bit unclear. What I’m really is looking for is a way to modify the referee itself not the “thing” it refers to, so I believe I have to do something like this.
<br />
const int value1 = 1;<br />
const int value2 = 2;<br />
<br />
void changeReference(const int *& p)<br />
{<br />
p = &value2;<br />
}<br />
<br />
void main()<br />
{<br />
const int * valueReference = &value1;<br />
changeReference(valueReference);<br />
std::cout << value1 << ", " << value2 << ", " << *valueReference;<br />
}<br />
Actually I felt quite familiarly with references in C++, until I started thinking about this particular task.
|
|
|
|
|
Once a reference has bound with a variable, there is no way you can change one without changing the other. So your only option is to use a pointer.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Hi,
How can i get the handle of the timer by using timer ID of the multimedia timer under WIN32 environment.
Thanks in advance!
|
|
|
|
|
Hi Guys,
I have a void* Message pointer.
i wonder how can i print out the content of the memmory which be pointed by the void type pointer.
And is there any article introduce about the void* pointer in detail.
Thanks very much in advance!
|
|
|
|
|
A void * is a memory address, and you have no idea what is in it. YOu could turn it into a char pointer and thus iterate over the data a byte at a time. But you'll have no idea of it's type, or how long it is.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Hi Christian,
Thanks verymuch for your help!
there should be a string stored in side the memory address.
Is it possible i can direct cast the it to a string typ?
Many thanks
|
|
|
|
|
Yes, you can do a static_cast or a simple C style cast, just make sure you have valid string at the address, otherwise the thing will blow up at runtime.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
The following code sequence produces an "A" on stdout, and I cannot figure it out why. PLEASE help !
stringstream os;
string buf;
os.str("");
buf=os.str();
cout << buf << flush;
|
|
|
|
|
How to arrange message sending and getting for example in MDI windows forms app I need to send a custom message WM_USER+1 or something from child to parent and to get it in parent. Something like in VC++ ON_MESSAGE(...) macro is it possible in windows forms, please give a snippet of code
9ine
|
|
|
|
|
|
ok here is what I need to to.
I have a system.string, and I would like to write to file. for this I thought I was gonna use fputs(). the prob is it must be a const char * and not some sort of string. I managed to convert it to __wchar_t __gc[] but this is not what I need.
thank you
|
|
|
|
|
quarry_06 wrote:
fputs().
Why are you using C file handling ?
quarry_06 wrote:
__wchar_t __gc[]
.NET uses Unicode strings, so you're going to need to convert from Unicode to ASCII if you want to use a char *.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
can u point me to a quick introduction of .net file handling with c++
thank you
|
|
|
|
|
I wouldn't bother, I'd just use C++. If you search the site for ifstream, you'll find articles from me, at least. C++ file handling is very rich, the only benefit of using MC++ for file handling would be that it would be bound to take the String object you've got there.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Christian Graus wrote:
the only benefit of using MC++ for file handling would be that it would be bound to take the String object you've got there.
it is also platform independant, as we can use exactly the same way the .NET framework on windows NT, 2k, XP, 2k3, MAC...
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
toxcct wrote:
it is also platform independant, as we can use exactly the same way the .NET framework on windows NT, 2k, XP, 2k3, MAC...
What crap is this ? Using .NET file handling is MORE platform independant than using straight C++ ? Are you on drugs ?
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
|
|
char *t1="Tommy";
char *t2="Lee";
strcat(t1,t2);
whatz wrong with the above code?
i'm using VC6.0.. the application gets terminated..
if i use char[] its working.... then y not char*? whatz the difference??
|
|
|
|
|
MOTLEY !!!!
Sorry. First of all, you're asking in the C++/CLI board, which is for managed C++. Yeah, the new name makes it less clear.
Secondly, you should prefer to use the std::string class, or even CString, rather than manipulating char *.
Finally, the first argument passed into strcat should contain enough space after the null terminator to make room to copy in the second string. This is the sort of crap that makes C string handling really sucky, compared to using std::string.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
char *t1="Tommy";
char *t2="Lee";
strcat(t1,t2);
//wrong
char t1[]="Tommy";
char t2[]="Lee";
strcat(t1,t2);
//right
char*'s are pointers to an array of char's
|
|
|
|
|
Hi all,
When Iam making a "push_back" into the vector,Its pushing back a new object into my vector(co's it goes to the constructor of the object) although ,Iam passing a refererence of the object into the "push_back".
Does "Push_Back" make a copy of the object passed to it.How can I avoid it....
Thanks...
|
|
|
|
|
Yeap push back will make a copy. It does not matter you pass a ref into it. You should have found that a lot of copy constructors take a reference and that's what you r doing.
To avoid it, you should declare a vector of pointers rather than the that class.
e.g.
class CMyClass
{
....
};
std::vector<*CMyClass> myVec;
Just remember to delete everyting at an appropriate time so that there's no memory leak
|
|
|
|