代码之家  ›  专栏  ›  技术社区  ›  Brian R. Bondy

在C++中,几个级别的基类会减慢类/结构吗?

  •  14
  • Brian R. Bondy  · 技术社区  · 16 年前

    有几个级别的基类会减慢一个类的速度吗?A派生B派生C派生D派生F派生G,…

    多重继承会减慢类的速度吗?

    12 回复  |  直到 16 年前
        1
  •  25
  •   Salman A    16 年前
        2
  •  6
  •   Nescio    16 年前
        3
  •  4
  •   Michael Burr    16 年前

    (static_cast<Base*>( this) == this)
    

        4
  •  2
  •   Corey Ross    16 年前

        6
  •  2
  •   Jim Buck    16 年前

        7
  •  1
  •   Evan Teran    16 年前

        8
  •  1
  •   AShelly    16 年前

        9
  •  0
  •   Statement    16 年前

    // F is-a E,
    // E is-a D and so on
    
    A* aObject = new F(); 
    aObject->CallAVirtual();
    

        10
  •  0
  •   Thomas    16 年前

        11
  •  0
  •   Community CDub    8 年前

    Corey Ross

    dynamic_cast

    sturct A { int i; };
    struct B { int j; };
    
    struct C : public A, public B { int k ; };
    
    // Let's assume that the layout of C is:  { [ int i ] [ int j ] [int k ] }
    
    void foo (C * c) {
      A * a = c;                // Probably has zero cost
      B * b = c;                // Compiler needed to add sizeof(A) to 'c'
      c = static_cast<B*> (b);  // Compiler needed to take sizeof(A)' from 'b'
    }
    
        12
  •  -1
  •   Daniel Bruce    16 年前