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

  • Home
  • SEARCH
  • 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 9226457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:50:06+00:00 2026-06-18T04:50:06+00:00

I have spent a couple hours looking for discusion on this both in some

  • 0

I have spent a couple hours looking for discusion on this both in some good C++ books as well as here on stack overflow, and while I have seen quite a number of questions regarding the “heap vs stack” question, I’m looking more specifically for an understanding of the use of pointers or not in creating object members of a class, i.e. with composition.

For example:

class A{
B c;
}

vs.

class A{
B*c;
}

In the first example, this is not really a “stack” allocation, rather an allocation in the “static storage area,” which is a different thing, so discussions of stack vs heap don’t apply, I think.

What’s not clear to me are the pros and cons of either. It seems like most code I read is using the second option, but why?

It’s possible I don’t know the proper terminology of these techniques to search this site properly, or else there simply haven’t been questions on this. If there are indeed answers pertaining to this elsewhere, by all means let me know how to find them, but nearly everything seems to be more about stack vs heap in the context of a local variable, and I think I have a handle on that okay.

  • 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-18T04:50:07+00:00Added an answer on June 18, 2026 at 4:50 am

    The reason why people use the 2nd option is this:

    class A { B b;}; // error, order of classes wrong
    class B { }; 
    

    This simple feature of C++ makes it difficult to build working system with the B b; style. You have to put all related classes to same file and reorder the classes to exactly correct order.

    Also inheritance does some magic with the order too:

    class A : public B { }; // error, order of classes wrong
    class B { };
    

    So using inheritance and B b; together is fixing the order of classes completely and large number of data members will be nightmare. (note that types like float and int does not have the order problem – and it’s possible to do it correctly for classes too)

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

Sidebar

Related Questions

I have spent the last couple of hours looking through other questions on this
I've spent a couple of hours looking at this and my head's spinning. Can
Urgh, I have spent the last couple of hours on this now. I normally
I've spent a couple of hours searching about this error, and I have tested
I have spent last couple of hours searching for answers here but nothing seems
I have spent hours reading a couple dozen different blogs and others q's here
I spent couple of hours to resolve this weird issue. I have date formate
I've spent a good couple of hours on this and could use a second
Ok so I've spent a couple hours trying to resolve this issue and have
I have spent couple of hours but unable to solve this problem. When I

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.