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

后端 未结 4 1109
一向
一向 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:47

    When the CompareTo member function is pure virtual, IComparableObject is an abstract class.

    You can't directly copy an object of an abstract class.

    When you pass an object by value you're directly copying that object.

    Instead of passing by value, you can pass by reference to const.

    That is, formal argument type IComparableObject const&.


    By the way, the function should probably be declared const so that it can be called on const object.

    Also, instead of #pragma once, which is non-standard (but supported by most compilers), consider an ordinary include guard.

    Also, when posting code that illustrates a problem, be sure to post exact code. In this case, there's a missing semicolon at the end, indicating manual typing of the code (and so that there could be other typos not so easily identified as such, but instead misidentified as part of your problem). Simply copy and paste real code.


    Cheers & hth.,

提交回复
热议问题