On May 8, 1:32 pm, xman <cshin...@gmail.comwrote:
This produces an error for structA, because it is missing a
declaration of structB. But, declaring structB before structA, just
turns the problem around as structB then will be missing a declaration
of structA...
Do you know of any possible solutions?
Use forward declaration like:
class A;
class B;
class A { ... };
class B { ... };
However, you'll also need to use pointer to reference the other
classes.
No he doesn't. He can use whatever he finds appropriate for his needs.
The OP is dealing with parameters to member functions, not members of
the class(es). As an example, consider primitives, he may very well
decide not to use a pointer or reference because his tests show better
results are achieved by copying the primitive. Here is an example.
#include <iostream>
struct B;
struct A {
int n;
A(int n_) : n(n_) { std::cout << "A()\n"; }
A(const A& copy)
{
std::cout << "copy A\n";
n = copy.n;
}
void function( const B ); // by value
};
struct B {
int n;
void function( const A ); // by value
};
void A::function( const B b )
{
n = b.n;
}
void B::function( const A a )
{
n = a.n;
}
int main()
{
A a(99);
B b;
b.function( a );
std::cout << b.n << std::endl;
}
/*
A()
copy A // fast copy
99
*/
Again, the only reason to use a pointer or reference or const
reference is if the instance being passed as a parameter has a
performance hit if_and_when a copy ctor is invoked. Thats is not the
Op's situation, he is *not* suffering from circular membership /
redundancy.