简体   繁体   English

C ++声明对象时可以实现虚方法吗?

[英]C++ Can virtual methods be implemented when declaring an object?

I'm back to working with C++ after quite a long while using Java almost exclusively, so I'm hitting snags when trying to do things I could do in Java but can't in C++. 经过很长一段时间我几乎完全使用Java后,我又回到了使用C ++的时候,所以当我尝试用Java做的事情但却不能用C ++做的事情时,我会遇到麻烦。

In this case, in Java I could do something like this: 在这种情况下,在Java中,我可以这样做:

class MyClass
{
   public void myMethod();
}

// -------

MyClass myObject = new MyClass(){
    myMethod()
    {
      // Do stuff
    }
};

In essence, I'm implementing the logic for myMethod() when declaring the MyClass object (Note that my syntax might be off). 本质上,我在声明MyClass对象时实现myMethod()的逻辑(注意我的语法可能已关闭)。

I'm trying to find out if there's an equivalent for C++ 我试图找出是否有相当于C ++的东西

My specific use case is a set of tests for a Server/Client architecture where the Test objects extend the base Server/Client abstract classes and implement the virtual methods controlling their behaviour. 我的具体用例是一组服务器/客户端体系结构的测试,其中Test对象扩展基本的Server / Client抽象类并实现控制其行为的虚拟方法。 I need to modify this behaviour for each specific test, and it would come in handy to be able to "inline" these implementations when declaring the Test objects, rather than having to implement several variations on the Test object for each specific test. 我需要为每个特定的测试修改此行为,并且在声明Test对象时能够“内联”这些实现,而不是必须为每个特定测试实现Test对象的多个变体,这样做会很方便。

Thanks! 谢谢!

EDIT: I've implemented Smeeheey 's answer as follows, and works like a charm 编辑:我已经实现了Smeeheey的答案如下,并且像一个魅力

int main(...)
{

   class TestServer : public BaseServer
   {
      public:
         TestServer(...) : BaseServer(...){...} //Constructor

         void virtualFunction(...){...} // Extended function
   };

   TestServer testInstance(...);

   // Execute tests using testInstance object

} }

You can actually come pretty close, with an anonymous local struct/class: 实际上你可以非常接近,使用匿名的本地结构/类:

class MyClass
{
public:
    virtual void doStuff() = 0;
};

void doStuffWithMyStruct(MyClass& s)
{
    s.doStuff();
}

int main()
{
    class: public MyClass
    {
    public:
        void doStuff() override
        {
            std::cout << "Hello" << std::endl;
        }
    } s;

    doStuffWithMyStruct(s);

    return 0;
}

Note that in the above, the variable s has an anonymous type, very similar to Java. 请注意,在上面,变量s具有匿名类型,与Java非常相似。 Its functions can be accessed in a reference to its base type. 可以通过引用其基本类型来访问其函数。 Live demo here . 现场演示这里

No, there's no equivalent in C++, nor could there be for the way classes are commonly used in C++. 不,C ++中没有等价物,也不存在C ++中常用类的方式。

In Java, this creates a new class type which derives from MyClass and overrides myMethod() . 在Java中,这将创建一个新的类类型,它派生自MyClass并覆盖myMethod() This works, because variables of type MyClass are just references, so those variables can hold references to derived classes. 这是有效的,因为MyClass类型的变量只是引用,因此这些变量可以保存对派生类的引用。

In C++, variables of type MyClass are not references. 在C ++中, MyClass类型的变量不是引用。 They must be of type MyClass , nothing else. 它们必须是MyClass类型,没有别的。 While it is possible for MyClass & to hold a reference to a class that derives from MyClass , the use of references (or pointers) in this case is very unidiomatic C++. 虽然MyClass &可以保存对从MyClass派生的类的引用,但在这种情况下使用引用(或指针)是非常单一的C ++。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM