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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:46:15+00:00 2026-05-26T06:46:15+00:00

My first question here… I want to use an abstract class A from which

  • 0

My first question here…

I want to use an abstract class A from which i derive a class B (and classes B2, B3, …). I understood that in order to handle them uniformly, I have to use pointers to the base class, so here variables of type A*.

In the example below, I want to fill aa which is of type vector<A*> in function f. Since I can only use references and the variables lose their scope at the end of f, I cannot access the members of the entries of aa in main anymore.

What could be a solution to this problem?

#include <vector>
#include <iostream>


class A {
    public:
        virtual int getVar(int, int) = 0;
};

class B : public A {
    public:
        B(std::vector<std::vector<int> > var) : var_(var) {};
        int getVar(int i, int j) { return var_[i][j]; }
    private:
        std::vector<std::vector<int> > var_;
};

void f(std::vector<A*>& aa) {
    std::vector<std::vector<int> > var(1, std::vector<int>(1));
    var[0][0] = 42;

    B b(var);

    aa.push_back(&b);
}

int main() {
    std::vector<A*> aa;

    f(aa);

    std::cout << aa[0]->getVar(0, 0) << std::endl;

    return 0;
}
  • 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-26T06:46:16+00:00Added an answer on May 26, 2026 at 6:46 am
    B b(var);
    aa.push_back(&b);
    

    It is pushing the address of the local variable. That is the cause of the problem, because the local variable gets destroyed on returning from the functon, but aa still contains the pointer to the non-existing object. Such a pointer is called dangling pointer using which invokes undefined behavior – which is one of the most dangerous and irritating aspect of C++.

    Use new:

    aa.push_back(new B(var));
    

    Now it should work.

    Important : Don’t forget to make ~A() virtual:

    class A {
        public:
            virtual ~A() {} //MUST DO IT
            virtual int getVar(int, int) = 0;
    };
    

    It is necessary otherwise you wouldn’t be able to delete objects of derived type (in a well-defined way), using the pointers of base type.

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

Sidebar

Related Questions

first question here. I'm developing a program in C# (.NET 3.5) that displays files
First question here: it is a very short yet fundamental thing in Java that
this is my first question here :) I know that I should not check
this is my first question to stackoverflow so here it goes... I use cruise
this is my first question here. Writing some code, i receive this error from
First question here. I am trying to instantiate a generic class using the type
This is my first question here, so I hope that I am providing enough
My first question here, hopefully I'm not doing it wrong. My problem is that
This is my first question here on stackoverflow, so I hope that I am
First question here. Please excuse noob errors. I want to be able to refer

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.