Classes access other classes local variables

This is a discussion on Classes access other classes local variables within the C++ Programming forums, part of the General Programming Boards category; Lets say I have a header file that declares a class, I have another header file that declares another separate ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    58

    Classes access other classes local variables

    Lets say I have a header file that declares a class, I have another header file that declares another separate class so now we have Class1 and Class2 who have no contact with one another. Class2 gets ballsy and wants to access local variables from Class1 how is this done exactly? Or should I rework the framework of my program?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,261
    Class1 should have a public GetSomething() function which returns its member variable. If you only want to make these local variables available to Class2 (and no other classes), then you can make Class2 a friend of Class1.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    First of all if u are saying that you want to access private variable of a class (local u said), there are numerous ways to do that....

    1... make the class as friends to each other.
    2... Make an object of a class and call there property function which we say get / set functions
    3... Another one which i can tell u is not in the books u will find, as it is having some restrictions as well.


    Code:
    #include <stdio.h>
    
    class Test {
    private:
      int a;
      int b;
    
    public:
      inline void show() {
        printf("a == [%d] || b ==[%d]\n", a, b);
      }
    };
    
    
    int main() {
      Test obj;
      int* ptr = (int*)&obj;
      *ptr = 10;
      ++ptr;
      *ptr = 20;
      obj.show();
      return 0;
    }

    sample code output


    Code:
    [rocky@localhost cpp]$ g++ test.cc
    [rocky@localhost cpp]$ ./a.out 
    a == [10] || b ==[20]
    [rocky@localhost cpp]$

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,625
    If Class1 has not provided some specific means to allow Class2 to access its members (eg making them public, declaring Class2 as a friend, some employment of the visitor pattern) there is no way that Class2 can force the issue without relying on non-standard (eg compiler specific) hacks.

    If by "ballsy" you mean that Class1 has not provided such access, then you can't.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    58
    Unfortunately i'm not sure how to implement this so i'll draw out how I interpret it:

    Class1.h
    Code:
    #ifndef _CLASS1_
    #define _CLASS1_
    
    #include "RandomAudioFramework.h"
    
    class Class1 {
    public:
         RandomAudioFramework *mRandomAudioFramework;
    
         DoSomethingWithFramework();
         RandomAudioFramework ReturnClass1LocalFramework();
    }
    
    #endif
    Class1.cpp
    Code:
    #include "Class1.h"
    
    void DoSomethingWithFramework() {
         mRandomAudioFramework->PlayBeep();
    }
    
    RandomAudioFramework RetrunClass1LocalFramework() {
         return *mRandomAudioFramework;
    }
    Class2.h
    Code:
    #ifndef _CLASS2_
    #define _CLASS2_
    
    #include "Class1.h"
    
    class Class2 {
         EditClass1Variable();
    }
    
    #endif
    Class2.cpp
    Code:
    #include "Class2.h"
    
    void EditClass1Variable() {
         ReturnClass1AudioFramework().Volume = 0;
    }
    main.cpp
    Code:
    #include <stdio.h>
    #include "Class1.h"
    #include "Class2.h"
    
    void main() {
         Class1 *class1 = new Class1;
         Class2 *class2 = new Class2;
         class2->EditClass1Variable();
         return 1;
    }
    Now i've been programming objective-c for the past few days so remembering how to call things in Cpp is for somereason harder than usual but I think the gyst is there. I want to create Class1 and Class2 and have Class2 access Class1... I hope this makes sence

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,625
    Since all your members of Class1 are public, you don't have a problem.

    The only caveats are that Class2's EditClass1Variable needs to be implemented as
    Code:
    Class2::EditClass1Variable() {
         some_particular_object_of_type_Class1.mRandomAudioFramework->Volume = 0;
    }
    If you want to use Class1's ReturnClass1LocalFramework() member function, then that function needs to return a reference, not an object. In other words;
    Code:
    class Class1
    {
       public:
          //other stuff
    
        RandomAudioFramework &ReturnClass1LocalFramework() {return *mRandomAudioFramework;};
    
    };
    This assumes, of course, that it makes sense for Class1 to allow other classes (eg Class2) unfettered abilities to modify its members. In practice, such things often represent poor design.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    Know that you can create the variables on the stack rather than the heap, too.
    You'll have to delete the variables that you allocate in your example, but you don't.
    Making them stack variables solves that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 09-30-2008, 03:12 AM
  2. Order of Memory Locations
    By vlrk in forum Linux Programming
    Replies: 11
    Last Post: 06-30-2008, 08:11 AM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  4. global and local variables
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2001, 02:17 PM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 09:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21