The Artima Developer Community
Sponsored Link

Design Forum
How to create final class in C++?

3 replies on 1 page. Most recent reply: Jan 16, 2008 12:54 AM by asadullah Ansari

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Nguyen Minh Huy

Posts: 1
Nickname: nmhuy
Registered: Jun, 2005

How to create final class in C++? Posted: Jun 18, 2005 3:02 AM
Reply to this message Reply
Advertisement
Hi everybody,

In C++, we can avoid making an instance of a class by declare all constructors of that class private or protected. I wonder are there any way to create something like "Java final class" in C++ (i.e classes that cannot be inherited)?


Shashank D. Jha

Posts: 68
Nickname: shashankd
Registered: May, 2004

Re: How to create final class in C++? Posted: Jun 24, 2005 9:51 PM
Reply to this message Reply
responding to Nguyen

In C++ there is no keyword (final) to declare a class as non-inheritable as in Java. But then C++ has its own features which you may exploit to get the same behaviour.

Bsically it uses concepts of private constructor and friend class.

Idea is if you derive(virtual public) a class from another class having private constructors, you cannot create an object of derived class.

To avoid this you need to declare derived (Final class) class as friend of base class. So that now if some one tries to inherit from this Final class, compilation gives error as this class cannot call constructor of its super class i.e. Final class's super class i.e. base class that has private constructor.

Hope this helps.

regards,
Shashank

asadullah Ansari

Posts: 3
Nickname: asadullah
Registered: Jan, 2008

Re: How to create final class in C++? Posted: Jan 16, 2008 12:52 AM
Reply to this message Reply
example of creating object on heap
class final2
{
public:
static final2* Create() { return (new final2()) ; }
private:
~final2(){} ;

} ;
class child : public final2
{
public:
child(){ ;}
} ;
int main()
{
final2 *f ;
f = final::Create() ;
}

Final Class can be created on stack by this way….

class temp
{
private:
~temp() {; }
friend class Final;
};
class Final: virtual public temp
{
//Define as usual
}

Now no one can inherite Fianl class. But you can create the object of it in stack as well as heap.

asadullah Ansari

Posts: 3
Nickname: asadullah
Registered: Jan, 2008

Re: How to create final class in C++? Posted: Jan 16, 2008 12:54 AM
Reply to this message Reply
In place of priavte constructor, use private Destructor. Because Constructor can be overloaded and Destructor can'nt be overloaded.

Flat View: This topic has 3 replies on 1 page
Topic: How to identify classes in Object Oriented Design? Previous Topic   Next Topic Topic: What is AJAX novice looking for?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use