标签: virtual

Virtual和Override在幕后做了什么

我刚刚了解Virtual和Override用于(我找不到这么长时间使用).现在我在Factory Patterns中使用它们.所以我的问题是Virtual和Override在幕后做了什么?我愿意进入IL和机器代码的东西.

c# virtual overriding

7
推荐指数
1
解决办法
1018
查看次数

有没有办法标记(在编译时)签名与基本签名不匹配的"重写"方法?

基本上,我想override在我的C++代码中使用其关键字的C#编译器功能.

class Base
{
   virtual int foo(int) const;
};

class Derived : public Base
{
   virtual int foo(int); // wanted to override Base, but forgot to declare it const
};
Run Code Online (Sandbox Code Playgroud)

众所周知,上面的代码编译得很好,但会产生一些奇怪的运行时行为.我希望我的C++编译器可以用C#的override关键字来捕获我糟糕的实现.是否有任何像"覆盖"这样的关键字被引入到C++中,或者我们是否坚持#define override virtual用来展示我们的意图?(实际上,我不这样做 - 我讨厌使用预处理器来"扩展"语言).

c++ virtual overriding

7
推荐指数
1
解决办法
188
查看次数

覆盖非const虚方法是否会隐藏const重载?

考虑:

#include <iostream>

using namespace std;

struct A {
  virtual void f() { cout << "A::f" << endl; }
  virtual void f() const { cout << "A::f const" << endl; }
};

struct B : public A {};

struct C : public A {
   virtual void f() { cout << "C::f" << endl; }
};


int main()
{
   const B b;
   b.f();   // prints "A::f const"

   const C c;
   c.f();
   // Compile-time error: passing ‘const C’ as ‘this’ argument of …
Run Code Online (Sandbox Code Playgroud)

c++ virtual inheritance const name-lookup

7
推荐指数
1
解决办法
2431
查看次数

虚函数和性能C++

在你对重复的标题感到畏缩之前,另一个问题不适合我在这里提出的要求(IMO).所以.

我真的想在我的应用程序中使用虚函数使事情变得容易一百倍(不是OOP的全部内容;)).但我读到了他们以性能成本出现的某个地方,只看到了过早优化的同样过时的炒作,我决定在一个小的基准测试中快速调整它:

CProfiler.cpp

#include "CProfiler.h"

CProfiler::CProfiler(void (*func)(void), unsigned int iterations) {
    gettimeofday(&a, 0);
    for (;iterations > 0; iterations --) {
        func();
    }
    gettimeofday(&b, 0);
    result = (b.tv_sec * (unsigned int)1e6 + b.tv_usec) - (a.tv_sec * (unsigned int)1e6 + a.tv_usec);
};
Run Code Online (Sandbox Code Playgroud)

main.cpp中

#include "CProfiler.h"

#include <iostream>

class CC {
  protected:
    int width, height, area;
  };

class VCC {
  protected:
    int width, height, area;
  public:
    virtual void set_area () {}
  };

class CS: public CC {
  public:
    void set_area () { area …
Run Code Online (Sandbox Code Playgroud)

c++ performance virtual inheritance

7
推荐指数
2
解决办法
1224
查看次数

C#和C++中VIRTUAL函数的编译器行为差异.这是我在面试中被问到的

虚拟化在C++和c#中有何不同?基本上我想知道Vtable在C#和c ++中表示的方式有什么不同吗?编译器在C#和c ++中创建V表的方式也有区别吗?

基本上,面试官想知道在C#和c ++的情况下,虚函数的编译器行为如何不同.

我已经回答了这个问题,提到VTABLE可能没有区别,期望c#中没有Virtual Destructor.

c# c++ virtual

7
推荐指数
1
解决办法
542
查看次数

在派生类中重新定义typedef?

因此,经过大量搜索我的问题的答案后,我终于放弃了我的Google技能.

我有一个基类Base和一个派生类Derived.我想在Derived类中使用一个类重写Base类中的类型.这是一个例子:

class Apple {
public:
    Apple() { }

    // ...

};

class Orange {
public:
    Orange() { }

    // ...

};

class Base {
public:
    typedef Apple fruit;

    // ...

    virtual fruit func() { return Apple(); }
};

class Derived : public Base {
public:
    typedef Orange fruit;

    // ...

    fruit func() override { return Orange(); } // <-- Error C2555!
};
Run Code Online (Sandbox Code Playgroud)

这段代码不起作用,它给出了一个

C2555 error ('Derived::func': overriding virtual function return type …
Run Code Online (Sandbox Code Playgroud)

c++ virtual inheritance overriding typedef

7
推荐指数
1
解决办法
7454
查看次数

DataGridView虚拟模式以简单列表作为源

之前我问过一个关于我的dataGridView性能的问题,因为它显示了大量基于传入流添加的行.给出了多种解决方案,其中一种解决方案支持虚拟模式 MSDN有一篇关于这个主题的文章,但它感觉比我需要的更复杂,因为它使用数据库和可编辑字段.我的DataGridView仅用于显示,我显示的数据放在List中.

在我接受答案后,我收到了这个链接:http://www.codeproject.com/Articles/23937/PagingANN-with havenGridView-in-VirtualMode.即使使用数据库示例,它也更适合我需要的东西.包含我要显示的数据的My List声明如下:

List<ResultRow> captureResults = new List<ResultRow>();
Run Code Online (Sandbox Code Playgroud)

ResultRow对象定义如下:

/* Simplified */
public class ResultRow
{
    private int first = 0;
    private string second = "";
    private UInt64 third = 0;
    private IPAddress fourth = null;
    /* etc */

    public ResultRow()
    {
    }

    public void Set (<the values>) //In actuallity a KeyValuePair
    {
        //field gets set here
    }

    public UInt64 Third
    {
        get { return third; }
        set { third = value; }
    } …
Run Code Online (Sandbox Code Playgroud)

c# virtual datagridview list

7
推荐指数
1
解决办法
1万
查看次数

是否建议明确地使覆盖函数成为虚拟的?

在C++ 11之前,当在派生类中重写虚函数时,建议将virtual关键字也添加到派生类函数中以使意图清晰.

现在这样的功能被标记为"覆盖",其中包括必须存在虚拟基本功能的概念.因此我现在更喜欢省略虚拟:

class Derived: public Base {
public:
  void Overriden() override;
  // Instead of: virtual void Overriden() override;
};
Run Code Online (Sandbox Code Playgroud)

但是,这会导致MSVC 2012中的IntelliSense错误:'override'修饰符需要一个带有显式"virtual"关键字的虚函数声明

显然编译器会编译类,但错误让我想一想.是否仍有正当理由添加虚拟关键字?

c++ virtual overriding c++11

7
推荐指数
1
解决办法
1544
查看次数

使用纯虚拟类复制和交换习语

我正在尝试使用纯虚方法和'复制和交换'惯用法实现虚拟类,但我遇到了一些问题.代码将无法编译,因为我在包含纯虚方法的类A的assign运算符中创建实例.

有没有办法如何使用纯虚方法和复制和交换成语?

class A
{
public:
    A( string name) :
            m_name(name) { m_type = ""; }
    A( const A & rec) :
            m_name(rec.m_name), m_type(rec.m_type) {}
    friend void swap(A & lhs, A & rhs)
    {
        std::swap(lhs.m_name, rhs.m_name);
        std::swap(lhs.m_type, rhs.m_type);
    }

    A & operator=( const A & rhs)
    {
        A tmp(rhs); 
        swap(*this, tmp);
        return *this;
    }

    friend ostream & operator<<( ostream & os,A & x)
    {
         x.print(os);
         return os;
    }

protected:
    virtual void print(ostream & os) =0;    

    string m_type;
    string m_name;
}; …
Run Code Online (Sandbox Code Playgroud)

c++ virtual class copy-and-swap

7
推荐指数
1
解决办法
882
查看次数

C++:使用指向派生类的指针的虚函数调用仍然具有vlookup

只是想知道,如果我有一个指向最派生类的指针,并在其上调用一个虚拟函数(大多数派生类定义),这是否仍会导致虚拟表中的查找?

毕竟,在编译时,编译器知道这个类是派生最多的,它知道它定义了虚函数,没有歧义,所以它应该只把它当作非虚函数?

或者我错过了什么?

我问的原因是我正在编写一个模板,我想从后面派生出来合并代码,不同的函数将在派生类中实现.

没有必要在模板中将这些函数定义为虚拟,但如果稍后忽略该虚拟调用,我正在考虑这样做,纯粹是为了向实现者可视化,以后需要仍然编写哪些函数.

c++ virtual inheritance templates

7
推荐指数
1
解决办法
184
查看次数