Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

instantiate an abstract class in c++ like in Java

I am stuck on a point at my C++ personal learning process. I come from Java language.

I am trying to set a class in C++ which have an abstract method. Up to there, there is no big deal. But I'd like to instantiate that class as I can do in 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. 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.

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? 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.

like image 1000
Hugo Regibo Avatar asked Mar 23 '16 10:03

Hugo Regibo


People also ask

Can I instantiate an abstract class in Java?

Abstract classes cannot be instantiated, but they can be subclassed. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract .

Why can't we instantiate an abstract class in C?

We can't instantiate an abstract class because the motive of abstract class is to provide a common definition of base class that multiple derived classes can share.

Can you instantiate an abstract class C ++?

An abstract class is, conceptually, a class that cannot be instantiated and is usually implemented as a class that has one or more pure virtual (abstract) functions. A pure virtual function is one which must be overridden by any concrete (i.e., non-abstract) derived class.

Can we instantiate abstract class in C#?

An abstract class cannot be instantiated. An abstract class may contain abstract methods and accessors. It is not possible to modify an abstract class with the sealed modifier because the two modifiers have opposite meanings.


1 Answers

I would say - in c++, the equivalent of your java code would be:

#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...

like image 170
Nim Avatar answered Sep 22 '22 00:09

Nim