external function with a parameter that references a struct/class’s internal struct/class

That’s a mouthful. Here’s how an example:

#include <iostream>

/* Demonstration of referencing structure within class
from an external function declaration */

// This same concept applies to any class/struct holding
// a class/struct

class MyClass
{
public:
  MyClass(int a, int b) : s(a,b) {}
  struct MyStruct
  {
    MyStruct(int a, int b) : a(a), b(b) {}
    int a;
    int b;
  } s;
};

// important part here v v v
void func(MyClass::MyStruct &s)
{
  std::cerr << "a is " << s.a << std::endl;
  std::cerr << "b is " << s.b << std::endl;
}

int main()
{
  MyClass mc(1,2);

  func(mc.s);

  return 0;
}

I hope that helps you like it helped me. I wrote it from experimentation and deduction.

1 Comment on “external function with a parameter that references a struct/class’s internal struct/class

Leave a Reply

Your email address will not be published. Required fields are marked *

*