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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:45:15+00:00 2026-05-17T16:45:15+00:00

hey i am trying to understand how to overload the operator= when there is

  • 0

hey i am trying to understand how to overload the operator= when there is an inheritance
with no Success.
code example:

class Person 
{
    private:
            char* m_name;
            char* m_lastName;
    ..... 
    public:
            virtual Person& operator=(const Person& other);
};
/********************/
cpp implementation
/********************/
#include "Person.h"
Person& Person:: operator=(const Person& other)
{
     if(this == &other)
         return *this;
     delete[] m_name;
     delete[] m_lastName;
     if (other.m_name!=NULL)
     {
         m_name = new char[strlen (other.m_name)+1];
         strcpy(m_name,other.m_name);
     }
     if (other.m_lastName!=NULL)
     {
         m_lastName = new char[strlen (other.m_lastName)+1];
         strcpy(m_lastName,other.m_lastName);
     }
     return (*this);
}

now lets say Student inherit from Person how should the = operator should be implemented
i think it should be like the following please correct me cause i am probably being wrong:

#include "Person.h"
class Student : public Person
{
    private:
            char* m_collageName;
    ..... 
    public:
            virtual Person& operator=(const Person& other);
};
/********************/
cpp implementation
/********************/
#include "Student.h"
Person& Student:: operator=(const Person& other)
{
     if(this == &other)
         return *this;
     Person::operator=(other);
     delete[] m_collage;
     if ((Student)other.m_collageName != NULL)
     {
        m_collageName = new char[strlen((Student)other.m_collageName)+1];
        strcpy(m_collageName,(Student)other.m_collageName);
     }
     return (*this);
}

Thanks alot in advance much appriciate it.

  • 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-17T16:45:16+00:00Added an answer on May 17, 2026 at 4:45 pm

    Your implementation is not typesafe, because I can write:

    Student s;
    Person p;
    s = p;
    

    And it will compile the assignment successfully, but will result in U.B. (likely segfault or garbage read) at runtime, because you downcast the right argument of operator= from Person to Student, while it is not one.

    More generally, a virtual assignment operator doesn’t really make sense, since you’ll have to define assignment from Person (rather than a specific subclass) in all derived classes, and it is very unlikely that it is a meaningful operation for them.

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

Sidebar

Related Questions

Hey all i am trying to get my code below to work in order
Hey i am trying to compare the date on which the user opens the
hey i am trying to detect leaks in visual studio using : #define _CRTDBG_MAPALLOC
Hey guys I am trying to implement the twitter for posting the comments about
Hey guys I'm trying to figure how pointers are returned by strcat(), so I
Hey guys. I'm trying to configure TFS2010 on a home server. I want to
Hey all, I'm currently trying to write a compile-time string encryption (using the words
Hey, I am trying to type a message into notepad without having to have
Scrum development is based on listing user stories and implementing them in during sprints.
I'm sorry for the title nad no idea what to name if, anyone mind

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.