简体   繁体   English

像在Java中一样用c ++实例化一个抽象类

[英]instantiate an abstract class in c++ like in Java

I am stuck on a point at my C++ personal learning process. 我在C ++个人学习过程中遇到了困难。 I come from Java language. 我来自Java语言。

I am trying to set a class in C++ which have an abstract method. 我试图在C ++中设置一个具有抽象方法的类。 Up to there, there is no big deal. 到那里,没有什么大不了的。 But I'd like to instantiate that class as I can do in Java: 但是我想在Java中实例化那个类:

// MyClass has an abstract method named "AbstractMethod"
MyClass class_object = new MyClass()
{
   @Override
   public void AbstractMethod()
   {
     // Do something here
   }
};
class_object.AbstractMethod();

In Java, it works perfectly. 在Java中,它完美地运行。 But i'd like to do the same in C++, and there is a problem here: C++ doesn't seems to like the idea of instantiating a class having virtual method. 但是我想在C ++中做同样的事情,这里有一个问题:C ++似乎不喜欢实例化具有虚方法的类的想法。

I have searched for some days now and I can't find any answer to that question on internet. 我已经搜索了几天,我在互联网上找不到任何答案。 Maybe it is just me badly writting the search sentence, as I am not a native English speaker I might have some difficulties finding the correct syntax on asking that question. 也许只是我写错了搜索句子,因为我不是英语母语人士,在提出问题时我可能会遇到一些困难。

Is there any possibility for me, in C++, to do as in Java or at least, likely? 在C ++中,我是否有可能像在Java中一样,或者至少可能? Is using Templates a solution ? 使用模板是解决方案吗? (never learned templates before, I still have a lot to learn). (之前从未学过模板,我还有很多需要学习的东西)。

Also, I cannot create numerous classes to redefine a method, as this class will be used to do a custom treatment for each instance. 此外,我无法创建许多类来重新定义方法,因为此类将用于为每个实例执行自定义处理。 It would be, I think, a waste to create classes just to see it being the proud father of one and only one object of that type. 我认为,创建课程只是为了让它成为这种类型中唯一一个对象的骄傲之父。

I would say - in c++, the equivalent of your java code would be: 我会说 - 在c ++中,相当于你的java代码将是:

#include <iostream>

struct A {
    virtual void foo() = 0;
};

int main(void) {
    struct B : public A {
        void foo() override {
            std::cout << "inst::foo()" << std::endl;
        }
    };
    A* p = new B;
    p->foo();
}

As you can see there is no such thing as instantiating an abstract class, you must provide a concrete implementation to instantiate. 正如您所看到的那样,没有实例化抽象类,您必须提供实例化的具体实现。 The subtle difference is that in java it's an anonymous class, here it's not... 微妙的区别是在java中它是一个匿名类,这里不是......

It is not an instantiation of the MyClass in your example! 它不是您示例中MyClass的实例化! You just extend it with anonymous inner-class and then instantiate it's instance by this code and syntax (class isn't so much anonymous, though - under the hood it has a name like YourClass$1 ). 您只需使用anonymous inner-class扩展它,然后通过此代码和语法实例化它的实例(类不是那么匿名,但是 - 它的名称类似于YourClass$1 )。

And then you can place a reference of this anonymous YourClass$1 class instance to the variable with MyClass because it is a superclass (you can use Object or some interface type too) 然后你可以使用MyClass将这个匿名的YourClass$1类实例的引用放到变量中,因为它是一个超类(你也可以使用Object或某些接口类型)

C++ 11 doesn't have exactly the same type of inner-classes and extend/instantiate syntax, but you can try to use Lambdas to achieve similar results. C ++ 11没有完全相同类型的内部类和扩展/实例化语法,但您可以尝试使用Lambdas来实现类似的结果。 Look here: Does C++0x support Anonymous Inner Classes? 看这里: C ++ 0x是否支持匿名内部类?

Java, unlike c++, distinguishes between an interface and an abstract class, maybe this is the source of your confusion. 与c ++不同,Java区分接口和抽象类,这可能是您混淆的根源。

This is how an Java interface looks in c++: 这是Java接口在c ++中的外观:

class myClass{
   virtual void foo =0; //pure virtual method
}

This is an Java abstract class in c++: 这是c ++中的Java抽象类:

class myClass{
   virtual void foo=0;
   virtual void abstractMethod(); 
}

Neither of these can be instantiated, you can only inherit from them and override the abstract methods. 这些都不能实例化,您只能从它们继承并覆盖抽象方法。

class myClass_Impl: public myClass
{
   void foo();//must provide an implementation for pure virtual methods
   void abstractMethod(); //override the abstract class method
}

main()
{
   myClass_Impl x; //instace of your class
   myClass * p = new myClass_Impl(); //using polymorphism 
}

The syntax is a little different. 语法略有不同。 But you can return instances of local classes: 但是您可以返回本地类的实例:

class MyAbstractClass {
public:
    virtual void my_method() = 0;
};

MyAbstractClass* some_function() {
    class MyConcreteClass : public MyAbstractClass { // Our local class definition.
    public:
        virtual void my_method() override {
            // some code
        }
    };

    return new MyConcreteClass(); // Return a pointer to an instance of it
}

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

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