473,320 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Templated friend function can't access private members?

Hi folks... I've been sifting through archived postings on this, but
haven't quite found an answer yet. I've got a templated stand-alone
function:

template<typename Tvoid access(const T & t) { ; }

And a class 'Foo':

struct Foo {
friend void access(const Foo & f);
private:
int x;
};

As you can see, Foo is friended to the function 'access'. One more
thing to do: specialize access() for Foo:

template<void access(const Foo & f) { f.x = 9; }

So access() should be able to work, right? Because access() has been
friended properly to Foo?

The following main() code, however, won't compile. The compiler
complains that 'x' is private:

int main() {
Foo f;
access(f);
}

Did I declare the friend function incorrectly? Or is this simply not
possible?

Thanks in advance,
--Steve (mr************@hotmail.com)

Nov 6 '06 #1
3 1836
mrstephengross wrote:
Hi folks... I've been sifting through archived postings on this, but
haven't quite found an answer yet. I've got a templated stand-alone
function:

template<typename Tvoid access(const T & t) { ; }

And a class 'Foo':

struct Foo {
friend void access(const Foo & f);
private:
int x;
};

As you can see, Foo is friended to the function 'access'.
Yes, to a non-templated overload of it.
One more thing to do: specialize access() for Foo:

template<void access(const Foo & f) { f.x = 9; }

So access() should be able to work, right? Because access() has been
friended properly to Foo?
Not if you mean the template specialization.
The following main() code, however, won't compile. The compiler
complains that 'x' is private:

int main() {
Foo f;
access(f);
}

Did I declare the friend function incorrectly? Or is this simply not
possible?
Try:

friend void access<>(const Foo & f);

Nov 6 '06 #2
Ok, the addition of the "<>" got it working. My next problem is
namespaces, though. I stick Foo in a namespace ("NS"), and it compiles;
the linker, however, complains that access<is multiply defined:

================================================
template<typename Tvoid access(const T & t) { ; }
namespace NS {
struct Foo {
friend void access<>(const NS::Foo & f);
private:
int x;
};
} // End namespace

template<void access(const NS::Foo & f) { f.x = 9; }

int main() { NS::Foo f; access(f); }
================================================

Am I maybe forgetting 'inline' tags? Is there some fundamental issue
with templates & namespaces?

Thanks again,
--Steve

Nov 6 '06 #3
mrstephengross schrieb:
Ok, the addition of the "<>" got it working. My next problem is
namespaces, though. I stick Foo in a namespace ("NS"), and it compiles;
the linker, however, complains that access<is multiply defined:

================================================
template<typename Tvoid access(const T & t) { ; }
namespace NS {
struct Foo {
friend void access<>(const NS::Foo & f);
private:
int x;
};
} // End namespace

template<void access(const NS::Foo & f) { f.x = 9; }

int main() { NS::Foo f; access(f); }
================================================

Am I maybe forgetting 'inline' tags? Is there some fundamental issue
with templates & namespaces?
I am surprised that this code can be compiled, as you pass a reference
to a const object in access(). Hence, the compiler should complain that
you try to access 'x' which is read-only... Or am I missing something?

Furthermore, note that the code above with the const qualifier removed
compiles fine with gcc 4.1.0

Cheers,
Loic.

Nov 6 '06 #4

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

Similar topics

12
by: Bryan Parkoff | last post by:
CMain Class is the base class that is initialized in main function. CA Class is the base class that is initialized in CMain::CMain(). CMain Class is always public while CA Class is always...
5
by: Srini nandiraju | last post by:
Hi folks, I am trying to implement a friend class and the following is what I did. Please scroll down. /***************** CODE **********************************/ class BKP { private: int...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
5
by: Teddy | last post by:
Hello all consider the class Date declaretion below: class Date { public: Date(); Date(int year, int month, int day); Date(const string&); int getYear() const;
3
by: Fiber Optic | last post by:
I wrote a sample app to test using a function to obtain friend-level access to a templated class. However, I am getting similar errors from two separate compilers. If anyone can recommend what...
5
by: pat270881 | last post by:
hello, i should implement this class: namespace test_1 { class statistician { public: // CONSTRUCTOR
1
by: yancheng.cheok | last post by:
currently, i have a private function in cat named privateFun. i would like to have this function "private" to all except dog's action member function. by using the following approach, all the...
6
by: Sunny | last post by:
Hi All, In C++ , if a function or class is declared friend, then where is that information stored? How does friend function get access to private data of a Class? Thanks
19
by: subramanian100in | last post by:
Stroustrup, in his book TC++PL 3rd Edition, in page 16, under the section '1.8 Advice' has mentioned the following: Don't use global data(use members) Don't use global functions Don't use...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.