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 8992697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:01:07+00:00 2026-06-15T23:01:07+00:00

I have an abstract class implemented by several concrete classes with different memory footprint,

  • 0

I have an abstract class implemented by several concrete classes with different memory footprint, in order to use polymorphism.

#include <iostream>
using namespace std;

class abstractFoo {
public:
    virtual void method() = 0;
};

First concrete class:

class concreteFoo1 : public abstractFoo {
private:
    int member1;
public:
    concreteFoo1() {
        cout << "Constructing Foo1" << endl;
        member1 = 1;
    }
    virtual void method() {
        cout << "Foo1 member: " << member1 << endl;
    }
};

Another concrete class:

class concreteFoo2 : public abstractFoo {
private:
    int member1;
    int member2;
public:
    concreteFoo2() {
        cout << "Constructing Foo2" << endl;
        member1 = 2;
        member2 = 3;
    }
    void method() {
        cout << "Foo2 members: " << member1 << ", " << member2 << endl;
    }
};

What I want to do is to declare an object of abstract type abstractFoo and passing it as a parameters in a function which will create it as an object of either concrete type concreteFoo1 or concreteFoo2. I first use the usual way, with a pointer passed in parameter:

enum typeFoo {FOO1, FOO2};

void createFoo(typeFoo type, abstractFoo *foo) {
    switch (type) {
    case FOO1:
        foo = &concreteFoo1();
        break;
    case FOO2:
        foo = &concreteFoo2();
        break;
    }
}

int main() {
    abstractFoo *foo = new concreteFoo1();
    createFoo(FOO2, foo);
    foo->method();        //Not expected result!
    return 0;
}

Output is:

Constructing Foo1
Constructing Foo2
Foo1 member: 1

The problem is I can’t initialize foo as an object of abstract type, and if I initialize as concreteFoo1, as I did in this example, the foo pointer will still point to it, even after calling createFoo method.

In order to make it works, I was told to pass a pointer to a pointer in parameter:

enum typeFoo {FOO1, FOO2};

void createFoo(typeFoo type, abstractFoo **foo) {
    switch (type) {
    case FOO1:
        *foo = new concreteFoo1();
        break;
    case FOO2:
        *foo = new concreteFoo2();
        break;
    }
}
int main() {
    abstractFoo *foo = new concreteFoo1();
    createFoo(FOO2, & foo);
    foo->method();        //Expected result
    return 0;
}

Output:

Constructing Foo1
Constructing Foo2
Foo2 members: 2, 3

Well, this solution work, but I am not very pleased by it: I still can’t create a pointer to an abstract type, so I have to construct an object I will not use in astractFoo* foo = new concreteFoo1(), and the memory allocated to it is never freed, so I think I will end with a memory leak.

Is there a way to create AbstractFoo** , a pointer to a pointer to an abstract type, without constructing an object?

So, can you confirm that a double pointer is the rigth solution to my problem, and if so, can you answer my two problems? If not, how should I do?

  • 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-06-15T23:01:09+00:00Added an answer on June 15, 2026 at 11:01 pm

    Well, I just found that you can create pointer with operator new, so I answer my own question:

    enum typeFoo {FOO1, FOO2};
    void createFoo(typeFoo type, abstractFoo **foo) {
        switch (type) {
        case FOO1:
            *foo = new concreteFoo1();
            break;
        case FOO2:
            *foo = new concreteFoo2();
            break;
        }
    }
    int main() {
        abstractFoo **foo = new abstractFoo*;
        createFoo(FOO2, foo);
        abstractFoo *foo2Ptr = *foo;
        foo2Ptr->method();        //Expected result
        delete foo2Ptr;
        createFoo(FOO1, foo);
        abstractFoo *foo1Ptr = *foo;
        foo1Ptr->method();        //Expected result
        delete foo1Ptr;
    
        return 0;
    }
    

    Output:

    Constructing Foo2
    Foo2 members: 2, 3
    Constructing Foo1
    Foo1 member: 1
    

    Any comment, especially if I should use reference or smart pointers?

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

Sidebar

Related Questions

Imagine I have a abstract FriendEvent model which has several different concrete implementations, ie.
I have several Java interfaces/ABCs/classes: public abstract class Target { public abstract void fire(Load
I have the following Strategy Pattern implemented: public abstract class RetrievalStrategy { public abstract
In C#, I have a class hierarchy with a couple of abstract base classes
I have an abstract class A and several implementations of it. I expect this
I have the following problem: 1) There is some abstract class A with several
I have several Abstract Base Classes which act like interfaces as known from Java
I have several Entity classes. Now I want to use @PostLoad in every entity.
I have an abstract class that gets implemented by Java and by Android. This
In Java, I have an abstract class with a method implemented (not abstract) which

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.