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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:55:51+00:00 2026-05-31T17:55:51+00:00

I have a class User that looks like this: class User { private: char*

  • 0

I have a class User that looks like this:

class User
{
    private:
        char* p_username;
        int nProcesses; 
        struct time
        {
            int mins;
            int secs;
        } totalTime;        
        int longestPID;     
        char* p_longestPath;    
    public:
        User();
        User(const char[],int,int,int,const char[]);
        ~User();
        User operator=(const User&);
        // Other functions
};

And the overloaded assignment operator function is:

User User::operator=(const User &u)
{
    if (this != &u)
    {
        delete [] p_username;
        delete [] p_longestPath;
        p_username = new char[strlen(u.p_username)+1];
        strcpy(p_username,u.p_username);
        nProcesses = u.nProcesses;
        totalTime.mins = u.totalTime.mins;
        totalTime.secs = u.totalTime.secs;
        longestPID = u.longestPID;
        p_longestPath = new char[strlen(u.p_longestPath)+1];
        strcpy(p_longestPath,u.p_longestPath);
    }
    return *this;
}

A sample main program using the assignment operator:

int main()
{
    cout << "\n\nProgram\n\n";

    User u("Username",20,30,112233,"Pathname"),u2;
    u2 = u;
}

When I try to use the assignment operator in the line u2 = u, everything is assigned properly except the dynamic char arrays.

Test output from the end of the operator= function shows that at the end of the assignment itself everything has works perfectly (the usernames and pathnames are correct), however test output from the main function directly after the assignment shows that all of a sudden the char arrays have changed. Suddenly the username of u2 is empty, and the first half of the pathname is garbage.

If at the end of the assignment operator function the username and pathname are perfect, how can they be wrong back in the calling function?

This really has me stumped…

Edit: Here are the constructors

User::User()
{
    p_username = 0;
    nProcesses = 0;
    totalTime.mins = 0;
    totalTime.secs = 0;
    longestPID = -1;
    p_longestPath = 0;
}


User::User(const char UID[],int minutes,int seconds,int PID,const char path[])
{
    p_username = new char[strlen(UID)+1];
    strcpy(p_username,UID);
    nProcesses = 1; 
    totalTime.mins = minutes;
    totalTime.secs = seconds;
    longestPID = PID;
    p_longestPath = new char[strlen(path)+1];
    strcpy(p_longestPath,path);
}
  • 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-05-31T17:55:52+00:00Added an answer on May 31, 2026 at 5:55 pm

    You are returning by value from the assignment function. It is possible that your copy constructor is flawed.

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

Sidebar

Related Questions

I have a connection class for MySQL that looks like this: class MySQLConnect {
I have a web service that looks like this: public class TheService : System.Web.Services.WebService
I have an Entity class that looks like this: <?php namespace Entities; /** @Entity
I have a class that defined a user defined operator for a TCHAR*, like
I have usercontrols which inherit a base class which looks like this: BasePage.cs: using
Here goes: I have a view that looks like this: public interface IAddressView {
I have some JSON data that looks like this: { data: [{ name:John Smith,
I have a class Bar that has a user-defined list of config keys and
I have a class called UserInfo that contains details about a given user. There
I have a Filter Method in my User Class, that takes in a list

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.