How to prevent from inhertance
-
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....
-
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....
Switch to java can't do it in C++
-prakash
-
Switch to java can't do it in C++
-prakash
can we check in base class constructor with this pointer which class is inveoked with this we can prevent from inheritance sai
-
can we check in base class constructor with this pointer which class is inveoked with this we can prevent from inheritance sai
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
-
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....
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
-
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....
http://groups-beta.google.com/group/comp.lang.c++.moderated/msg/214aaf1fe6df3b51[^] Read the whole thread! Best wishes, R.