Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » C++

Can't template operator << be private?

 
Jump to:  
 
lv
PostPosted: Thu Sep 04, 2008 4:24 am    Post subject: Can't template operator << be private?
       
Hi all!

I have a class with a private template operator << like this:

class MyTest
{
template <typename T>
MyTest & operator << (T const & t)
{
std::cout << t << std::endl;
return *this;
}
};

But the operator is accessible outside the class. However, if I make
it a non-template operator that only accepts an integer, it's truly
private. The code was tested on vc9.0.

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Jiang
PostPosted: Thu Sep 04, 2008 9:28 am    Post subject: Re: Can't template operator << be private?
       
On Sep 4, 1:24 pm, lv <lv.jc...@gmail.com> wrote:
Quote:
Hi all!

I have a class with a private template operator << like this:

class MyTest
{
template <typename T
MyTest & operator << (T const & t)
{
std::cout << t << std::endl;
return *this;
}

};

But the operator is accessible outside the class. However, if I make
it a non-template operator that only accepts an integer, it's truly
private.

Access specifiers should be respected for both template and
non-template code.

Quote:
The code was tested on vc9.0.

Test more please. Seems vc9 is broken here.

* icl 10.1
* GCC (4.3.2/4.3.1/4.2.2/3.4.4)
* como 4.3.10.1

reject your access code in my mind.

Regards,

Jiang


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Thomas Richter
PostPosted: Thu Sep 04, 2008 9:28 am    Post subject: Re: Can't template operator << be private?
       
lv schrieb:
Quote:
Hi all!

I have a class with a private template operator << like this:

class MyTest
{
template <typename T
MyTest & operator << (T const & t)
{
std::cout << t << std::endl;
return *this;
}
};

But the operator is accessible outside the class.

For which definition of "accessible"? Could you please post a complete
example that demonstrates the problem?

So long,
Thomas

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
lv
PostPosted: Thu Sep 04, 2008 7:31 pm    Post subject: Re: Can't template operator << be private?
       
On Sep 4, 5:28 pm, Thomas Richter <t...@math.tu-berlin.de> wrote:
Quote:
lv schrieb:


For which definition of "accessible"? Could you please post a complete
example that demonstrates the problem?

So long,
Thomas


Well, I should have made it clearer. I meant the following code
compiles ,

#include <iostream>
class MyTest
{
template <typename T>
MyTest & operator << (T const & t)
{
std::cout << t << std::endl;
return *this;
}
};


int main()
{
MyTest test;
test << 10;

return 0;
}

while this one doesn't compile:

#include <iostream>
class MyTest
{
MyTest & operator << (int const & t)
{
std::cout << t << std::endl;
return *this;
}
};


int main()
{
MyTest test;
test << 10;

return 0;
}

I tested it again with vc8.0 and got the same result.


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Guest
PostPosted: Thu Sep 04, 2008 10:36 pm    Post subject: Re: Can't template operator << be private?
       
On Sep 4, 12:31 pm, lv <lv.jc...@gmail.com> wrote:
Quote:
On Sep 4, 5:28 pm, Thomas Richter <t...@math.tu-berlin.de> wrote:
For which definition of "accessible"? Could you please post a complete
example that demonstrates the problem?
Well, I should have made it clearer. I meant the following code
compiles ,
[snip]


Try your code on LINK
You will see that both samples do not compile. It's a bug in visual
studios apparently.


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

gratka docieplenie domu Sklep zoologiczny Fotka gry do pobrania