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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:18:13+00:00 2026-06-14T07:18:13+00:00

Possible Duplicate: C++ – when should I use a pointer member in a class

  • 0

Possible Duplicate:
C++ – when should I use a pointer member in a class

I’ve worked with c++ for years but I still have questions I havent answered..
I am defining a c++ class but I have some doubts about this

question 1: should I store investment by using pointer or no?

Investment* investment;
// or
Investment investment;

question 2: why?

question 3: in case I use Investment investment; should I return a reference this way Investment & getInvestment();?

I know that both ways my code works but I would like to know the efficient way to do this..

the code follows:

class Urgency
{
private:
    Investment* investment;
public:
    Urgency(Investment* investment);
    ~Urgency(void);

    Investment* getInvestment();
};
  • 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-14T07:18:13+00:00Added an answer on June 14, 2026 at 7:18 am

    I typically prefer to use pointer when the object is large and to use a stack object when it is small. Pointers allow for some things such as lazy init and forward declaration, while stack objects reduce the likely hood you’ll need to create your own copy constructor and having to worry about explicitly destroying the object in the destructor. (although, you could use a shared pointer type object to do this for you) Also, a stack object is typically faster to access, but in most scenarios this shouldn’t be a reason for choosing one over the other as the difference in most applications is negligible.

    In reference to your code, you typically would return a const Investment & in order to not allow direct modifications to Investment object. As mentioned above, if you use a raw pointer, you should create a copy constructor and a destructor that properly manage the memory on the object. With a pointer object you would typically return a const Investment * again to remove the ability for an outside caller to directly modify your object.

    Take notice of how your functions are declared in the Investment class when you return const correct Investment objects. If you simply have Investment::getNumShares(), you will not be able to access the function using a const reference or const pointer. To fix this issue, mark all member functions that do not modify the object to be const such as Investment::getNumShares() const

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

Sidebar

Related Questions

Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: What is 0x10 in decimal? I notice that Console.WriteLine(18); writes 18, but
Possible Duplicate: replace all occurrences in a string I found this question/answer: Use JavaScript
Possible Duplicate: What is the fastest XML parser in PHP? I still need to
Possible Duplicate: regex for URL including query string I have a text or message.
Possible Duplicate: How to call a JavaScript function from PHP? I have a php
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: Singleton: How should it be used Following on from Ewan Makepeace 's
Possible Duplicate: Trying to get tables next to each other horizontal I have two

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.