How to avoid calling function in base class?










1















I have two classes A and B like below, B have to inherit from A. The classType() function of class B is not provided, If call B::classType(), will get the class type of A. I would like to get a compiler error here instead of calling into the classType() function of class A. How do I do it?



In general, the sub classes of A should provide the function classType(), in case the author forgets to write the classType() function, it's best to report a compiler error. This is the purpose. Maybe, I can use static_assert in TypeOf() function.



class A

public:
static const char* classType()

static const char* s_classType = "A";
return s_classType;

;

class B : public A

;

template <class T>
const char* TypeOf()

return T::classType();


int main()

const char* type = TypeOf<B>(); // I would like to get compiler error here, not get "A".










share|improve this question
























  • Prefer std::string to const char*.

    – Ron
    Nov 15 '18 at 16:09











  • void main?? -

    – Quentin
    Nov 15 '18 at 16:11











  • Solution: replace class B : public A with class B.

    – Evg
    Nov 15 '18 at 16:12











  • @Evg, B needs to inherits form A in the case.

    – ldlchina
    Nov 15 '18 at 16:14






  • 1





    See edited answer.

    – Evg
    Nov 15 '18 at 16:39















1















I have two classes A and B like below, B have to inherit from A. The classType() function of class B is not provided, If call B::classType(), will get the class type of A. I would like to get a compiler error here instead of calling into the classType() function of class A. How do I do it?



In general, the sub classes of A should provide the function classType(), in case the author forgets to write the classType() function, it's best to report a compiler error. This is the purpose. Maybe, I can use static_assert in TypeOf() function.



class A

public:
static const char* classType()

static const char* s_classType = "A";
return s_classType;

;

class B : public A

;

template <class T>
const char* TypeOf()

return T::classType();


int main()

const char* type = TypeOf<B>(); // I would like to get compiler error here, not get "A".










share|improve this question
























  • Prefer std::string to const char*.

    – Ron
    Nov 15 '18 at 16:09











  • void main?? -

    – Quentin
    Nov 15 '18 at 16:11











  • Solution: replace class B : public A with class B.

    – Evg
    Nov 15 '18 at 16:12











  • @Evg, B needs to inherits form A in the case.

    – ldlchina
    Nov 15 '18 at 16:14






  • 1





    See edited answer.

    – Evg
    Nov 15 '18 at 16:39













1












1








1








I have two classes A and B like below, B have to inherit from A. The classType() function of class B is not provided, If call B::classType(), will get the class type of A. I would like to get a compiler error here instead of calling into the classType() function of class A. How do I do it?



In general, the sub classes of A should provide the function classType(), in case the author forgets to write the classType() function, it's best to report a compiler error. This is the purpose. Maybe, I can use static_assert in TypeOf() function.



class A

public:
static const char* classType()

static const char* s_classType = "A";
return s_classType;

;

class B : public A

;

template <class T>
const char* TypeOf()

return T::classType();


int main()

const char* type = TypeOf<B>(); // I would like to get compiler error here, not get "A".










share|improve this question
















I have two classes A and B like below, B have to inherit from A. The classType() function of class B is not provided, If call B::classType(), will get the class type of A. I would like to get a compiler error here instead of calling into the classType() function of class A. How do I do it?



In general, the sub classes of A should provide the function classType(), in case the author forgets to write the classType() function, it's best to report a compiler error. This is the purpose. Maybe, I can use static_assert in TypeOf() function.



class A

public:
static const char* classType()

static const char* s_classType = "A";
return s_classType;

;

class B : public A

;

template <class T>
const char* TypeOf()

return T::classType();


int main()

const char* type = TypeOf<B>(); // I would like to get compiler error here, not get "A".







c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 19:41









Evg

4,01221436




4,01221436










asked Nov 15 '18 at 16:08









ldlchinaldlchina

4161823




4161823












  • Prefer std::string to const char*.

    – Ron
    Nov 15 '18 at 16:09











  • void main?? -

    – Quentin
    Nov 15 '18 at 16:11











  • Solution: replace class B : public A with class B.

    – Evg
    Nov 15 '18 at 16:12











  • @Evg, B needs to inherits form A in the case.

    – ldlchina
    Nov 15 '18 at 16:14






  • 1





    See edited answer.

    – Evg
    Nov 15 '18 at 16:39

















  • Prefer std::string to const char*.

    – Ron
    Nov 15 '18 at 16:09











  • void main?? -

    – Quentin
    Nov 15 '18 at 16:11











  • Solution: replace class B : public A with class B.

    – Evg
    Nov 15 '18 at 16:12











  • @Evg, B needs to inherits form A in the case.

    – ldlchina
    Nov 15 '18 at 16:14






  • 1





    See edited answer.

    – Evg
    Nov 15 '18 at 16:39
















Prefer std::string to const char*.

– Ron
Nov 15 '18 at 16:09





Prefer std::string to const char*.

– Ron
Nov 15 '18 at 16:09













void main?? -

– Quentin
Nov 15 '18 at 16:11





void main?? -

– Quentin
Nov 15 '18 at 16:11













Solution: replace class B : public A with class B.

– Evg
Nov 15 '18 at 16:12





Solution: replace class B : public A with class B.

– Evg
Nov 15 '18 at 16:12













@Evg, B needs to inherits form A in the case.

– ldlchina
Nov 15 '18 at 16:14





@Evg, B needs to inherits form A in the case.

– ldlchina
Nov 15 '18 at 16:14




1




1





See edited answer.

– Evg
Nov 15 '18 at 16:39





See edited answer.

– Evg
Nov 15 '18 at 16:39












4 Answers
4






active

oldest

votes


















4














Alternatively, make A::classType() private in B:



class B : public A 
private:
using A::classType;



Edit. After the question has been clarified, I suggest that you use type traits technique instead of static member functions.



template<class>
struct Traits;

template<>
struct Traits<A>
static const char* classType()
return "A";

;

template<class T>
const char* TypeOf()
return Traits<T>::classType();



Now the user has to define his own type trait for B, otherwise



const char* type = TypeOf<B>(); 


will not compile.






share|improve this answer

























  • Explicitly deleting the function in class B seems more expressive to me. Both approaches work with classes inheriting from B.

    – Paul Sanders
    Nov 15 '18 at 16:19












  • @PaulSanders, agree. I upvoted your answer. But there is an alternative solution, nevertheless.

    – Evg
    Nov 15 '18 at 16:20


















3














Apart from a couple of typos in your code ... add the following to the definition of class B



static const char* classType() = delete;


Live Demo






share|improve this answer























  • It's not my purpose. My purpose is that when the author forgets to write the classType() function in the sub classes of class A, the author should get a reminder.

    – ldlchina
    Nov 15 '18 at 16:30


















1














I got the idea from @Evg, thanks Evg.
Use template like below:



class A

public:
template <class T>
static const char* classType() static_assert(false, "classType not implemented");
template <>
static const char* classType<A>()

static const char* s_classType = "A";
return s_classType;

;

template <class T>
const char* TypeOf()

return T::classType<T>();






share|improve this answer
































    0














    Declare the function as pure virtual.



    class A

    public:
    virtual static const char* classType() =0;
    ;


    Then all instantiated classes inheriting from A have to implement this.






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323485%2fhow-to-avoid-calling-function-in-base-class%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      Alternatively, make A::classType() private in B:



      class B : public A 
      private:
      using A::classType;



      Edit. After the question has been clarified, I suggest that you use type traits technique instead of static member functions.



      template<class>
      struct Traits;

      template<>
      struct Traits<A>
      static const char* classType()
      return "A";

      ;

      template<class T>
      const char* TypeOf()
      return Traits<T>::classType();



      Now the user has to define his own type trait for B, otherwise



      const char* type = TypeOf<B>(); 


      will not compile.






      share|improve this answer

























      • Explicitly deleting the function in class B seems more expressive to me. Both approaches work with classes inheriting from B.

        – Paul Sanders
        Nov 15 '18 at 16:19












      • @PaulSanders, agree. I upvoted your answer. But there is an alternative solution, nevertheless.

        – Evg
        Nov 15 '18 at 16:20















      4














      Alternatively, make A::classType() private in B:



      class B : public A 
      private:
      using A::classType;



      Edit. After the question has been clarified, I suggest that you use type traits technique instead of static member functions.



      template<class>
      struct Traits;

      template<>
      struct Traits<A>
      static const char* classType()
      return "A";

      ;

      template<class T>
      const char* TypeOf()
      return Traits<T>::classType();



      Now the user has to define his own type trait for B, otherwise



      const char* type = TypeOf<B>(); 


      will not compile.






      share|improve this answer

























      • Explicitly deleting the function in class B seems more expressive to me. Both approaches work with classes inheriting from B.

        – Paul Sanders
        Nov 15 '18 at 16:19












      • @PaulSanders, agree. I upvoted your answer. But there is an alternative solution, nevertheless.

        – Evg
        Nov 15 '18 at 16:20













      4












      4








      4







      Alternatively, make A::classType() private in B:



      class B : public A 
      private:
      using A::classType;



      Edit. After the question has been clarified, I suggest that you use type traits technique instead of static member functions.



      template<class>
      struct Traits;

      template<>
      struct Traits<A>
      static const char* classType()
      return "A";

      ;

      template<class T>
      const char* TypeOf()
      return Traits<T>::classType();



      Now the user has to define his own type trait for B, otherwise



      const char* type = TypeOf<B>(); 


      will not compile.






      share|improve this answer















      Alternatively, make A::classType() private in B:



      class B : public A 
      private:
      using A::classType;



      Edit. After the question has been clarified, I suggest that you use type traits technique instead of static member functions.



      template<class>
      struct Traits;

      template<>
      struct Traits<A>
      static const char* classType()
      return "A";

      ;

      template<class T>
      const char* TypeOf()
      return Traits<T>::classType();



      Now the user has to define his own type trait for B, otherwise



      const char* type = TypeOf<B>(); 


      will not compile.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 15 '18 at 16:39

























      answered Nov 15 '18 at 16:15









      EvgEvg

      4,01221436




      4,01221436












      • Explicitly deleting the function in class B seems more expressive to me. Both approaches work with classes inheriting from B.

        – Paul Sanders
        Nov 15 '18 at 16:19












      • @PaulSanders, agree. I upvoted your answer. But there is an alternative solution, nevertheless.

        – Evg
        Nov 15 '18 at 16:20

















      • Explicitly deleting the function in class B seems more expressive to me. Both approaches work with classes inheriting from B.

        – Paul Sanders
        Nov 15 '18 at 16:19












      • @PaulSanders, agree. I upvoted your answer. But there is an alternative solution, nevertheless.

        – Evg
        Nov 15 '18 at 16:20
















      Explicitly deleting the function in class B seems more expressive to me. Both approaches work with classes inheriting from B.

      – Paul Sanders
      Nov 15 '18 at 16:19






      Explicitly deleting the function in class B seems more expressive to me. Both approaches work with classes inheriting from B.

      – Paul Sanders
      Nov 15 '18 at 16:19














      @PaulSanders, agree. I upvoted your answer. But there is an alternative solution, nevertheless.

      – Evg
      Nov 15 '18 at 16:20





      @PaulSanders, agree. I upvoted your answer. But there is an alternative solution, nevertheless.

      – Evg
      Nov 15 '18 at 16:20













      3














      Apart from a couple of typos in your code ... add the following to the definition of class B



      static const char* classType() = delete;


      Live Demo






      share|improve this answer























      • It's not my purpose. My purpose is that when the author forgets to write the classType() function in the sub classes of class A, the author should get a reminder.

        – ldlchina
        Nov 15 '18 at 16:30















      3














      Apart from a couple of typos in your code ... add the following to the definition of class B



      static const char* classType() = delete;


      Live Demo






      share|improve this answer























      • It's not my purpose. My purpose is that when the author forgets to write the classType() function in the sub classes of class A, the author should get a reminder.

        – ldlchina
        Nov 15 '18 at 16:30













      3












      3








      3







      Apart from a couple of typos in your code ... add the following to the definition of class B



      static const char* classType() = delete;


      Live Demo






      share|improve this answer













      Apart from a couple of typos in your code ... add the following to the definition of class B



      static const char* classType() = delete;


      Live Demo







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 15 '18 at 16:13









      Paul SandersPaul Sanders

      5,2762621




      5,2762621












      • It's not my purpose. My purpose is that when the author forgets to write the classType() function in the sub classes of class A, the author should get a reminder.

        – ldlchina
        Nov 15 '18 at 16:30

















      • It's not my purpose. My purpose is that when the author forgets to write the classType() function in the sub classes of class A, the author should get a reminder.

        – ldlchina
        Nov 15 '18 at 16:30
















      It's not my purpose. My purpose is that when the author forgets to write the classType() function in the sub classes of class A, the author should get a reminder.

      – ldlchina
      Nov 15 '18 at 16:30





      It's not my purpose. My purpose is that when the author forgets to write the classType() function in the sub classes of class A, the author should get a reminder.

      – ldlchina
      Nov 15 '18 at 16:30











      1














      I got the idea from @Evg, thanks Evg.
      Use template like below:



      class A

      public:
      template <class T>
      static const char* classType() static_assert(false, "classType not implemented");
      template <>
      static const char* classType<A>()

      static const char* s_classType = "A";
      return s_classType;

      ;

      template <class T>
      const char* TypeOf()

      return T::classType<T>();






      share|improve this answer





























        1














        I got the idea from @Evg, thanks Evg.
        Use template like below:



        class A

        public:
        template <class T>
        static const char* classType() static_assert(false, "classType not implemented");
        template <>
        static const char* classType<A>()

        static const char* s_classType = "A";
        return s_classType;

        ;

        template <class T>
        const char* TypeOf()

        return T::classType<T>();






        share|improve this answer



























          1












          1








          1







          I got the idea from @Evg, thanks Evg.
          Use template like below:



          class A

          public:
          template <class T>
          static const char* classType() static_assert(false, "classType not implemented");
          template <>
          static const char* classType<A>()

          static const char* s_classType = "A";
          return s_classType;

          ;

          template <class T>
          const char* TypeOf()

          return T::classType<T>();






          share|improve this answer















          I got the idea from @Evg, thanks Evg.
          Use template like below:



          class A

          public:
          template <class T>
          static const char* classType() static_assert(false, "classType not implemented");
          template <>
          static const char* classType<A>()

          static const char* s_classType = "A";
          return s_classType;

          ;

          template <class T>
          const char* TypeOf()

          return T::classType<T>();







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 16 '18 at 5:31

























          answered Nov 15 '18 at 17:34









          ldlchinaldlchina

          4161823




          4161823





















              0














              Declare the function as pure virtual.



              class A

              public:
              virtual static const char* classType() =0;
              ;


              Then all instantiated classes inheriting from A have to implement this.






              share|improve this answer



























                0














                Declare the function as pure virtual.



                class A

                public:
                virtual static const char* classType() =0;
                ;


                Then all instantiated classes inheriting from A have to implement this.






                share|improve this answer

























                  0












                  0








                  0







                  Declare the function as pure virtual.



                  class A

                  public:
                  virtual static const char* classType() =0;
                  ;


                  Then all instantiated classes inheriting from A have to implement this.






                  share|improve this answer













                  Declare the function as pure virtual.



                  class A

                  public:
                  virtual static const char* classType() =0;
                  ;


                  Then all instantiated classes inheriting from A have to implement this.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 16:56









                  Robert AndrzejukRobert Andrzejuk

                  2,99621425




                  2,99621425



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323485%2fhow-to-avoid-calling-function-in-base-class%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      ReactJS Fetched API data displays live - need Data displayed static

                      Evgeni Malkin