NameLookupAndInterfacePrinciple1

namespace A 
{
  struct X;
  struct Y;
  void f( int );
  void g( X );
}

namespace B
{
  void f( int i )
  {
    f( i );   // which f()?
  }

  void g( A::X x )
  {
    g( x );   // which g()?
  }

  void h( A::Y y )
  {
    h( y );   // which h()?
  }
}


namespace NS 
{	
	class T { };	
	void f(T);
}
NS::T parm;

int main()
{	
	f(parm);    // OK, calls NS::f	
}

//*** Example 1 (a) 
class X { /*...*/ };
/*...*/
void f( const X& );



//*** Example 1 (b) 
class X
{
  /*...*/
public:
  void f() const;
};

/*** Example 2 (a) ***/ 
struct _iobuf { /*...data goes here...*/ };

typedef struct _iobuf FILE;

FILE* fopen ( const char* filename, const char* mode );
int   fclose( FILE* stream );
int   fseek ( FILE* stream,	 long  offset,int   origin );
long  ftell ( FILE* stream );
/* etc. */

//*** Example 2 (b) 
class FILE
{	
public: 	
	FILE( const char* filename, const char* mode );
	~FILE();
	int  fseek( long offset, int origin );
	long ftell();
	/* etc. */	
private:
	/*...data goes here...*/	
};

//*** Example 2 (c) 
class FILE
{
public:
	FILE( const char* filename,  const char* mode );
	~FILE();
	long ftell();
	/* etc. */
private:
	/*...data goes here...*/
};

int fseek( FILE* stream, long  offset,int   origin );

std::operator<<( std::cout, hello );

//*** Example 4 (b) 


namespace NS   // typically from
{             // some header T.h
  class T { };
  void f( T ); // <-- new function
}

void f( NS::T );

int main()
{
  NS::T parm;
  f(parm);     // ambiguous: NS::f
}             //   or global f?

//*** The Myers Example: "After" 
namespace A
{
	class X { };
	void f( X ); // <-- new function
}

namespace B
{
	void f( A::X );
	void g( A::X parm )
	{
		f(parm);   // ambiguous: A::f or B::f?
	}
	
}

//*** NOT the Myers Example 


namespace A
{
	class X { };
	void f( X );
}

class B      // <-- class, not namespace
{
	void f( A::X );
	void g( A::X parm )
	{
		f(parm); // OK: B::f, not ambiguous
	}
};


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章