Advanced Pointers and References [modified]
-
I have multiple levels of functions that I would like to have pointing to the same data. I have things set up like so. I have an issue that when I get into func2.. str1[0].myStr does not have data; I'm screwing this up somewhere.
struct myStruct{ int myInt; CString myStr; }; void main() { myStruct *st1 = new myStruct[10]; st1[0].myStr = "test1"; Class1 *cls1 = new Class1(st1[0]); } /////// CLASS 1 class Class1 { myStruct *m_pSt1; } void Class1:Class1(myStruct &st2) { m_pSt1 = &st2; Class2 *cls2 = new Class2(m_pSt1->myStr); } void Class1::OnFileSave() { //get value of str MessageBox(m_pSt1->myStr); //this has no data!! but is fine if Class2::OnEventFunc2() gets processed or is omitted } /////// CLASS 2 class Class2 { CString *pMyStr; }; void Class2::Class2(CString &myStr2) { m_pMyStr = &myStr2 } void Class2::OnEventFunc2() { *m_pMyStr = "test3"; //when this is omitted everything works ok //if it is there, str1[0].myStr does not have data, even if this //function is never called. }
Thanks in advance for any help on this :) -- modified at 11:33 Monday 8th October, 2007 -
I have multiple levels of functions that I would like to have pointing to the same data. I have things set up like so. I have an issue that when I get into func2.. str1[0].myStr does not have data; I'm screwing this up somewhere.
struct myStruct{ int myInt; CString myStr; }; void main() { myStruct *st1 = new myStruct[10]; st1[0].myStr = "test1"; Class1 *cls1 = new Class1(st1[0]); } /////// CLASS 1 class Class1 { myStruct *m_pSt1; } void Class1:Class1(myStruct &st2) { m_pSt1 = &st2; Class2 *cls2 = new Class2(m_pSt1->myStr); } void Class1::OnFileSave() { //get value of str MessageBox(m_pSt1->myStr); //this has no data!! but is fine if Class2::OnEventFunc2() gets processed or is omitted } /////// CLASS 2 class Class2 { CString *pMyStr; }; void Class2::Class2(CString &myStr2) { m_pMyStr = &myStr2 } void Class2::OnEventFunc2() { *m_pMyStr = "test3"; //when this is omitted everything works ok //if it is there, str1[0].myStr does not have data, even if this //function is never called. }
Thanks in advance for any help on this :) -- modified at 11:33 Monday 8th October, 2007aquawicket wrote:
I have an issue
what knid of issue ? compilation ? linkage ? execution ? what's the error message ? BTW, if you use pMyStr inside
func2()
without declaring it previously, how can you think it can work ? ps: please use<pre></pre>
tags when posting code samples...
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
aquawicket wrote:
I have an issue
what knid of issue ? compilation ? linkage ? execution ? what's the error message ? BTW, if you use pMyStr inside
func2()
without declaring it previously, how can you think it can work ? ps: please use<pre></pre>
tags when posting code samples...
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
srry.. had to edit the code a little bit. I can't post the entire code so i'm trying to break it down. the pointers in the functions are actually member pointers so the entire class has scope of the pointer. By the time Class to is created myst1[0].str no longer has data. I will edit the code one more time to be more specific.
-
srry.. had to edit the code a little bit. I can't post the entire code so i'm trying to break it down. the pointers in the functions are actually member pointers so the entire class has scope of the pointer. By the time Class to is created myst1[0].str no longer has data. I will edit the code one more time to be more specific.
i have a question. do you really want the member
pMyStr
to be a CString pointer ?? anyway, what does this line produce if you replace the line withinfunc2()
:*pMyStr = CString("test3");
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
i have a question. do you really want the member
pMyStr
to be a CString pointer ?? anyway, what does this line produce if you replace the line withinfunc2()
:*pMyStr = CString("test3");
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
the point is, The pointer in Class2 shows that it is pointing to the write place, because when I set it in Class 2 it works. And In Class1, setting the value by pointer works also, so I know that one is pointing to the right place to. It is the value that is set in the main function that is somehow getting overwritten or NULL'ed, and i can't understand why.
-
the point is, The pointer in Class2 shows that it is pointing to the write place, because when I set it in Class 2 it works. And In Class1, setting the value by pointer works also, so I know that one is pointing to the right place to. It is the value that is set in the main function that is somehow getting overwritten or NULL'ed, and i can't understand why.
just a guess, try to use your debugger with some breakpoints to figure out where the data is NULL'ed. also, if you can use references instead of pointers, i advise you to ; using pointers and allocating memory on the heap is driving you into trouble if you don't handle them perfectly...
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
just a guess, try to use your debugger with some breakpoints to figure out where the data is NULL'ed. also, if you can use references instead of pointers, i advise you to ; using pointers and allocating memory on the heap is driving you into trouble if you don't handle them perfectly...
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
ok.. but how do I get the reference into the scope of the intire class. I was using member pointers to do it. is their another way? class Class1 { int *m_ptr; } Class1::Class1(int& num) { m_ptr = # } Class1::func1() { //I have access to the reference m_ptr = 5; }
-
ok.. but how do I get the reference into the scope of the intire class. I was using member pointers to do it. is their another way? class Class1 { int *m_ptr; } Class1::Class1(int& num) { m_ptr = # } Class1::func1() { //I have access to the reference m_ptr = 5; }
I FIXED IT !!!! :) it was not a problem with pointers, classes, member pointers or references at all. I had an event that was triggering before any data was set and it was in fact NULLifying the value. Thanks for the help