Reputation: 968
Given the following simplified class which contains a vector of MyStruct
objects, where should I define MyStruct
assuming that it's only being used inside of Foo
and should not be seen or used from outside?
class Foo
{
std::vector<MyStruct> array;
};
Two possibilities I've come across are putting the definition of MyStruct
right inside of Foo
or using an anonymous namespace.
Upvotes: 0
Views: 120
Reputation: 153919
In this case, you have to define it in Foo
, since it has to
be completely defined before you use it to instantiate
std::vector
. Otherwise, it depends; I'd tend to define it in
a private namespace: an unnamed namespace in the implementation
file if all of the functions are in a single source file,
otherwise in a private, implementation header file in a private
namespace (e.g. FooPrivate
) if they aren't.
Upvotes: 1
Reputation: 27365
If array is in any way a public part of Foo
interface, declare MyStruct like this:
class Foo
{
public:
struct MyStruct { /* stuff */ };
/* ... */
};
Otherwise: declare it in the private section of Foo
.
Upvotes: 1
Reputation: 11070
If you do not want it to be used outside Foo, then the best way is to declare it in the private section of Foo
class Foo
{
struct My_Struct;
}
Upvotes: 0