0
votes

I have created a simple class called Foo, which contains a data member, Bar, which is a struct.

class Foo
{
  public :
  struct Bar {
    int a;
  };
};

I'd like to be able to access members in the struct either from functions I define in the class, or from the driver file, but I'm not sure how. Note: I've declared Bar as a public member because I am trying to access the members directly without using a get function. There is method in my madness, but I'll get to that later, so please accept that I want the struct to be public for now.

This is a very stripped down version of a larger program, so please forgive the simplicity.

1
Bar is not a data member, it is a class definition. You need something like Bar bar; to have bar as a member of Fookmdreko

1 Answers

0
votes

I have created a simple class called Foo, which contains a data member, Bar, which is a struct.

Not really. Foo doesn't contain any data members at all. It just defines a type called Foo::Bar. An object of type Foo::Bar has an int data member named a, but a Foo object itself doesn't have any Bar or int subobjects.

If you do want an object of type Bar in every Foo, you need to declare that member with some name:

class Foo
{
  public :
  struct Bar {
    int a;
  };
  Bar bar;
};

This would let you do things like:

void test() {
    Foo f;
    f.bar.a = 5;
}