Need help with C++ and classes

Gary Chatters gc-ar-l at
Tue Nov 11 15:07:19 CST 2008

Alberto di Bene wrote:
> Terry Fox wrote:
>> My problem is that the
>> reverse is not true.  Those other classes cannot gain access to the class 1
>> variables or methods, even though they are declared public.
> If the class1.h file in #included in the class2.h file, then all the public members of class1 *must* be accessible from 
> class2....
> 73  Alberto  I2PHD
As I understood the situation, an object of class 2 could not access a 
public variable or member function (method) of an object of class 1.  
This was because the scope of the class 1 object being outside the class 
2 member function.  A C++ object is a variable and must be in scope to 
be accessed.  To put it in to very abbreviated source code:

class Class1 {
        int f1();
        int v1;   //  For illustration purposes.  Attributes should not 
generally be public.

class Class2 {
        int f2();
        int v2;

int Class1::f1()
    Class2 c2;
    int fv1;
    fv1 = c2.f2();  // Object c2 is in scope.  Class1 function f1 can 
access it.
    return fv1;

int Class2::f2()
    int fv2;
    fv2 = c1.v1;  // Error.  Object c1 not in scope here.  It is in 
main.  Can not access it here.
    return 0;

     Class1 c1;
    int mv1;
    mv1 = c1.f1();

The class 1 object, or its attributes, could be made available by 
passing the appropriate pointer or value to the class 2 object.


More information about the Tacos mailing list