小编Bun*_*uni的帖子

为什么编译器会在重载时停止名称查找?

我刚读过这篇文章:C++命名空间的乐趣 作者表明编译器在遇到第一个时会停止查找重载,这里使用命名空间.

namespace A
{
   void f(int x); // like our std::sqrt(double)
}

namespace B
{
   struct S {}; // user-defined type with associated namespace B

   void f(S);
   void f(int, int);

   void test1()
   {
      using namespace A; // using DIRECTIVE
      f(1);              // ERROR  namespace A is not considered because
                         //        B contains two overloads for 'f'
      f(1,2);            // OK     B::f(int,int)
      f(B::S());         // OK     B::f(S)
   }   

   void test2()
   {
      using A::f; // using DECLARATION
      f(1);       // OK     A::f(int)
      f(1,2);     // …
Run Code Online (Sandbox Code Playgroud)

c++ compiler-construction

3
推荐指数
1
解决办法
121
查看次数

使用继承避免代码重复

我来自C++背景,其中多重继承不是问题.Java中的Hoewever我只能使用类来继承或实现接口.我有多个类已经扩展了另一个类.但是所有这些类应该共享相同的调试接口/类.如何在不多次复制调试逻辑的情况下实现此类行为?

考虑我目前的设置:

public interface Debug
{
  public abstract void log();
}

public class ClassA extends AnotherBaseClass implements Debug
{
  public boolean doDebug = false;

  public void log()
  {
    if( doDebug )
      System.out.println( "LOG" );
  }
}

public class ClassB implements Debug
{
  public boolean doDebug = false;

  public void log()
  {
    if( doDebug )
      System.out.println( "LOG" );
  }
}
Run Code Online (Sandbox Code Playgroud)

java inheritance

2
推荐指数
1
解决办法
674
查看次数

标签 统计

c++ ×1

compiler-construction ×1

inheritance ×1

java ×1