By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,889 Members | 1,297 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,889 IT Pros & Developers. It's quick & easy.

c++ Final class ?

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a

"Bezalel Bareli" <ba************@optonline.net> wrote in message
news:9f*************************@posting.google.co m...
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_f...#no-derivation

john
Jul 22 '05 #2

P: n/a
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.
Jul 22 '05 #3

P: n/a

"Bezalel Bareli" <ba************@optonline.net> wrote in message
news:9f*************************@posting.google.co m...
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
Jul 22 '05 #4

P: n/a
"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*?
Jul 22 '05 #5

P: n/a

"Marc" <Ma***********@Loria.Fr> wrote in message
news:c8***********@nef.ens.fr...
"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
Jul 22 '05 #6

P: n/a
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;};
};
Jul 22 '05 #7

P: n/a
ba************@optonline.net (Bezalel Bareli) wrote in message news:<9f*************************@posting.google.c om>...
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
Jul 22 '05 #8

P: n/a
>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.


Jul 22 '05 #9

P: n/a

"Michael D. Borghardt" <mi*****@borghardtConsulting.net> 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
Jul 22 '05 #10

P: n/a
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;
}
Jul 22 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.