Reputation: 695
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.
Upvotes: 10
Views: 2893
Reputation: 33655
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...
Upvotes: 8
Reputation: 33845
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
}
Upvotes: 0
Reputation: 4951
Java, unlike c++, distinguishes between an interface and an abstract class, maybe this is the source of your confusion.
This is how an Java interface looks in c++:
class myClass{
virtual void foo =0; //pure virtual method
}
This is an Java abstract class in c++:
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
}
Upvotes: 0
Reputation: 3836
It is not an instantiation of the MyClass
in your example! 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
).
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)
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. Look here: Does C++0x support Anonymous Inner Classes?
Upvotes: 2