English 中文(简体)
一. 关于C++虚拟功能的问题
原标题:one question about C++ virtual function
  • 时间:2009-12-12 20:07:43
  •  标签:
  • c++

我的朋友问我一个虚拟功能问题。

如果儿童反对将虚拟功能称为虚拟功能,那么这种虚拟功能实际上是在父亲执行中完成的?

问题回答

我认为,你需要张贴一些法典,以澄清你要求的内容,但(除校长外)除非儿童明确要求其履行自己的职责,否则不会叫上基础职能。 例如:

struct A {
    virtual ~A() {}
    virtual void f() {}
};

struct B : public A {
    virtual void f() {}
};

int main() {
    A * a = new B;
    a->f();
    delete a;
}

只有B的虚拟功能 f()被点名。 如果你想要A:f()要求你明确:

struct B : public A {
    virtual f() { 
       A::f();    // explicit call
    }
};

在B没有宣布职能的情况下,当然还有奥赫,在这种情况下,A:f()总是被叫来。

不可能理解问题在目前形式中的确切含义。

如果从字面上讲,问题有明显和立即的答案:如果父母的履行是有关职能的最终压倒者,即儿童没有执行自己的职责,则要求父母的版本。

class parent {
public:
  virtual void foo() { /* whatever */ }
};

class child : parent {
public:
  void bar() {
    foo(); /* call the parent s implementation, as requested */
  }
};

因此,请你回答。

当然,对任何人来说,这种解释是显而易见的,他们很可能不是问题所暗示的。 很可能意味着子女阶级优先于父母的职能。 在该案中,还有另一个明显的答案:如果儿童使用完全合格的职务名称,将叫作母体。

class parent {
public:
  virtual void foo() { /* whatever */ }
};

class child : parent {
public:
  virtual void foo() { /* whatever */ }
  void bar() {
    parent::foo(); /* call the parent s implementation, as requested */
  }
};

另一种可能的答案是,被称之为功能的物体实际上有<条码>。

class parent {
public:
  virtual void foo() { /* whatever */ }
};

class child : parent {
public:
  virtual void foo() { /* whatever */ }
  void bar() {
    parent p;
    p.foo(); /* call the parent s implementation, as requested */
  }
};

同样,它认为,这不是问题。 很可能,问题在于建筑商和司机发出的虚拟电话。

class parent {
public:
  parent() { 
    foo(); /* always calls `parent::foo` */
  }
  virtual void foo() { /* whatever */ }
};

class child : parent {
public:
  child() : parent() /* `parent::parent` will call `parent::foo` */
    {}
  virtual void foo() { /* whatever */ }
};

然而,由于这一问题的措辞不正确。 在发出呼吁时的最后一个例子中,还没有儿童目标。 它的记忆已经分配,但其寿命尚未开始。 说虚拟功能的电话由儿童对象履行是不正确的。 系由母物体执行。

因此,为了恢复上述工作:问题措辞模糊不清,措辞模糊不清,没有意义。

  • When the base class s scope is explicitly used ( Base::f(); )
  • Inside the base class s constructor (because the derived object s constructor hasn t been entered yet)
  • Inside the base class s destructor (because the derived object has already been destructed)

如果这一举动是想方的,则发送将是静态的。 http://cplus.co.il/2009/09/30/virtual-dissuing-within-a-ructor-or-a-destructor/“rel=“nofollow noreferer”>http://cplus.co.il/2009/09/30/virtual-dispading-within-a-ructor-or-a-destructor/

第一条的例子涉及:

struct A {
    A () { f(); }
    virtual void f () { }
};

struct B : A {
        B () :member(0) {}
        void f () { std::cout << member; }
    private:
        int member;
};

int main () {
    B b;
    return 0;
}

援引的f是A:f,尽管事实上它是虚拟的,并且是由具有自己执行的B类物体所呼吁的。

如果儿童班子被叫走,那么该目标现在就属于父母的类型,因此,父母的虚拟功能将被称作。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签