Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3698354
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:58:16+00:00 2026-05-19T04:58:16+00:00

I have a class Interface, that has pure virtual methods. In another class I

  • 0

I have a class Interface, that has pure virtual methods. In another class I have a nested type that inherits from Interface and makes it non-abstract. I use Interface as a type and use the function to initialise the type, but I am getting, cannot compile because of abstract type.

Interface:

struct Interface
{
   virtual void something() = 0;
}

Implementation:

class AnotherClass
{
    struct DeriveInterface : public Interface
    {
        void something() {}
    }

    Interface interface() const
    {
        DeriveInterface i;
        return i;
    }
}

Usage:

struct Usage : public AnotherClass
{
    void called()
    {
        Interface i = interface(); //causes error
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:58:17+00:00Added an answer on May 19, 2026 at 4:58 am

    You use abstract classes as pointer and references, so you’d do

    class AnotherClass
    {
        struct DeriveInterface : public Interface
        {
            void something() {}
        }
    
        DeriveInterface m_intf;
    
        Interface &interface() const
        {
            return m_intf;
        }
    }
    
    struct Usage : public AnotherClass
    {
        void called()
        {
            Interface &i = interface();
        }
    }
    

    plus a couple of semicolons and it will work fine. Note that only pointers and references are polymorphic in C++, so even if Interface were not abstract, the code would be incorrect because of so-called slicing.

    struct Base { virtual int f(); }
    struct Der: public Base { 
       int f(); // override
    };
    
    ...
    Der d;
    Base b=d; // this object will only have B's behaviour, b.f() would not call Der::f
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class Animal and an interface it inherits from IAnimal. @MappedSuperclass public
In my business model I have Entity class (IPoint interface ) that has a
I have a generic class that has one type parameter (T). I needed to
If I have a class that needs to implement an interface but one or
Let's say I have a class that implements the IDisposable interface. Something like this:
I have a factory that returns an interface FormatService : public class FormatServiceFactory {
I'm currently writing a class that implements the SeekableIterator interface and have run into
I have a class that inherits a generic dictionary and an inteface public class
Have a interface class abc { public: virtual int foo() = 0; ... }
Is it possible in C# to have a class that implement an interface that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.