Home > Backend Development > C++ > Why Does Slicing Occur in C Polymorphism, and How Can It Be Avoided?

Why Does Slicing Occur in C Polymorphism, and How Can It Be Avoided?

Patricia Arquette
Release: 2024-11-28 05:35:22
Original
918 people have browsed it

Why Does Slicing Occur in C   Polymorphism, and How Can It Be Avoided?

Polymorphism and Slicing in C : Decoding the Confusion

In C , polymorphism allows objects derived from a base class to exhibit different behaviors. However, a common pitfall, known as "slicing," can lead to unexpected results.

Question:

Consider the following code snippet:

class Animal {
    virtual void makeSound() { cout << "rawr" << endl; }
};

class Dog : public Animal {
    virtual void makeSound() { cout << "bark" << endl; }
};

int main() {
    Animal animal;
    animal.makeSound();

    Dog dog;
    dog.makeSound();

    Animal badDog = Dog();
    badDog.makeSound();

    Animal* goodDog = new Dog();
    goodDog->makeSound();
}
Copy after login

The output is:

rawr
bark
rawr
bark
Copy after login

Why does the output for badDog not print "bark"?

Answer:

This is an example of slicing. When you pass a Dog object to the Animal constructor (like in badDog = Dog()), the Animal part of the Dog is copied into badDog. However, since badDog is of type Animal, it can only access the Animal methods, which default to printing "rawr."

To achieve polymorphism, you must use pointers or references to point to the derived class object. In this case, goodDog is a pointer to a Dog object, so it can access the Dog methods and print "bark."

Implications:

Slicing can lead to unexpected behaviors and is generally discouraged. Using pointers or references ensures proper polymorphic behavior.

Additionally, remember that C uses value semantics by default, meaning that variables contain the actual objects. This differs from reference semantics in languages like Java and C#, where variables hold references to objects.

The above is the detailed content of Why Does Slicing Occur in C Polymorphism, and How Can It Be Avoided?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template