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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:31:16+00:00 2026-06-09T02:31:16+00:00

I have a complex class A: class A{ … }; and class B is

  • 0

I have a complex class A:

class A{
...
};

and class B is include A’s pointer and has a method to return the pointer.

class B{
A* GetA(){return pA;}
A* pA;
};

Then, when I use ‘GetA’ to get a A’s pointer, it is easier to use the illegal pointer, like:

void func(A* p){
B b;
p = b.GetA();
}

when I use ‘func’, I just get an illegal pointer.
The code above is just a sample. And I found it is much easy to make this mistake when I am in a multi-thread environment.

Is there any method to let me sure to avoid these mistakes?

Thanks a lot

  • 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-09T02:31:19+00:00Added an answer on June 9, 2026 at 2:31 am

    There is no general method in C++ to protect yourself from illegal pointer accesses, but there are idioms you can use to help protect yourself.

    Keep your classes as representing concepts/objects and don’t expose direct access to any pointers they might contain. Prefer for example descriptive methods, or for container-like classes begin/end style methods.

    When managing pointers, prefer first to use a standard container such as vector. They’re highly tuned and debugged and will prevent many problems since they already have all the right constructors, assignment, destructor, etc.

    If a standard container isn’t possible prefer a smart pointer such as unique_ptr, scoped_ptr, shared_ptr or weak_ptr depending on your pointer needs. By using an appropriate smart pointer, again you make it almost impossible to unintentionally make coding mistakes.

    Even using all these guidelines you can always bypass any protection you’ve attempted to implement. If you find yourself using a lot of casts you should step back and re-examine your design.

    In your case not only would I make pA not be accessible via accessor, I wouldn’t make it a raw pointer either.

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

Sidebar

Related Questions

I have a complex value object class that has 1) a number or read-only
I have a complex django object, which has properties of other class types. This
I have a class that has a Start method to start a thread that
i have following code #include<math.h> class complex { public: double getRe(); double gerIm(); void
I have a quite complex class hierarchy in which the classes are cross-like depending
I have a class with a complex construction process with many parameters. Multiple clients
I have a PHP class that stores a complex multidimensional array, and rather than
I have two (unrelated) classes. The first one is Point: typedef std::complex<double> complex_number; class
Well i have a complex form view model like this : public class TransactionFormViewModel
I have a pretty complex java class with over 60 small helper methods used

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.