10

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.

8
  • 12
    You are not instantiating an abstract class. You are instantiating a concrete subclass of an abstract class Commented Mar 23, 2016 at 10:26
  • 1
    afaik, you cannot instantiate an abstract class (neither in java nor in c++). But you definitely can instantiate a class with a virtual method in c++ Commented Mar 23, 2016 at 10:26
  • 1
    C++ doesn't seems to like the idea of instantiating a class having virtual method., make that a pure virtual method. Commented Mar 23, 2016 at 10:26
  • 3
    You can not instance an abstract class by definition. The code you show has an anonymous class. Commented Mar 23, 2016 at 10:27
  • 1
    can you please show the c++ code that is not working? Because what you describe should be perfectly possible in c++ (well not instantiating an abstract class, but thats also not really what you show in the java example) Commented Mar 23, 2016 at 10:28

4 Answers 4

7

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

Sign up to request clarification or add additional context in comments.

6 Comments

Might want to add a delete p; at the end of main(). C++ does not have garbage collection, so forgetting to release a dynamically allocated object is (at best) a poor idea and (at worst) a really bad idea in C++.
Awesome, big thanks to you all for the informations and sorry for the wrong terms that made me be misunderstood. Cheers !
Including the new type within the method just adds more confusion to an already difficult language. You could add the new type elsewhere to make it slightly more coherent.
@DaveDoknjas The point of this isn't to highlight the best approach (that's subjective and debatable.) The point of this is to show the equivalent of the java code the op posted...
@Nim: Yes, it is the closest equivalent, but is also one of the worst 'features' of C++.
|
2

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?

Comments

0

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 
}

Comments

0

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
}

2 Comments

That is not returning an instance of MyAbstractClass though. It is returning a pointer which points at an instance of a derived class, which is a different thing.
About the same thing happens in java. And like I said "return instances of local classes" or pointer to in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.