Implementing Equivalent Final Java class in c++

Implementing Equivalent Final Java class in c++

Post by Harmeet Sin » Tue, 05 Nov 2002 10:43:54



Hi,

I wish to implement a class in c++ which is equivalent to final
classes in Java (Classes whose objects can be created but can't be
derived/ extended from).

Eagerly waiting for tips/ solution ....

Regards
-Harmeet Singh


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Implementing Equivalent Final Java class in c++

Post by Daniel T » Wed, 06 Nov 2002 01:19:54



>I wish to implement a class in c++ which is equivalent to final
>classes in Java (Classes whose objects can be created but can't be
>derived/ extended from).

>Eagerly waiting for tips/ solution ....

You can't do that in C++. There are some ways you can make it difficult
to do, but no way to make it impossible.


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Implementing Equivalent Final Java class in c++

Post by Andy Sawye » Wed, 06 Nov 2002 01:21:03



 on 3 Nov 2002 20:43:54 -0500,

Quote:> Hi,

> I wish to implement a class in c++ which is equivalent to final
> classes in Java (Classes whose objects can be created but can't be
> derived/ extended from).

> Eagerly waiting for tips/ solution ....

This is well documented, and pretty straightforward:

class final_foo
{
 friend class foo;
 final_foo() {}

Quote:};

class foo : virtual private final_foo
{

Quote:};

It is now not possible to derive from "foo".

Regards,
 Andy S.
--
"Light thinks it travels faster than anything but it is wrong. No matter
 how fast light travels it finds the darkness has always got there
first,
 and is waiting for it."                  -- Terry Pratchett, Reaper Man


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Implementing Equivalent Final Java class in c++

Post by Carl Danie » Wed, 06 Nov 2002 06:12:37



Quote:> Hi,

> I wish to implement a class in c++ which is equivalent to final
> classes in Java (Classes whose objects can be created but can't be
> derived/ extended from).

One approach (the better one in most cases):

//
// MyClass - does ...
// Requires ...
//
// DO NOT derive from this class
//
class MyClass
{
    // ...

Quote:};

Another approach:

class MyClass
{
  private:
    MyClass();
    MyClass(...);    // whatever parameters are appropriate
  public
    MyClass* NewInstance(...) { return new MyClass(...); }

Quote:};

AFAIK, that's about as close as you can get - C++ has no concept like Java's
final (as applied to classes) or C#'s sealed.

-cd


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Implementing Equivalent Final Java class in c++

Post by Hyman Rose » Wed, 06 Nov 2002 06:14:16



> I wish to implement a class in c++ which is equivalent to final
> classes in Java (Classes whose objects can be created but can't be
> derived/ extended from).

Why would you want to do such a thing?

Quote:> Eagerly waiting for tips/ solution ....

template <class T> class make_final;
class foo;
template <> class make_final<foo> { friend class foo; ~make_final() { } };
class foo : make_final<foo>
{
     // ....

Quote:};


      [ about comp.lang.c++.moderated. First time posters: do this! ]
 
 
 

Implementing Equivalent Final Java class in c++

Post by Joshua Lehr » Wed, 06 Nov 2002 06:18:29


There is a trick to do this, it involves virtual inheritence from a
base class that only you have been given access to the constructor.
The idea is that the most derived class must initialize all virtual
base classes, so if anyone inherits from you, they will not be able to
call the constructor of the base.

However, I ask why you want to do this?  No C++ compiler that I am
aware of will determine that this class is final, and optimize virtual
function calls.  You can signal to the user that you intend for this
class to be final by using other techniques, like making the
destructor private and providing a destroy method.  Finally, if the
class has no virtual methods, by making the destructor public and
non-virtual, you have signalled to the consumer that this class is not
intended to be used as a base class.

I'm sure others will post the exact code to implement the virtual base
class technique that I described.

joshua lehrer
factset research systems


> Hi,

> I wish to implement a class in c++ which is equivalent to final
> classes in Java (Classes whose objects can be created but can't be
> derived/ extended from).

> Eagerly waiting for tips/ solution ....

> Regards
> -Harmeet Singh


      [ about comp.lang.c++.moderated. First time posters: do this! ]
 
 
 

Implementing Equivalent Final Java class in c++

Post by Andy Sawye » Thu, 07 Nov 2002 01:21:10



 on 4 Nov 2002 16:14:16 -0500,


> > I wish to implement a class in c++ which is equivalent to final
> > classes in Java (Classes whose objects can be created but can't be
> > derived/ extended from).

> Why would you want to do such a thing?

> > Eagerly waiting for tips/ solution ....

> template <class T> class make_final;
> class foo;
> template <> class make_final<foo> { friend class foo; ~make_final() { } };
> class foo : make_final<foo>
> {
>      // ....
> };

This doesn't do what was asked for.

class bar : foo {};

Is fine (bar has access to foo's destructor. It doesn't need access to
foo's base class destructors).

Regards,
 Andy S.
--
"Light thinks it travels faster than anything but it is wrong. No matter
 how fast light travels it finds the darkness has always got there first,
 and is waiting for it."                  -- Terry Pratchett, Reaper Man


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Implementing Equivalent Final Java class in c++

Post by Shannon Barb » Thu, 07 Nov 2002 04:15:52




> >I wish to implement a class in c++ which is equivalent to final
> >classes in Java (Classes whose objects can be created but can't be
> >derived/ extended from).

> >Eagerly waiting for tips/ solution ....

> You can't do that in C++. There are some ways you can make it difficult
> to do, but no way to make it impossible.

By the standard you can make it impossible, though not all vendors
implement the behavior correctly.  Andy posted the code that does so.


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Implementing Equivalent Final Java class in c++

Post by Hyman Rose » Thu, 07 Nov 2002 06:48:26



> This doesn't do what was asked for.

Yes, sorry. Rather,

template <class T> class make_final;
class foo;
template <> class make_final<foo> { make_final() { } friend class foo; };
class foo : virtual make_final<foo>
{
     //...

Quote:};


      [ about comp.lang.c++.moderated. First time posters: do this! ]
 
 
 

Implementing Equivalent Final Java class in c++

Post by James Kan » Thu, 07 Nov 2002 06:51:45




> > I wish to implement a class in c++ which is equivalent to final
> > classes in Java (Classes whose objects can be created but can't be
> > derived/ extended from).
> Why would you want to do such a thing?

Maybe because the class wasn't designed for inheritance, and you want to
prevent it.  Although logically, one would like the reverse: by default,
it shouldn't be possible to inherit from a class, and the programmer
should be required to specify that it is OK in this specific case.

--

Conseils en informatique oriente objet/
                    Beratung in objektorientierter Datenverarbeitung


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Implementing Equivalent Final Java class in c++

Post by Gennaro Prot » Thu, 07 Nov 2002 06:53:24



Quote:>template <class T> class make_final;
>class foo;
>template <> class make_final<foo> { friend class foo; ~make_final() { } };
>class foo : make_final<foo>
>{
>     // ....
>};

Apart from the two obvious errors in that code, it's important to note
that there's no known way to prevent derivation (or access to static
members of the derived class). That solution just prohibits creating
instances of any foo derivate.

Genny.


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Implementing Equivalent Final Java class in c++

Post by Francis Glassboro » Thu, 07 Nov 2002 06:55:33




Quote:>Finally, if the
>class has no virtual methods, by making the destructor public and
>non-virtual, you have signalled to the consumer that this class is not
>intended to be used as a base class.

But that only applies to classes that are not part of a polymorphic
hierarchy.

--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Implementing Equivalent Final Java class in c++

Post by Francis Glassboro » Thu, 07 Nov 2002 06:56:13




Quote:>Another approach:

>class MyClass
>{
>  private:
>    MyClass();
>    MyClass(...);    // whatever parameters are appropriate
>  public
>    MyClass* NewInstance(...) { return new MyClass(...); }

That needs to be a static member function

Quote:>};

>AFAIK, that's about as close as you can get - C++ has no concept like Java's
>final (as applied to classes) or C#'s sealed.

But you better be careful to lock out self copying because I think this
compiles:

class monster: public MyClass {
public:
    monster(): MyClass(*MyClass::NewInstance(...)) {}
...

Quote:};

--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Implementing Equivalent Final Java class in c++

Post by Hyman Rose » Fri, 08 Nov 2002 04:40:39


 > That solution just prohibits creating instances of any foo derivate.

I think that's what people want when they say they want to prevent
derivation.


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Implementing Equivalent Final Java class in c++

Post by Rani Sharon » Fri, 08 Nov 2002 05:08:58




> > This doesn't do what was asked for.

> Yes, sorry. Rather,

> template <class T> class make_final;
> class foo;
> template <> class make_final<foo> { make_final() { } friend class foo; };
> class foo : virtual make_final<foo>
> {
>      //...
> };

Not yet:

class foo2 : foo, virtual make_final<foo>
{
public:
     foo2() : make_final<foo>(*this)
     {}

Quote:};

foo2 X;

I guess that making make_final destructor private will be enough:
template <> class make_final<foo>
{ make_final() { } ~make_final() { } friend class foo; };

Just a reminder for the final class generic idiom for more relaxed compilers
(like VC):

template <class T> class make_final {

   make_final() { } ~make_final() { }

   friend T; // illegal (11.4/2) but works with VC

Quote:};

Rani


      [ about comp.lang.c++.moderated. First time posters: do this! ]