C++ Abstract class can't have a method with a parameter of that class

后端 未结 4 1102
一向
一向 2021-02-04 04:25

I created this .h file

#pragma once

namespace Core
{
    class IComparableObject
    {
    public:
            virtual int CompareTo(IComparableObject obj)=0;
          


        
4条回答
  •  南方客
    南方客 (楼主)
    2021-02-04 04:53

    You are trying to pass obj by value. You cannot pass an abstract class instance by value, because no abstract class can ever be instantiated (directly). To do what you want, you have to pass obj by reference, for example like so:

    virtual int CompareTo(IComparableObject const &obj)=0;
    

    It works when you give an implementation for CompareTo because then the class is not abstract any longer. But be aware that slicing occurs! You don't want to pass obj by value.

提交回复
热议问题