c++ Final class ?

B

Bezalel Bareli

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.
 
B

Bezalel Bareli

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.
 
J

John Harrison

Bezalel Bareli said:
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
 
M

Marc

John Harrison" said:
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 ?
 
J

John Harrison

Marc said:
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
 
M

Michael D. Borghardt

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;};
};
 
L

Luther Baker

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
 
D

DaKoadMunky

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

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.
 
J

John Harrison

Michael D. Borghardt said:
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
 
M

Michael D. Borghardt

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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top