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. Another impossible question

Another impossible question

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 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.
  • W Offline
    W Offline
    Waldermort
    wrote on last edited by
    #1

    Lets say I have a class with quite a few public members, each time one of these functions is called I need to check a variable (by calling another function) before I can allow the code to proceed. Obviously I could insert the function call into each public function, but is there an alternative? What I'm worried about is somebody subclassing and forgetting to add the call into their extended function.

    J J Z 3 Replies Last reply
    0
    • W Waldermort

      Lets say I have a class with quite a few public members, each time one of these functions is called I need to check a variable (by calling another function) before I can allow the code to proceed. Obviously I could insert the function call into each public function, but is there an alternative? What I'm worried about is somebody subclassing and forgetting to add the call into their extended function.

      J Offline
      J Offline
      JWood
      wrote on last edited by
      #2

      Private functions


      A cynic is a man who, when he smells flowers, looks around for a coffin.
      -H.L. Mencken

      W 1 Reply Last reply
      0
      • J JWood

        Private functions


        A cynic is a man who, when he smells flowers, looks around for a coffin.
        -H.L. Mencken

        W Offline
        W Offline
        Waldermort
        wrote on last edited by
        #3

        hey thats a great idea, I will declare all of the class functions as private, that will stop those hackers from subclassing my class. One small drawback though, it won't work.

        A 1 Reply Last reply
        0
        • W Waldermort

          Lets say I have a class with quite a few public members, each time one of these functions is called I need to check a variable (by calling another function) before I can allow the code to proceed. Obviously I could insert the function call into each public function, but is there an alternative? What I'm worried about is somebody subclassing and forgetting to add the call into their extended function.

          J Offline
          J Offline
          Jun Du
          wrote on last edited by
          #4

          This probably isn't what wanted either. Just in case, if you coded in managed C++, you could use "sealed" modifier to a public function which you don't want to be derived from.

          Best, Jun

          1 Reply Last reply
          0
          • W Waldermort

            Lets say I have a class with quite a few public members, each time one of these functions is called I need to check a variable (by calling another function) before I can allow the code to proceed. Obviously I could insert the function call into each public function, but is there an alternative? What I'm worried about is somebody subclassing and forgetting to add the call into their extended function.

            Z Offline
            Z Offline
            Zac Howland
            wrote on last edited by
            #5

            Are these public members variables or functions? If these are functions, you can use something like the Pimpl-Idiom to hide the implementation a bit, so that if they subclass it, they won't have access to the "meat" of the object. For example:

            // in the header file
            class _MyClass;
            class MyClass
            {
            public:
            	MyClass();
            	~MyClass();	// declare as virutal if you want this to be able to be derived from
            	void someFunc();
            	// whatever other functions you want
            private:
            	_MyClass* m_pImpl;
            };
            
            // in the implementation
            class _MyClass
            {
            public:
            	_MyClass()
            	{
            	}
            
            	void someFunc()
            	{
            		// do whatever
            	}
            };
            
            MyClass::MyClass() : m_pImpl(new _MyClass)
            {
            }
            
            MyClass::~MyClass()
            {
            	delete m_pImpl;
            }
            
            void MyClass::someFunc()
            {
            	m_pImpl->someFunc();	
            }
            

            That way, deriving from MyClass would allow them to call the MyClass public/protected methods, but have no access to the actual data and can't circumvent (at least not easily) any requirements you are trying to place on the class.

            If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

            W 1 Reply Last reply
            0
            • W Waldermort

              hey thats a great idea, I will declare all of the class functions as private, that will stop those hackers from subclassing my class. One small drawback though, it won't work.

              A Offline
              A Offline
              Anonymuos
              wrote on last edited by
              #6

              waldermort wrote:

              hey thats a great idea, I will declare all of the class functions as private, that will stop those hackers from subclassing my class. One small drawback though, it won't work.

              JWood's answer was too short. He probably meant something like:

              class Base {
              public:
              void doSomething() { // public non-virtual
              checkVar();
              doSomethingImp();
              }
              private:
              virtual void doSomethingImp() {
              // ...
              }
              };

              class Derived : public Base {
              public:

              private:
              virtual void doSomethingImp() {
              // overrides Base::doSomethingImp()
              }
              }

              -- modified at 17:43 Thursday 7th September, 2006 see also: http://www.gotw.ca/publications/mill18.htm[^]

              1 Reply Last reply
              0
              • Z Zac Howland

                Are these public members variables or functions? If these are functions, you can use something like the Pimpl-Idiom to hide the implementation a bit, so that if they subclass it, they won't have access to the "meat" of the object. For example:

                // in the header file
                class _MyClass;
                class MyClass
                {
                public:
                	MyClass();
                	~MyClass();	// declare as virutal if you want this to be able to be derived from
                	void someFunc();
                	// whatever other functions you want
                private:
                	_MyClass* m_pImpl;
                };
                
                // in the implementation
                class _MyClass
                {
                public:
                	_MyClass()
                	{
                	}
                
                	void someFunc()
                	{
                		// do whatever
                	}
                };
                
                MyClass::MyClass() : m_pImpl(new _MyClass)
                {
                }
                
                MyClass::~MyClass()
                {
                	delete m_pImpl;
                }
                
                void MyClass::someFunc()
                {
                	m_pImpl->someFunc();	
                }
                

                That way, deriving from MyClass would allow them to call the MyClass public/protected methods, but have no access to the actual data and can't circumvent (at least not easily) any requirements you are trying to place on the class.

                If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                W Offline
                W Offline
                Waldermort
                wrote on last edited by
                #7

                That's an excellent idea, I must remember this method. But in my current instance, I have simply added the function call to each function. If anybody does decide to override the methods, they are just going to have to read the docs.

                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