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

  • SEARCH
  • Home
  • 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 697527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:10:38+00:00 2026-05-14T03:10:38+00:00

take this simple code: class A{ public: virtual void foo() = 0; void x(){

  • 0

take this simple code:

class A{ 
  public: 
  virtual void foo() = 0; 
  void x(){ foo(); }
};
class B: public A{ foo(){ ... } };

main(){
  B b;
  b.x();
}

What I want is to build an abstract class that will have a function that will call a function expecting it to be implemented in the derived class

The question is that I can’t seem to make that work, the compiler says it can’t compile because it can’t find the reference(or something like that) to the foo() to be executed in x() of the base class. Can this work? Can anyone give me an example of this?

EDIT: It seems that it just doesn’t work when the “foo();” is inside the destructor of class A(the base one)…
It just got me confused. =[

EDIT2: how interesting this got. I just created a callfoo(){ foo(); } and now it compiles ok, but if I try to call the pure abstract function directly from within the destructor of Base class A, it gives me errors… weird. Anyone has any idea of this? O_o

any help on this please?

Thanks,
Jonathan

Update

It worked outside the destructor. Now I just got confused.

Try putting the “foo()” inside the destructor of the A(base) class, at least for me is not compiling…

any help plz?

  • 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-14T03:10:39+00:00Added an answer on May 14, 2026 at 3:10 am

    There is nothing preventing you from doing that:

    struct A {
        virtual ~A() {}
        virtual void f() = 0;
        virtual void g() { f(); }
    };
    
    struct B : A {
        void f() { std::cout << "B::f()" << std::endl; }
    };
    
    // ...
    A* a = new B;
    a->g(); // prints "B::f()"
    

    As for calling a pure virtual function from the destructor (or constructor): Don’t! It invokes undefined behaviour.

    §10.4/6:

    Member functions can be called from a constructor (or destructor) of an abstract class; the effect of making a virtual call (10.3) to a pure virtual function directly or indirectly for the object being created (or destroyed) from such a constructor (or destructor) is undefined.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please take a look at the following simple code: class Foo { public: Foo(){}
Why doesn't object.__init__ take *args, **kwargs as arguments? This breaks some simple code in
I am trying to make a simple alarm. in this code portion a take
I have this very simple br2nl function that I use to take a string
Take this sample class as an example: [AttributeUsage(AttributeTargets.All, AllowMultiple=true)] public class BugFixAttribute : System.Attribute
Good morning. Take this simple scenario: I've got a NetClient that connects itself to
I am writing a simple parser that will take a string of the format
I have a problem with This code in C# : main class { var
I have simple User entity: public class User { public virtual int Id {
Take a simple XML file formatted like this: <Lists> <List> <Note/> ... <Note/> </List>

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.