Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Advanced Pointers and References [modified]

Advanced Pointers and References [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
help
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aquawicket
    wrote on last edited by
    #1

    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

    T 1 Reply Last reply
    0
    • A aquawicket

      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

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      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]

      A 1 Reply Last reply
      0
      • T toxcct

        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]

        A Offline
        A Offline
        aquawicket
        wrote on last edited by
        #3

        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.

        T 1 Reply Last reply
        0
        • A aquawicket

          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.

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          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 within func2() :

          *pMyStr = CString("test3");


          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

          A 1 Reply Last reply
          0
          • T toxcct

            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 within func2() :

            *pMyStr = CString("test3");


            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

            A Offline
            A Offline
            aquawicket
            wrote on last edited by
            #5

            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.

            T 1 Reply Last reply
            0
            • A aquawicket

              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.

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              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]

              A 1 Reply Last reply
              0
              • T toxcct

                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]

                A Offline
                A Offline
                aquawicket
                wrote on last edited by
                #7

                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; }

                A 1 Reply Last reply
                0
                • A aquawicket

                  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; }

                  A Offline
                  A Offline
                  aquawicket
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups