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 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 guys, I've been trying to understand the documentation and find an example, but
Hey I am trying to understand the following code snippet. public static void main(String[]
Hey all i am trying to understand how to keep my console app open
Hey i can't seem to understand this error. I'm trying to select an image
Hey I'm trying to return a message when there are no results for the
Hey guys I am having a lot of trouble trying to understand this and
Hey guys, i'm really trying to understand regular expressions while scraping a site, i've
Hey so i'm trying to build the following member Functors of class ConcavePolygon ,
Hey I'm trying to load a Lua file into my c++ project but there
Hey I'm using phonegap databases, and trying to understand why my callback is called

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.