473,805 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Special scope rules for static operators?

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<<(ost ream &os,
const Object &object);
...
};

void Loader::aFuncti on(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.
Jul 23 '05 #1
4 1908
"J.J.GarcÃ* a" <ir****@terra.e s> wrote:
| 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<<(ost ream &os,
| const Object &object);
| ...
| };
|
| void Loader::aFuncti on(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&);
};
--
Robert Bauck Hamar
Jul 23 '05 #2
ro**********@if i.uio.no 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::operator <<(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.
Jul 23 '05 #3
"J.J.GarcÃ* a" <ir****@terra.e s> wrote:
| ro**********@if i.uio.no 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::operator <<(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:1 7: warning: unused parameter 'b'
$ ./a.out
B
$

--
Robert Bauck Hamar
Jul 23 '05 #4
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::operator <<(ostream &os, const Loader::Object &object) {
// ...
return os;
}

Instead of:

#include "renderer.h "
namespace rend {

....

ostream &operator<<(ost ream &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!
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6479
by: Anonymous | last post by:
Is namespace the same thing as scope? While reading the book "Thinking in C++", I was under the impression that namespace is, well, a namespace--a feature to create a hiearchy for identifiers within the Global Namespace. And that, all identifiers within a given namespace, however deeply nested they are, each of them has the Global Scope. So, if namespace outer { int a = 1;
8
3393
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an actual program. To me "scope" of the name of a function or object are the general rules for the areas of a program that can through a declaration, have "visibility."
23
5828
by: myth.drannon | last post by:
lets say I have a header file : struct AAAA { blabla..... }; typedef struct AAAA A; typedef struct BBB
39
3204
by: utab | last post by:
Dear all, Is there a clear distinction how to decide which functions to be members of a class and which not How is your attitude (Your general way from your experiences ...) "If the function changes the state of the object, it ought to be a member of that object." Reference Accelerated C++, A. Koenig, page 159.
3
2139
by: n2xssvv g02gfr12930 | last post by:
Although I rarely if ever have a problem with scope, I was wondering whether anybody knows of good reference, (book, web page, etc), that covers scope rules. Hopefully this will be helpful to others. JB
5
4798
by: Jeff | last post by:
Hey Below is a C# program I've made.... it's an app where I experiment with variable scope. In this code you see two variables named i (one is a local variable and the other is a member of the class). This code print out 5 ( Console.WriteLine(i); )... the member variable isn't passed on to the Multiply, Increment variables etc.. I wonder what the rules are here... I've been googling and and can't find a good article on variable scope in...
6
1865
by: Subra | last post by:
Hi, If I have the same varible defined in global as well as in local scope, how to access global scope varible in a function having the same local def. #include<stdio.h> static int a=25; int main() {
1
2356
by: Tim Haughton | last post by:
Firstly, if there's a better newsgroup to ask this on, feel free to redirect me. I have these requirements of WDS for a project: 1) I need to be able to add a new location to the user scope rules for WDS so that it indexes an application specific folder. This folder, at the moment, contains only text files. 2) This folder *should not* be included in any of Vista's searches from the
1
3762
by: Giacomo Catenazzi | last post by:
Hello, To learn the details of C, I've build the following example, could you check if it is correct and if it miss some important cases? Are there some useful (real cases) examples of: - "function prototype scope" for structures and unions? - "extern" for internal linkage ?
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10359
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10364
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10104
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9182
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7645
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5541
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.