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. How to prevent from inhertance

How to prevent from inhertance

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialc++javaoopquestion
6 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.
  • K Offline
    K Offline
    Kiran Kumar Singani
    wrote on last edited by
    #1

    hai ! i am facing a pbm in c++ . How can i prevent from inheritance of my class.. example: class a { } class b:public a { } i want to prevent a from b not inherit like in java final keyword....

    P J C 3 Replies Last reply
    0
    • K Kiran Kumar Singani

      hai ! i am facing a pbm in c++ . How can i prevent from inheritance of my class.. example: class a { } class b:public a { } i want to prevent a from b not inherit like in java final keyword....

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

      Switch to java can't do it in C++


      -prakash

      V 1 Reply Last reply
      0
      • P Prakash Nadar

        Switch to java can't do it in C++


        -prakash

        V Offline
        V Offline
        virtualkirankumar
        wrote on last edited by
        #3

        can we check in base class constructor with this pointer which class is inveoked with this we can prevent from inheritance sai

        P 1 Reply Last reply
        0
        • V virtualkirankumar

          can we check in base class constructor with this pointer which class is inveoked with this we can prevent from inheritance sai

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

          virtualkirankumar wrote: can we check in base class constructor with this pointer which class is inveoked with this we can prevent from inheritance sai or make all members private. but i dont understand the your intention. If the user cannot inherite ur class, then the user can certainly write a wrapper over ur class, which you cant prevent. My suggestion, be constructive on the class logic. if you are worried about ppl using ur class, just stick a copy right notice on the top.


          -prakash

          1 Reply Last reply
          0
          • K Kiran Kumar Singani

            hai ! i am facing a pbm in c++ . How can i prevent from inheritance of my class.. example: class a { } class b:public a { } i want to prevent a from b not inherit like in java final keyword....

            J Offline
            J Offline
            jan larsen
            wrote on last edited by
            #5

            As Prakash says, there is no keyword for this. But there is a method for getting the same kind of encapsulation. First you declare the interface for you class in a cpp file, then you declare an implementing class in a header file. Here's a small example: The header file dclares the interface Public, and a function that creates an instance:

            // The header file tester.h
            #ifndef _TESTER_H_
            #define _TESTER_H_

            // The interface definition:
            class Public
            {
            public:
            virtual int GetX() = 0;
            };

            // This function returns a pointer to an instance of an implementing class.
            Public * CreatePublic(int x);

            #endif // _TESTER_H_

            The cpp file declares the implementing class Private:

            // The cpp file tester.cpp
            #include "tester.h"

            // The implementing class.
            class Private: public Public
            {
            public:

            Private(int x): m\_x(x)
            {
            
            }
            

            int GetX()
            {
            return m_x;
            }

            private:
            int m_x;
            };

            Public * CreatePublic(int x)
            {
            return new Private(x);
            }

            Here's how to use it:

            #include "tester.h"

            int main()
            {
            // Can't do this because Private is declared in the header file.
            //Private priv = new Private(2);

            Public * pPub = CreatePublic(42);

            delete pPub;
            

            return 0;
            }

            The funny thing is, that while the Java keyword final optimizes the code by removing the need for a vtable, this C++ technique adds an overlay by creating a vtable. "After all i

            1 Reply Last reply
            0
            • K Kiran Kumar Singani

              hai ! i am facing a pbm in c++ . How can i prevent from inheritance of my class.. example: class a { } class b:public a { } i want to prevent a from b not inherit like in java final keyword....

              C Offline
              C Offline
              CP Visitor
              wrote on last edited by
              #6

              http://groups-beta.google.com/group/comp.lang.c++.moderated/msg/214aaf1fe6df3b51[^] Read the whole thread! Best wishes, R.

              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