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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:35:13+00:00 2026-06-06T00:35:13+00:00

I have functions that return an object, but I’m confused that should I return

  • 0

I have functions that return an object, but I’m confused that should I return the object itself or a pointer to the object?

Here is an example of my function:

CImage CDocument::AddImage(string Name, string fileName)
{
    CImage img = CImage();
    img.Name = Name;
    img.Path = fileName;
    img.IwImage = Iw2DCreateImage(&fileName[0]);

    Images.push_back(&img);

    return img;
}

Is this correct or should I return the pointer to the object:

CImage * CDocument::AddImage(string Name, string fileName)
{
    CImage * img = new CImage();
    img->Name = Name;
    img->Path = fileName;
    img->IwImage = Iw2DCreateImage(&fileName[0]);

    Images.push_back(img);

    return img;
}

Although the last code don’t compile correctly because I got this error:

error C2440: 'initializing' : cannot convert from 'CImage' to 'CImage *'

I think this might be very simple question. I’m very new to c++ so bear with me.

  • 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-06T00:35:15+00:00Added an answer on June 6, 2026 at 12:35 am

    There are multiple problems in this code, the basic difference to understand when coding in C++ is between stack allocation and heap allocation. When you do CImage img = CImage(); the object is created on stack. This object is destroyed automatically when the function ends. Now, if you return a pointer to this object, the caller will be getting a pointer to an invalid memory location as the object is already destroyed. You are also pushing the address of this object into the vector, which again will be invalid once the function ends. Same problem is there with the fileName also.

    To solve this, you need to allocate memory from heap so that object is not destroyed when the function ends. You can allocate objects on heap using new in C++. So your code will become CImage* pImage = new CImage();. Note that in this case it is your responsibility to release the memory using delete. So you can change your function to return CImage* using this technique. You can also push this pointer into the vector Images. Note that to release the memory allocated for the CImage obects, you need to loop through the Images vector and call explicitly call delete on each pointer.

    A better approach would be to use smart pointer such as std::shared_ptr in this case which will manage the calling of delete automatically for you.

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

Sidebar

Related Questions

I have two functions that return simple strings. Both are registered. $.views.helpers({ parseDate: function
I have a function that returns a pointer to an object. Like this: class
Going through happstack-lite tutorial : we build functions that have return type of ServerPart
I have read documentation for functions such as values and define-values that return and
I have a function that return an array (won't work in IE) with two
I have a function that return [[]] , and I want to test the
I have created a function that will return the string path from my DB.
Now I have a function that has to return a string. I saw a
I have a function that uses out parameters to return multiple values to the
I have 1 function that I want to return the address of an assigned

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.