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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:10:35+00:00 2026-05-13T08:10:35+00:00

I am trying to get a grasp on pointers and their awesomeness as well

  • 0

I am trying to get a grasp on pointers and their awesomeness as well as a better C++ understanding. I don’t know why this wont compile. Please tell me what is wrong? I’m trying to initialize the pointer when an instance of the class is created. If I try with a normal int it works fine but when I tried to set it up with a pointer i get this in the console

Running…

Constructor called

Program received signal: “EXC_BAD_ACCESS”.

sharedlibrary apply-load-rules all

Any assistance is appreciated greatly.

Here is the code

#include <iostream> 
using namespace std;
class Agents
{
public:
    Agents();
    ~Agents();
    int getTenure();
    void setTenure(int tenure);
private:
    int * itsTenure;
};
Agents::Agents()
{
    cout << "Constructor called \n";
    *itsTenure = 0;
}
Agents::~Agents()
{
    cout << "Destructor called \n";
}
int Agents::getTenure()
{
    return *itsTenure;
}
void Agents::setTenure(int tenure)
{
    *itsTenure = tenure;
}
int main()
{
    Agents wilson;
    cout << "This employees been here " << wilson.getTenure() << " years.\n";
    wilson.setTenure(5);
    cout << "My mistake they have been here " << wilson.getTenure() <<
             " years. Yep the class worked with pointers.\n";
    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-13T08:10:35+00:00Added an answer on May 13, 2026 at 8:10 am

    You don’t ever create the int that the pointer points to, so the pointer is pointer to an area of memory that doesn’t exist (or is used for something else).

    You can use new to get a block of memory from the heap, new returns the address of the memory location.

    itsTenure = new int;
    

    So now itsTenure holds the memory location you can dereference it to set its value.

    The changed constructor is as follows:

    Agents::Agents()
    {
        cout << "Constructor called \n";
        itsTenure = new int;
        *itsTenure = 0;
    }
    

    But you must also remember to delete it using delete

    Agents::~Agents()
    {
        cout << "Destructor called \n";
        delete itsTenure;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get a better grasp on pointers. My class assignment was to
I'm trying to get a better grasp of the inner workings of background jobs
I'm trying to get a better grasp of structural type dispatch. For instance, assume
hello i am trying to get a better grasp of the views of android
Sorry. Pretty new to this and trying to get a grasp on getting extended
I am new to hibernate queries, and trying to get a grasp on how
Trying to get this to work, with no luck: [DataMember] public Type ParameterType {
While learning c# and wpf, i am trying to get grasp of data binding.
I am trying to get a grasp on layouts, at the moment I'm trying
Overview So I'm trying to get a grasp on the mechanics of neural networks.

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.