Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. technique for assure, that a function is overwritten in c++??

technique for assure, that a function is overwritten in c++??

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    schaereran gmx net
    wrote on last edited by
    #1

    hy, i have a virtual function in a class. i must go sure, that every class who is inherited from this class overwrites this function, otherwise a warning shoud occur at runtime, or something like that. is this possible?? thanx andreas

    P J 2 Replies Last reply
    0
    • S schaereran gmx net

      hy, i have a virtual function in a class. i must go sure, that every class who is inherited from this class overwrites this function, otherwise a warning shoud occur at runtime, or something like that. is this possible?? thanx andreas

      P Offline
      P Offline
      Prakash Nadar
      wrote on last edited by
      #2

      in the base class make the functino pure virtual like virtual void somefunction() = 0; //pure virtual function. this ensures that the derived class has to override/implement the method.

      S 1 Reply Last reply
      0
      • P Prakash Nadar

        in the base class make the functino pure virtual like virtual void somefunction() = 0; //pure virtual function. this ensures that the derived class has to override/implement the method.

        S Offline
        S Offline
        schaereran gmx net
        wrote on last edited by
        #3

        yes, but i need a technique, who workes also when i derive a class from a derived class and so on! is there any trick?

        P 1 Reply Last reply
        0
        • S schaereran gmx net

          yes, but i need a technique, who workes also when i derive a class from a derived class and so on! is there any trick?

          P Offline
          P Offline
          Prakash Nadar
          wrote on last edited by
          #4

          Please do read about pure virtual function.

          S 1 Reply Last reply
          0
          • P Prakash Nadar

            Please do read about pure virtual function.

            S Offline
            S Offline
            schaereran gmx net
            wrote on last edited by
            #5

            yes, but what, if i make a pure virtual function (abstract class) then derive a class from it, and implement this function. now i derive a class from this class, and forget to overwritte this function, how will i know?

            R P 2 Replies Last reply
            0
            • S schaereran gmx net

              yes, but what, if i make a pure virtual function (abstract class) then derive a class from it, and implement this function. now i derive a class from this class, and forget to overwritte this function, how will i know?

              R Offline
              R Offline
              Robert A T Kaldy
              wrote on last edited by
              #6

              It will cause a compile error. Robert-Antonio "CRAY is the only computer, which runs an endless loop in just 4 hours"

              P S 2 Replies Last reply
              0
              • R Robert A T Kaldy

                It will cause a compile error. Robert-Antonio "CRAY is the only computer, which runs an endless loop in just 4 hours"

                P Offline
                P Offline
                Prakash Nadar
                wrote on last edited by
                #7

                It doesnt for the second derivation.

                1 Reply Last reply
                0
                • S schaereran gmx net

                  yes, but what, if i make a pure virtual function (abstract class) then derive a class from it, and implement this function. now i derive a class from this class, and forget to overwritte this function, how will i know?

                  P Offline
                  P Offline
                  Prakash Nadar
                  wrote on last edited by
                  #8

                  If you derive the class from CObject in MFC then i guess you can use the RTTI to determine which object's method is called and then generate the error accordingly.

                  S 1 Reply Last reply
                  0
                  • R Robert A T Kaldy

                    It will cause a compile error. Robert-Antonio "CRAY is the only computer, which runs an endless loop in just 4 hours"

                    S Offline
                    S Offline
                    schaereran gmx net
                    wrote on last edited by
                    #9

                    there is no complie error(on the 2th. deriv)!!! is there any trick, or technique, that could give me a compile error?

                    1 Reply Last reply
                    0
                    • P Prakash Nadar

                      If you derive the class from CObject in MFC then i guess you can use the RTTI to determine which object's method is called and then generate the error accordingly.

                      S Offline
                      S Offline
                      schaereran gmx net
                      wrote on last edited by
                      #10

                      so it would be possible that, whenever someone is calling the function, who is not overwritten (and therefore the one of the superclass is called) it throws an error? that would be grat!

                      S 1 Reply Last reply
                      0
                      • S schaereran gmx net

                        so it would be possible that, whenever someone is calling the function, who is not overwritten (and therefore the one of the superclass is called) it throws an error? that would be grat!

                        S Offline
                        S Offline
                        S van Leent
                        wrote on last edited by
                        #11

                        Maybe some light. Any function has its start address. So you can check whether the start address of the function in the derived class is the same as the address of the function in the super class itself. If so, you could fault on runtime. It has some difficulties, and some knowledge of vtables seem to be handy here, but I think it is in the direction you should be thinking. LPCTSTR Dutch = TEXT("Double Dutch :-)");

                        1 Reply Last reply
                        0
                        • S schaereran gmx net

                          hy, i have a virtual function in a class. i must go sure, that every class who is inherited from this class overwrites this function, otherwise a warning shoud occur at runtime, or something like that. is this possible?? thanx andreas

                          J Offline
                          J Offline
                          Joe Woodbury
                          wrote on last edited by
                          #12

                          No. This is a very serious design flaw in C++. (It gets worse. Say you created a function of the same name in the inherited class, but very slightly changed one of the parameters. You're screwed.) Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups