I want to do something like this:

class foo
{
private:
class foo2
{
...
void Print (ostream & out)
....
};
ostream & operator << (ostream &out, const foo & temp);
public:
...
}

I want it so class foo can cout << foo2 objects only in the foo class. But when I try to do this, it gives me problems. Is there a way to do this, or should I just rely on a basic void Print() method?

Mistro116

Recommended Answers

All 2 Replies

you could make foo1 a base class

class foo1()
{
// blabla
};

class foo : private foo1
{
  // blabla
};

you could make foo1 a base class

class foo1()
{
// blabla
};

class foo : private foo1
{
  // blabla
};

But then we can create foo1 classes outside of foo, which is NOT what I want to happen. I only want foo to use the overloaed << for foo1, and I want this function to be a private member function outside of foo1, where foo1 is nested in the private section of foo. That way, only foo objects can cout << foo1 objects.

Any idea as to how? I tried friend, member function, non-member function. I got argument errors for all of these...

Mistro116

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.