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. A virtual function question

A virtual function question

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelptutorial
8 Posts 6 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.
  • B Offline
    B Offline
    bcemick
    wrote on last edited by
    #1

    I've looked around trying to find this information, but there's nothing I can find that fits my situation. I have two separate classes: Print and PropertySheet that are part of a larger program. I didn't write the code for either one and making any radical changes is out. All I need to do is add a function to the PropertySheet class that will access the Print class and print out certain information obtained from PropertySheet. Here's the problem: All of the print functions in Print (such as starting a new document, starting a new page, etc) are virtual functions and I can't access them from the PropertySheet class. I've never cared for vf's and haven't worked with them or used them before, so I'm lost on how to do this. Here's the question: How can I access those virtual functions in Print from PropertySheet without re-writing the program (I just want to add the code to print in the PropertySheet class)? I apologize if this has been addressed before, but, as I said, I couldn't find anything for this situation. I appreciate any help and say thank you in advance anyone that responds.

    C 1 Reply Last reply
    0
    • B bcemick

      I've looked around trying to find this information, but there's nothing I can find that fits my situation. I have two separate classes: Print and PropertySheet that are part of a larger program. I didn't write the code for either one and making any radical changes is out. All I need to do is add a function to the PropertySheet class that will access the Print class and print out certain information obtained from PropertySheet. Here's the problem: All of the print functions in Print (such as starting a new document, starting a new page, etc) are virtual functions and I can't access them from the PropertySheet class. I've never cared for vf's and haven't worked with them or used them before, so I'm lost on how to do this. Here's the question: How can I access those virtual functions in Print from PropertySheet without re-writing the program (I just want to add the code to print in the PropertySheet class)? I apologize if this has been addressed before, but, as I said, I couldn't find anything for this situation. I appreciate any help and say thank you in advance anyone that responds.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      If you can't access them, that probably means they are protected or private, not virtual, necessarily. If they are protected, derive a class from Print to gain access. Make sure this is the right thing to do, they are probably protected for a reason. bcemick wrote: I've never cared for vf's Why - do you hate object orientation ? Is it possible to put code that defines the interaction between these two objects into a third class, so that they don't become tightly coupled ? Christian Graus - Microsoft MVP - C++

      B 1 Reply Last reply
      0
      • C Christian Graus

        If you can't access them, that probably means they are protected or private, not virtual, necessarily. If they are protected, derive a class from Print to gain access. Make sure this is the right thing to do, they are probably protected for a reason. bcemick wrote: I've never cared for vf's Why - do you hate object orientation ? Is it possible to put code that defines the interaction between these two objects into a third class, so that they don't become tightly coupled ? Christian Graus - Microsoft MVP - C++

        B Offline
        B Offline
        bcemick
        wrote on last edited by
        #3

        The functions I need to access are public and declared as virtual. That's the first thing I checked. public: UINT virtual NewDocument(); It's not that I hate object orientation; I've just never understood the point of virtual functions. I don't understand why you would want to use them. Christian Graus wrote: Is it possible to put code that defines the interaction between these two objects into a third class, so that they don't become tightly coupled ? Not at this time, although that is a possibility later on. For right now, though, I have to get it working this way.

        S L 2 Replies Last reply
        0
        • B bcemick

          The functions I need to access are public and declared as virtual. That's the first thing I checked. public: UINT virtual NewDocument(); It's not that I hate object orientation; I've just never understood the point of virtual functions. I don't understand why you would want to use them. Christian Graus wrote: Is it possible to put code that defines the interaction between these two objects into a third class, so that they don't become tightly coupled ? Not at this time, although that is a possibility later on. For right now, though, I have to get it working this way.

          S Offline
          S Offline
          Stlan
          wrote on last edited by
          #4

          What do you mean by: I can't access them ? Compilation error/warning ? Failing assertion ? Crash ?

          B 1 Reply Last reply
          0
          • S Stlan

            What do you mean by: I can't access them ? Compilation error/warning ? Failing assertion ? Crash ?

            B Offline
            B Offline
            bcemick
            wrote on last edited by
            #5

            My apologies; I should have been clearer on that point. When I click on the Print button on the property sheet, it gives me an access violation (Error code: 5) when I'm in debug mode.

            L 1 Reply Last reply
            0
            • B bcemick

              My apologies; I should have been clearer on that point. When I click on the Print button on the property sheet, it gives me an access violation (Error code: 5) when I'm in debug mode.

              L Offline
              L Offline
              liquid_
              wrote on last edited by
              #6

              That's not a problem of virtual, private nor public function. Access violation means you violated the memory you accessed, i.e. address of memory wasn't valid.

              H 1 Reply Last reply
              0
              • L liquid_

                That's not a problem of virtual, private nor public function. Access violation means you violated the memory you accessed, i.e. address of memory wasn't valid.

                H Offline
                H Offline
                heman154
                wrote on last edited by
                #7

                Have you placed any breakpoints to determine exactly where the access violation occurs (When you try to call the function, or inside the function...may trying to manipulate another object that isn't there anymore). How does this Print Object get instantiated?

                1 Reply Last reply
                0
                • B bcemick

                  The functions I need to access are public and declared as virtual. That's the first thing I checked. public: UINT virtual NewDocument(); It's not that I hate object orientation; I've just never understood the point of virtual functions. I don't understand why you would want to use them. Christian Graus wrote: Is it possible to put code that defines the interaction between these two objects into a third class, so that they don't become tightly coupled ? Not at this time, although that is a possibility later on. For right now, though, I have to get it working this way.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  bcemick wrote: Not at this time, although that is a possibility later on. For right now, though, I have to get it working this way. It's NEVER a good idea to 'do it this way for now and change to a good design later'. I suggest you stop coding and read a book called 'Code Complete'. Seriously. If the function is not pure virtual ( has no body for you to call anyhow ), I don't see why you can't call it.

                  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