Special scope rules for static operators?

  • Thread starter =?ISO-8859-1?Q?=22J=2EJ=2EGarc=EDa=22?=
  • Start date
?

=?ISO-8859-1?Q?=22J=2EJ=2EGarc=EDa=22?=

Hi,

I have defined an static operator in a class, and when I try to use it
inside of the class, my compiler --Visual C++ 7.1-- complains saying
that there not exist an operator that suits the statement.

I have something like this:

namespace rend {

class Loader {
public:
void aFunction(void);
...
protected:
class Object;
...
private:
static ostream &operator<<(ostream &os,
const Object &object);
...
};

void Loader::aFunction(void) {
Object object;
...
cout << object; // Complains about not being a overloaded op.
// that permits the object class in the right
// of the binary operator "<<"
operator<<(cout, object); // This works ok, and it's really the
// same thing
}

} // namespace rend

I need the class Object to be defined as private, as it's really needed
outside of the Loader class. For this reason I cannot define the
operator<< in the rend namespace scope, as in this scope Loader::Object
is not accessible. So I need it to be a private static member of the
class. But then there seem to be scope exceptions for static operators
in a class, as I cannot use "cout << object", when it should be legal.

Can someone give me some light about this?

Thanks,
J.J.
 
R

roberth+news

| Hi,
|
| I have defined an static operator in a class, and when I try to use it
| inside of the class, my compiler --Visual C++ 7.1-- complains saying
| that there not exist an operator that suits the statement.
|
| I have something like this:
|
| namespace rend {
|
| class Loader {
| public:
| void aFunction(void);
| ...
| protected:
| class Object;
| ...
| private:
| static ostream &operator<<(ostream &os,
| const Object &object);
| ...
| };
|
| void Loader::aFunction(void) {
| Object object;
| ...
| cout << object; // Complains about not being a overloaded op.
| // that permits the object class in the right
| // of the binary operator "<<"
| operator<<(cout, object); // This works ok, and it's really the
| // same thing
| }
|
| } // namespace rend
|
| I need the class Object to be defined as private, as it's really needed
| outside of the Loader class. For this reason I cannot define the
| operator<< in the rend namespace scope, as in this scope Loader::Object
| is not accessible.

make it accessible:

class Loader {
//...
friend ostream& operator << (ostream&, const Object&);
};
 
G

Guest

(e-mail address removed) escribió:
make it accessible:

class Loader {
//...
friend ostream& operator << (ostream&, const Object&);
};

The problem is that I cannot write the implementation like this:

ostream &rend::eek:perator<<(ostream &os, const Loader::Object &object) {
//...
}

Because Object is protected, and not accessible from the rend namespace.
Object is only used inside the Loader class (in derived classes too) so
that I don't think it's a good idea to make it public, or outside of the
Loader class (something that would be a possible solution). I think that
this forces me to make the operator static, and in that situation I have
the scope problem explained in my previous message.
 
R

roberth+news

| (e-mail address removed) escribió:
| > make it accessible:
| >
| > class Loader {
| > //...
| > friend ostream& operator << (ostream&, const Object&);
| > };
|
| The problem is that I cannot write the implementation like this:
|
| ostream &rend::eek:perator<<(ostream &os, const Loader::Object &object) {
| //...
| }
|
| Because Object is protected, and not accessible from the rend namespace.

Have you actually tried? I can't guarantee that my compiler is
absolutely correct, but this works for me.

$ cat nestfriend.cc
#include <iostream>

using namespace std; //Don't do this at home, kids!

namespace T {
class A {
protected:
class B {};
public:
void foo()
{ B b; cout << b; }
friend ostream& operator << (ostream&, const B&);
};

ostream& operator << (ostream& o, const A::B& b)
{
return o << "B\n";
}
}

int main()
{
T::A a;
a.foo();
}
$ g++ -ansi -pedantic -W -Wall nestfriend.cc
nestfriend.cc:17: warning: unused parameter 'b'
$ ./a.out
B
$
 
G

Guest

Have you actually tried? I can't guarantee that my compiler is
absolutely correct, but this works for me.

I tried your example in Visual C++ and works perfectly too; the problem
was that in my cpp file, I was doing:

#include "renderer.h"
using namespace rend;

....

ostream &rend::eek:perator<<(ostream &os, const Loader::Object &object) {
// ...
return os;
}

Instead of:

#include "renderer.h"
namespace rend {

....

ostream &operator<<(ostream &os, const Loader::Object &object) {
// ...
return os;
}

} // namespace rend

I am not sure of the difference between these two, but I don't care
anyway, as it's working perfectly at last.

Thanks a lot, you saved me a lot of time!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top