English 中文(简体)
我的虚拟功能的原型是什么?
原标题:What should be my virtual function s prototype?

请允许我说,我有一个抽象的基础类别:Base,具有虚拟功能doSomething(

有两个衍生类别,其中一个在<编码>oSomething()中不考虑参数,而另一个类别则将结构和分类作为参数。

A function in another class (SomeClass) calls doSomething() using a Base* variable. It also needs to pass the parameters i mentioned for DerivedTwo.

如何选择原型,而不在操作时间用一只手脚检查物体类别?

谢谢。

    class Base {
      public:
      void virtual doSomething(); 
    }

    class DerivedOne : Base {
      public:
      void doSomething(int a,struct b);
    }

    class DerivedTwo : Base {
      public:
      void doSomething();
    }
最佳回答

这样做的一个途径是:

class Base {
public:
    Base();
    virtual ~Base();
    virtual void doSomething();
};

class DerivedOne : public Base {
public:
    DerivedOne();
    void doSomethingElse(int a,struct b);
};

class DerivedTwo : public Base {
public:
    DerivedTwo();
    virtual void doSomething();
};

然后,你可以使用<条码>动力学-cast来确定操作时间的类型,因为你在<条码>上似乎有某种类型的有条件表述。 这些方法并不相同,根本上有所不同。 此外,Derived One:doSomething将隐藏Base:doSomething

<>Update>

正如其他人已经指出的那样,如果你的方案依靠的是类型的有条件的表述,它常常是坏的。 由于你的榜样没有足够的环境来提供适当的解决办法,我们难以在这方面帮助你。 如果你有兴趣去除这种类型的关系,解决这一问题的许多潜在解决办法之一是:

class Base {
public:
    Base();
    virtual ~Base();
    virtual void doSomething();
};

class DerivedOne : public Base {
public:
    DerivedOne();
    // ...
    virtual void doSomething(); // << no parameters required.
                                // they have moved to member data:
private:
    int a;
    b another;
};

class DerivedTwo : public Base {
public:
    DerivedTwo();
    virtual void doSomething();
};

那么,你就可以从你的方案中删除“有条件”的表述。 如果你认为更合适的话,如果你愿意在这方面提供帮助的话,就可自由地在这里提问,或提出一个新问题。

问题回答

You are wanting to change the parameter list of a virtual method in a derived class. This cannot be done. Usually when you find yourself wanting to do this it indicates that your class hierarchy design is incorrect.

解决这一问题的无休止尝试通常涉及在基类中增加一些内容,而基类对于某些衍生的阶层来说只是有意义的。 这违反了良好设计的原则。

更妥善地处理这个问题有多种不同的方式,但很难根据这一人为树立的榜样提出建议。

如果你需要将这两个衍生型的变量加以利用,那么就在任何地方宣布这些变量相同:

class Base {
  public:
  void virtual doSomething(int a,struct b); 
}

class DerivedOne : Base {
  public:
  void doSomething(int a,struct b);
}

class DerivedTwo : Base {
  public:
  void doSomething(int a,struct b);
}

如果你需要使用某类参数,而不是其他类型参数,那么你所需要的不是一类等级。 在这方面,你需要更多地阐述你的问题。

我们能够解决这个问题,例如:

class Base {
  public:
  void virtual doSomething();
  void virtual doSomething(int a,struct b); 
}

class DerivedOne : Base {
  public:
  void doSomething(int a,struct b);
}

class DerivedTwo : Base {
  public:
  void doSomething();
}

您可以使用RTTI,而不是如果/或这样的话,你可以即时使用Derived One和Derived 之后,两位阁下将基地的点子投向了两人(Derived One and DerivedTwo),结果之一是NUL。 之后,你检查了想要的 Der子是否不是民族解放军,如果你称职的话:

DerivedOne *pDrvOne=dynamic_cast<DerivedOne*>(pBase);
DerivedTwo *pDrvTwo=dynamic_cast<DerivedTwo*>(pBase);
then: if(pDrvOne)pDrvOne->deSo,ething(a,b); if(pDrvTwo) pDrvTwo->doSomething();

* Remeber the use of RTTI will undermine the principles of polymorphism. in your example you didn t overload doSomething() in DerivedOne subclass: "to overload a function is to use the same signature(name of function and the same number and type of parameters)". but you just overload it in DerivedTwo. thus consider this example:

Base *pBs;
pBs=new DerivedOne; pBs->doSomething(a,b); the result a call to base doSomthing!!!

* 解决办法是设法了解贵阶层将要解决的问题。

类似于塔拉斯的例子,只是在塔拉斯的基地和DerivedTwo的原型添加了缺省值。 这样,使用基地阶级或DerivedTwo阶级的法典的其余部分就不得不改变。





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

热门标签