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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:28:39+00:00 2026-06-11T12:28:39+00:00

im trying to return a local object from a function and i have this

  • 0

im trying to return a local object from a function

and i have this problem

if i return the locally created object it returns null

DString DString :: operator + (const char* param)
{
    // Variable definition
    int nSize = ( this->GetLength() + (strlen(param)));
// Create a new char array in the opropriate size
char* cstrNewString = new char[nSize + 1];

// Append data
strcpy(cstrNewString, this->_cstrString);
strcat(cstrNewString, (char*)param);
cstrNewString[nSize] = '\0';

// Create a new DString to return
DString dstNewData(cstrNewString);

// Clean up
delete[] cstrNewString;

// Return data
return (dstNewData); // Null!

if i do the same exact thing only creating a new object in the return line such as this:

DString DString :: operator + (const char* param)
{
    // Variable definition


int nSize = ( this->GetLength() + (strlen(param)));

// Create a new char array in the opropriate size
char* cstrNewString = new char[nSize + 1];

// Append data
strcpy(cstrNewString, this->_cstrString);
strcat(cstrNewString, (char*)param);
cstrNewString[nSize] = '\0';

// Create a new DString to return
DString dstNewData(cstrNewString);

// Clean up
delete[] cstrNewString;

// Return data
return (DString(dstNewData.ToCharArray())); // Not null, returns correctly!

it returns correctly.. why is it doing this and how can i fix this ?

  • 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-11T12:28:40+00:00Added an answer on June 11, 2026 at 12:28 pm

    How is your copy constructor defined? In the first case, the copy
    constructor is called; in the second no. You don’t show us any of the
    essential code for DString, but given the apparent semantics, it seems
    almost certain that the compiler generated copy constructor will not
    do the right thing. If you’re DString contains a dynamically
    allocated pointer (and I don’t see how it could be otherwise), which is
    deleted in the destructor, the copy constructor must make a deep copy.

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

Sidebar

Related Questions

Im trying to return the value that a $ajax call returns, from a function
Trying to reproduce the problem from this question , I've found I can't plot
I'm trying to return a left child as a pointer I have template <typename
I am trying to return the value by using unix_timestamp function but it behaves
I am trying to return two json sets from java which each contain key/value
I'm trying to return use a JSON object with handlebars. Making a small todo
I am trying to return twitter titles based on today's date only. I have
I'm trying to get a list of JSON objects (products) from a local file
I am trying to create my own Perl module in /usr/local/lib/perl I have the
I'm trying to get an Image from a WCF service. I have an OperationContract

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.