Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

Inner class accessing outer class private variables.

Hi.

class ClassA{
      public:
            class ClassB{
            }
      private:
            int aVar;
}

How can I allow the methods of classB to use the variable in class A in a tidy fashion?

Thanks,
Uni
0
Unimatrix_001
Asked:
Unimatrix_001
2 Solutions
 
ZoppoCommented:
Hi Unimatrix_001,

there's no other way than somehow passing an instance of ClassA to ClassB - this is clear since as your example is coded you can instantiate a ClassB without having a ClassA, i.e.:

ClassA::ClassB b;

So, a possibility to do so would be something like this:

class ClassA
{
public:
      void foo() { std::cout << "ClassA::foo called" << std::endl; }

      class ClassB
      {
            ClassA& m_classA;
      public:
            ClassB( ClassA& classA ) : m_classA( classA ) {}

            void foo() { m_classA.foo(); }
      };
};

ClassA a;
ClassA::ClassB b(a);
b.foo();

Hope that helps,

ZOPPO
0
 
ravenplCommented:
Inner classes are allowed to access Outer class private variables. They just treated as methods.

Just make sure, that inner class' methods are defined after outer class variables definition.
And of course inner class have to have reference(pointer) to instance of superclass.
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Unimatrix_001,

It only makes sense to declare a nested (or inner) class if you instantiate the inner class.

class ClassA{
      public:
            class ClassB{
            }
      private:
            classB *B;
            int aVar;
}

Now your options are wide open.  You can pass aVar to the classB instantiator which saves it in the inner class, or pass it to a method in the inner class.

ClassA  *A;

  A = new ClassA;
  A->B = new ClassB;


Good Luck,
Kent
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Unimatrix_001Author Commented:
Hi Zoppo: Thanks for that, although I can't pass an instance as what I'm wanting int classA is static...
0
 
Unimatrix_001Author Commented:
Kent: The reason for the nested class is to seperate certain methods in classA from other methods in classA while keeping all the methods within classA... That sounds a bit of a mouthful, but nevertheless there is logical reasoning behind it. ;)
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Unimatrix_001,

Sounds like a candidate for the spaghetti code website.   :)

Still, it works.  If the varible aVar will not change during the lifetime of the inner object, pass it to the instantiator.

class ClassA{
      public:
            class ClassB{
              public:
                ClassB (int my_aVar);
                int AVar;
            }
      private:
            classB *B;
            int aVar;
}

classA::classB::classB (int my_aVar)
{
  aVar = my_aVar;
}

Kent
0
 
Unimatrix_001Author Commented:
Hehe, it does seem a bit of a muddle... Still, it seem that's the only way to do it.

Thanks :)
Uni.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now