c++ Final class ?

Discussion in 'C++' started by Bezalel Bareli, May 19, 2004.

  1. I know I have seen some threads on the subject long time ago and it
    was using a virtual base class ...

    in short, what is the nicest way to implement the Java final class in
    c++

    Thanks.
     
    Bezalel Bareli, May 19, 2004
    #1
    1. Advertising

  2. "Bezalel Bareli" <> wrote in message
    news:...
    > I know I have seen some threads on the subject long time ago and it
    > was using a virtual base class ...
    >
    > in short, what is the nicest way to implement the Java final class in
    > c++
    >
    > Thanks.


    http://www.research.att.com/~bs/bs_faq2.html#no-derivation

    john
     
    John Harrison, May 19, 2004
    #2
    1. Advertising

  3. Thanks John; I was actually thinking on that path, however, I am
    trying to templatize the idea. Do you think it is template legal; I
    tried the exercise and failed compiling but I will read my template
    book again.
     
    Bezalel Bareli, May 20, 2004
    #3
  4. "Bezalel Bareli" <> wrote in message
    news:...
    > Thanks John; I was actually thinking on that path, however, I am
    > trying to templatize the idea. Do you think it is template legal; I
    > tried the exercise and failed compiling but I will read my template
    > book again.


    I couldn't get it to work either. I would say it cannot be templatized
    because of the problem of trying to grant friendship to the derived class,
    but I could be wrong.

    john
     
    John Harrison, May 20, 2004
    #4
  5. Bezalel Bareli

    Marc Guest

    "John Harrison" wrote:

    > I couldn't get it to work either. I would say it cannot be templatized
    > because of the problem of trying to grant friendship to the derived class,
    > but I could be wrong.


    I did not read the book, so I am probably missing something, but what
    need is there for that "friend" lineĀ ?
     
    Marc, May 20, 2004
    #5
  6. "Marc" <> wrote in message
    news:c8jcho$15aq$...
    > "John Harrison" wrote:
    >
    > > I couldn't get it to work either. I would say it cannot be templatized
    > > because of the problem of trying to grant friendship to the derived

    class,
    > > but I could be wrong.

    >
    > I did not read the book, so I am probably missing something, but what
    > need is there for that "friend" line ?


    The point is to prevent derivation from Usable. Usable can construct
    Usable_lock because Usable_lock has granted it friendship. Any class derived
    from Usable will not be a friend of Usable_lock and so will not be able to
    construct Usable_lock and therefore, as Usable_lock is a virtual base, will
    not be able to be constructed at all.

    john
     
    John Harrison, May 21, 2004
    #6
  7. Hi. try this:

    template<typename T>
    class MakeFinal
    {
    private:
    MakeFinal() {std::cout << "MakeFinal Hello World" << std::endl;};
    ~MakeFinal() {std::cout << "MakeFinal Goodbye World" << std::endl;};
    friend T;
    };

    class Final : virtual public MakeFinal<Final>
    {
    public:
    Final() {std::cout << "Final Hello World" << std::endl;};
    ~Final() {std::cout << "Final Goodbye World" << std::endl;};
    };

    /* error Final not deriveable */
    class Derived : public Final
    {
    public:
    Derived() {std::cout << "Derived Hello World" << std::endl;};
    ~Derived() {std::cout << "Derived Goodbye World" << std::endl;};
    };
     
    Michael D. Borghardt, May 21, 2004
    #7
  8. Bezalel Bareli

    Luther Baker Guest

    (Bezalel Bareli) wrote in message news:<>...
    > I know I have seen some threads on the subject long time ago and it
    > was using a virtual base class ...
    >
    > in short, what is the nicest way to implement the Java final class in
    > c++


    Why not make the constructors private and write a public static factory method?

    -Luther
     
    Luther Baker, May 21, 2004
    #8
  9. Bezalel Bareli

    DaKoadMunky Guest

    >Why not make the constructors private and write a public static factory
    >method?


    Making constructors private for the purpose of preventing derivation has the
    possibly unwanted consequence of placing restrictions on how/where objects are
    instantiated.

    The virtual-base class based solution prevents derivation without imposing such
    restrictions allowing the "final" class to be used in a natural manner.
     
    DaKoadMunky, May 22, 2004
    #9
  10. "Michael D. Borghardt" <> wrote in message
    news:QUhrc.553255$Ig.357552@pd7tw2no...
    > Hi. try this:
    >
    > template<typename T>
    > class MakeFinal
    > {
    > private:
    > MakeFinal() {std::cout << "MakeFinal Hello World" << std::endl;};
    > ~MakeFinal() {std::cout << "MakeFinal Goodbye World" << std::endl;};
    > friend T;
    > };
    >
    > class Final : virtual public MakeFinal<Final>
    > {
    > public:
    > Final() {std::cout << "Final Hello World" << std::endl;};
    > ~Final() {std::cout << "Final Goodbye World" << std::endl;};
    > };


    I just did, after adding 'int main() { Final f; }' I get

    main.cpp(26) : error C2248: 'MakeFinal<T>::__ctor' : cannot access private
    member declared in class 'MakeFinal<T>'
    with
    [
    T=Final
    ]
    and
    [
    T=Final
    ]
    and
    [
    T=Final
    ]

    on MSVC++ 7.1. On gcc 3.3.1 I get

    error: template parameters cannot be friends

    On Comeau C++ I get

    line 26: error: "MakeFinal<T>::MakeFinal() [with T=Final]" is
    inaccessible
    Derived() {std::cout << "Derived Hello World" << std::endl;};

    What did you get on your compiler?

    john
     
    John Harrison, May 22, 2004
    #10
  11. Hi.

    This compiles under MSVC 7.0 and the free MSVC 7.1

    Also compiler under gcc 2.96

    works on Comeau non-strict

    Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
    Copyright 1988-2003 Comeau Computing. All rights reserved.
    MODE:non-strict warnings C++

    compiler error strict

    Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
    Copyright 1988-2003 Comeau Computing. All rights reserved.
    MODE:strict errors C++

    "ComeauTest.c", line 8: error: omission of "class" is nonstandard
    friend T;
    ^

    1 error detected in the compilation of "ComeauTest.c".



    If I use friend typename T in strict mode

    Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
    Copyright 1988-2003 Comeau Computing. All rights reserved.
    MODE:strict errors C++

    "ComeauTest.c", line 8: error: a class or namespace qualified name is
    required
    friend typename T;
    ^

    "ComeauTest.c", line 13: error: "MakeFinal<T>::MakeFinal() [with T=Final]"
    is
    inaccessible
    Final() {std::cout << "Final Hello World" << std::endl;};
    ^

    "ComeauTest.c", line 13: error: "MakeFinal<T>::~MakeFinal() [with T=Final]"
    is
    inaccessible
    Final() {std::cout << "Final Hello World" << std::endl;};
    ^

    "ComeauTest.c", line 14: error: "MakeFinal<T>::~MakeFinal() [with T=Final]"
    is
    inaccessible
    ~Final() {std::cout << "Final Goodbye World" << std::endl;};
    ^

    4 errors detected in the compilation of "ComeauTest.c".
    Below is the code I used#include <iostream>
    template<typename T>
    class MakeFinal
    {
    private:
    MakeFinal() {std::cout << "MakeFinal Hello World" << std::endl;};
    ~MakeFinal() {std::cout << "MakeFinal Goodbye World" << std::endl;};
    friend T;
    };
    class Final : virtual public MakeFinal<Final>
    {
    public:
    Final() {std::cout << "Final Hello World" << std::endl;};
    ~Final() {std::cout << "Final Goodbye World" << std::endl;};
    };
    /*
    class Derived : public Final
    {
    public:
    Derived() {std::cout << "Derived Hello World" << std::endl;};
    ~Derived() {std::cout << "Derived Goodbye World" << std::endl;};
    };
    */
    int main()
    {
    Final f;
    // Derived *d = new Derived();
    // delete d;
    return 1;
    }
     
    Michael D. Borghardt, May 24, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bigbinc

    final class, global?/

    bigbinc, Aug 7, 2003, in forum: Java
    Replies:
    9
    Views:
    423
    Roedy Green
    Aug 13, 2003
  2. Yin99

    final class

    Yin99, Jan 5, 2005, in forum: Java
    Replies:
    6
    Views:
    556
    thufir
    Jan 6, 2005
  3. E11
    Replies:
    1
    Views:
    4,941
    Thomas Weidenfeller
    Oct 12, 2005
  4. JFCM
    Replies:
    4
    Views:
    5,779
  5. Replies:
    5
    Views:
    545
    Chris Uppal
    Nov 17, 2006
Loading...

Share This Page