English 中文(简体)
关于虚拟功能的排序问题
原标题:question regarding templatization of virtual function

我知道,你不能限制虚拟功能,我确实理解这一功能背后的概念。 但是,我仍然需要一种办法,来纠正我正在发现的一些错误。 我能够做得uff的工作,但我看不到。

我的班次为<代码>。 系统

#include "Vector.h"
class System
{
    virtual void VectorToLocal(Vector<T>& global_dir,const Vector<T>* global_pos =  0)      const  = 0;  
};

class UnresolvedSystem : public System
{
    virtual void VectorToLocal(Vector<T>& global_dir,const Vector<T>* global_pos = 0) const
    {
      //do something 
    }  
};

http://www.un.org/Depts/DGACM/index_french.htm

tenplate<typename T>
class Vector
{
  //some functions
};

现在,我想在<编码>系统.h中选取Vector<T>,但我不能这样做,因为这是一个虚拟功能。 我要谈一个工作。 我知道我有<代码>。 Vector To Local Vector<float>,Vector<double>, 以此作为论据,但我不想这样做。

问题回答

成员职能模板不能虚拟。 与此无关。

但是,虚拟成员的职能可以采用完全界定的类型,而这种类型只是使用模板:

class System
{
public:
    virtual void DoIt(vector<int>* v);
};


int main()
{
    vector<int> v;
    System s;
    s.DoIt(&v);
    return 0;
}

因此,你为什么执行自己的病媒类别?

消除虚拟职能的任何共同办法,如CRTP,也将有所帮助。

我不知道你想要做什么,但有时可以把模板功能称为虚拟功能,以实施实际行为。 在此情况下,你将执行你上下层的模板转换,将其称为一种不固定的纯虚拟,不管你想要做什么。 如果你对等距实行限制,这通常只能奏效:

#include "Vector.h"
class System
{
    virtual void print( const std::string & ) const = 0;
    template<class T>
    void VectorToLocal(Vector<T>& global_dir,const Vector<T>* global_pos =  0) const {
        print( T.GetName() );
    }
};

class UnresolvedSystem : public System
{
    virtual void print( const std::string & Name ) const {
        std::cout << name << std::endl;
    }  
};

在这种情况下,我假定<代码>。 T 有一个成员功能代码

如果你界定了C++模板功能,则为每一类模板理由类型设定了新的功能。 因此,单一功能可以是机器编码中的一种功能或背后功能。 这有助于使其迅速发展。

如今,汇编者决定了你的职能的哪几种版本如何使用。 如果<代码>int从来不是一种类型的参数,则汇编者不得被调换成实施。 现在,如果你说话是虚拟的,那么就很难发现它是如何使用的,因此,在汇编使用模板功能的职能时,功能的定义不能放在标题上。 如果没有该职能的源代码,汇编者便可设立汇编的职能。

当C++允许虚拟模板功能时,你会遇到其他一些不现实之处。 类似虚拟职能通常如何执行。

这可能是C++允许它的原因。 你可能认为你需要,但也许还有另一种方式,我相信,如果你给我们一点细节,说明这项法典背后的要求,人们会帮助你找到。

你有几种选择,都取决于你重新努力做什么。

如果<代码>T是<代码>所固有的某种方式。 系统(例如,如果System有成员变量的T),你可将其整个类别模板成:

template< typename T >
class System
{
    virtual void VectorToLocal(Vector<T>& global_dir,const Vector<T>* global_pos =  0)      const  = 0;  
};

template< typename T >
class UnresolvedSystem : public System<T>
{
    virtual void VectorToLocal(Vector<T>& global_dir,const Vector<T>* global_pos = 0) const
    {
      //do something 
    }  
};

如果Ts context is local to Vector To local,那么更好的设计决定是将这一类之外的职能考虑在内:

template< typename T >
void VectorToLocal( System& s, Vector<T>& global_dir, ... )
{
   // use s s public interface to make changes to the object
}

您可以更详细地介绍<代码>的目的。 Vector To local?

如果你想要提供一个单一的执行点,那么可以向模板发出呼吁。

struct base {
   virtual void f( int );
   virtual void f( double );
};

struct derived : base {
   virtual void f( int x ) { f_tmpl(x); }
   virtual void f( double x ) { f_tmpl(x); }

   template <typename T>
   void f_tmpl( T x ) { // ... }
};

我必须说,如果列入一个名单,你实际上可以产生虚拟功能,但这可能只会使守则复杂化。





相关问题
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?

热门标签