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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:07:41+00:00 2026-06-10T14:07:41+00:00

For a long time now I’ve perceived pointers, new and delete somewhat unnecessary in

  • 0

For a long time now I’ve perceived pointers, new and delete somewhat unnecessary in C++ unless dealing with long-lived objects, with references being a cleaner alternative that fits better in the RAII model. However, I am still unable to ascertain how to avoid pointers when using dynamic polymorphism in C++.

Supose we have these clasess:

class A
{
public:
    virtual void a() const = 0;
};

class B : public A
{
    virtual void a() const
    {
        std::cout << "B";
    }
};

class C : public A
{
    virtual void a() const
    {
        std::cout << "C";
    }
};

void invoke(const A& obj)
{
    obj.a();
}

int main()
{
    B b;
    invoke(b); // Prints B
}

Object can be passed to invoke as a reference and there are no pointers involved (well, at least from the programmer’s perspective). However, the above example is essentially static polymorphism.

If I wanted to make the type of b dependent on something else, I would have to use pointers:

int main()
{
    A* a;
    if (something)
        a = new B;
    else
        a = new C;

    invoke(*a); // Prints B
    delete a;
}

This looks ugly as hell to me. Sure, I could use smart pointers:

int main()
{
    std::unique_ptr<A> a;
    if (something)
        a.reset(new B);
    else
        a.reset(new C);
    invoke(*a); // Prints B
}

But smart pointers are just wrappers for pointers.

I would like to know if there is a way to avoid this and utilize polymorphic classes without the use of pointers.

  • 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-10T14:07:43+00:00Added an answer on June 10, 2026 at 2:07 pm

    You can’t avoid pointers for this. If you don’t like them C++ isn’t going to be the language for you, because if you want to do anything polymorphic you’re going to have to use pointers to get past the most trivial uses. Constructing objects on the heap, i.e. using new is how you avoid the scoped lifetime of stack-constructed objects, which you have to do if you want to make things inside conditional branches and then assign them to variables in a parent scope – if you don’t need to do that you also don’t actually need polymorphism because your types are all determinable at compile time. There’s no way around this.

    Use smart pointers of course, they can really help avoid issues with pointer lifecycles, but there’ll be pointers in there somewhere no matter what really cool abstractions you end up using.

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

Sidebar

Related Questions

For a long time now I have noticed something annoying when working on Web
I been using VS2008 Pro for a long time now but since of licensing
I have searched for this for a long time now... I am writing an
I have tried for a very long time now to figure out, why this
This question has been bugging me for a long time now but essentially I'm
This question has been puzzling me for a long time now. I come from
I'm aware of server-side javascript for a long time now, but I don't have
I've been working on this for a long time now and can't figure out
Ladies and Gentlemen, For quite a long time now, I've been using the following
I've been using SVN since a long time and now we're trying on Git.

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.