 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Marco T. Guest
|
Posted: Wed Aug 09, 2006 9:10 am Post subject: Simple question (namespaces and operators) |
|
|
Hi all,
why the former code fragment works and the latter doesn't?
=============
//CORRECT CODE:
namespace ciao
{
class A{};
A operator *(A &a, A &b) {return a;};
A method() ;
}
using namespace ciao;
A method() {A a1,a2; return a1*a2;}
=============
//INCORRECT CODE
namespace ciao
{
class A{};
A operator *(A &a, A &b);
A method() ;
}
using namespace ciao;
A operator *(A &a, A &b) {return a;}
A method() {A a1,a2; return a1*a2;} //line X
error: 'operator *' is ambiguous at line X
(compiler: VC++ 6.0)
=============
The issue is that I don't want to write the implementation of operator
* inside the namespace declaration.
thanks
marco |
|
| Back to top |
|
 |
Marco T. Guest
|
Posted: Wed Aug 09, 2006 9:10 am Post subject: Re: Simple question (namespaces and operators) |
|
|
Sumit RAJAN wrote:
| Quote: | A ciao::operator *(A &a, A &b) {return a;}
I would not recommend the using directive. Further, it may be a nice
idea to make the parameters const refs.
A method() {A a1,a2; return a1*a2;} //line X
Regards,
Sumit.
|
Thanks!
:-) |
|
| Back to top |
|
 |
Sumit RAJAN Guest
|
Posted: Wed Aug 09, 2006 9:10 am Post subject: Re: Simple question (namespaces and operators) |
|
|
Marco T. wrote:
| Quote: | //INCORRECT CODE
namespace ciao
{
class A{};
A operator *(A &a, A &b);
A method() ;
}
using namespace ciao;
A operator *(A &a, A &b) {return a;}
|
A ciao::operator *(A &a, A &b) {return a;}
I would not recommend the using directive. Further, it may be a nice
idea to make the parameters const refs.
| Quote: | A method() {A a1,a2; return a1*a2;} //line X
|
Regards,
Sumit. |
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|