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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:21:36+00:00 2026-06-03T09:21:36+00:00

I wish for a class A to contain some data, and class B will

  • 0

I wish for a class A to contain some data, and class B will contain a pointer to that data. I am providing access to the data through a function returning a reference to the data object A. If I create an object B then i can access the object A, however if create a pointer to B then the equivalent operation produces a segmentation fault. Like this:

#include <iostream>
#include <vector>

class A {
public:
    A(const int pInt) {mInt = pInt;}
    void print() {std::cout << mInt << std::endl;}
private:
    int mInt; //some data
};

class B {
public:
   B() {mP1 = new A(1);} //initialise to 1
   ~B() {delete mP1;}
   A& access() {return *mP1;} //return reference to the data

private:
    A* mP1; //pointer to some data
};

int main() {
    B vB;
    vB.access().print(); //this works.

    B *vBptr;
    vBptr->access().print(); //Segmentation fault!

    std::vector<B*> vVec;
    vVec.resize(1);
    vVec[0]->access().print(); //Segmentation fault!
}

I guess when creating B *vBptr then a B object is not getting initialised?
How then could I create a vector of pointers to B objects that are automatically initialised?

Cheers.

  • 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-03T09:21:39+00:00Added an answer on June 3, 2026 at 9:21 am

    Two issues in your program

    B *vBptr;
    

    Your pointer has not been initialized, and though its a pointer of type B but is not pointing to a valid object of type B. Change it to

    B *vBptr = new B();
    
    vVec.resize(1);
    

    Here resizing does not allocate the storage for each of the elements of the vector. You can initialize the elements with the actual object by supplying the initialization to the resize method of the vector

    vVec.resize(1,new B());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Qt and wish to write a class that will perform some
I have a class that includes a std::list and wish to provide public begin()
I've created a class library which exposes my back-end object model. I don't wish
What I wish to do is have a class that contains a map of
I have a custom class that I wish to save and load. The class
I have some objects that I wish to cache on disk. I use serialize()
We have a vendor-provided Jar that contains a class we wish to extends and/or
I wish to define the following typeclass Mapping : {-# LANGUAGE MultiParamTypeClasses #-} class
I wish to use JSON.Net on a brand spankin new Mono class library (say
I have written a base class from which I wish to derive several child

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.