object accessing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mar11
    New Member
    • Sep 2009
    • 38

    object accessing

    hi all,

    i have some trouble to figure out how can i access the object like in this case
    Code:
    //file1.h
    class T1{
    T1();
    ~T1();
    ....
    };
    ====
    //file2.h
    #include file1.h
    class T2{
    T2();
    ~T2();
    
    T1 Test;   // consider this object
    
    mem_foo() // member function
    ....
    };
    =====
    //file2.cpp
    ...
    T2::mem_foo(){
    foo();   //   this function is declared in file1.h
    }
    =====
    //file3.h
    extern foo();
    ====
    //file3.cpp
    foo()
    {
    // here is my problem! how can i access the object "Test" 
    //which is declared  in file2 in such case????
    }
    P.S. i have tried to simplify the code to just consider the on the main problem. Also, this code is not compilable.


    thanks for each suggestion
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You can't access .Test in foo unless you make foo a member of T2 or pass a reference or pointer to the T2 object into foo.

    I think a pertanent question here is why can't foo be a member of T2?

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      Also you appear to be attempting to create the object Test within the T2 class which would normally occur in main().

      Comment

      • mar11
        New Member
        • Sep 2009
        • 38

        #4
        @banfa;
        you may need this approach of accessing the object "Test" if you got the the function foo() from the file3.h as an interface function. In this case you are not allowed to change the name or declare it somewhere else....

        In this case how can you deal with it. i hope my question is getting through now. Again, i have to treat the foo() function in the file3.h as interface function.

        form you experience, may you lead me to the right way :) ?

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          You are saying that you can change the contents of the function foo() but not the prototype?

          In that case you need another function that you can call that will return the instance of T2 to use. This could be a function you call which takes some identifying parameter and looks up the object in an object store or it could be a singleton instance accessor (singleton design pattern).

          Alternatively if the instance of T2 only needs to last for the lifetime of the function construct it in foo().

          TBH the details of your problem are a little hazy as you haven't actually said what the function foo() is tying to achieve.

          Comment

          Working...