Two virtuals of the same name

N

Nick

In the code below, class Z inherits from T<1> and T<2>. Both parents have
pure virtual members of the same name V(), which are overloaded in Z.
How do I define those overloaded members outside of Z? Currently, they are
defined inside the class declaration and this works fine. The problem is
that I cannot define them outside of Z as

void Z::T<1>::V()
{ cout<< "Z(1)" << endl; }

There must be some special syntax for this.

Thanks.


---------------------------------
#include <iostream>
using namespace std;

template< int N >
struct T {

T() { m_N = N; cout<< "T(" << m_N << ")" << endl;}
~T() { cout<< "~T(" << m_N << ")" << endl;}

void S() { V(); }
virtual void V() = 0;

int m_N;
};

struct Z: private T<1> , private T<2> {

Z() { }

void Do()
{
T<1>::S();
T<2>::S();
}

void T<1>::V() { cout<< "Z(1)" << endl; }
void T<2>::V() { cout<< "Z(2)" << endl; }
};

void main( int argc, char* argv[])
{
Z z;
z.Do();
}
 
H

Hendrik Schober

Nick said:
[...]
How do I define those overloaded members outside of Z? [...]


Which compiler are you using?

FWIW; Comeau reports this for your code:
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 26: error: qualified name is not allowed
void T<1>::V() { cout<< "Z(1)" << endl; }
^

"ComeauTest.c", line 27: error: qualified name is not allowed
void T<2>::V() { cout<< "Z(2)" << endl; }
^

"ComeauTest.c", line 30: error: return type of function "main" must be "int"
So use int main() OR int main(int argc, char *argv[])
void main( int argc, char* argv[])
^

"ComeauTest.c", line 32: error: object of abstract class type "Z" is not allowed:
pure virtual function "T<N>::V [with N=1]" has no overrider
pure virtual function "T<N>::V [with N=2]" has no overrider
Z z;
^

4 errors detected in the compilation of "ComeauTest.c".

Schobi
--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
 
H

Hendrik Schober

Nick said:
Hendrik Schober said:
Nick said:
[...]
How do I define those overloaded members outside of Z? [...]


Which compiler are you using?

As the newsgroup name suggests, I use MS Visual C++ .NET 2002 and 2003.


That does make a lot of differences
(especially regarding templates)!
Also, people frequently post their VC6
problems here.

Anyway, did you have a look at Comeau's
messages? I don't think implementing (or
overriding) two versions of the same
inherited virtual isn't possible. The
only solution I can think of right now
would be to introduce helper classes:

#include <iostream>
using namespace std;

template< int N >
struct T {
T() { cout<< "T(" << N << ")" << endl;}
~T() { cout<< "~T(" << N << ")" << endl;}
void S() { V(); }
virtual void V() = 0;
};

struct U1 : public T<1> {
void V() { cout<< "U1" << endl; }
};

struct U2 : public T<2> {
void V() { cout<< "U2" << endl; }
};

struct Z: private U1 , private U2 {
void Do() {U1::S();U2::S();}
};

int main()
{
Z z;
z.Do();
return 0;
}

'U' could also be a template.

HTH,

Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
 
N

Nick

Anyway, did you have a look at Comeau's
messages?

Comeau doesn't seem to support two overridden virtuals of the same name at
all. I can't see how Comeau's messages can help me.
The only solution I can think of right now
would be to introduce helper classes:

With helper classes, I would need to define two new classes and pass to them
a pointer to Z, since the overriden V() works mainly with Z's members. This
wouldn't be such a nice solution as the code in my original posting.

My code works fine. The only problem is that the overridden functions are
inlined. I am just looking for a way to define them outside of the Z class
declaration. Meanwhile, I had to declare two additional functions:

struct Z: private T<1> , private T<2> {
.......

void V1();
void V2();

void T<1>::V() { V1(); }
void T<2>::V() { V2(); }
};

Hendrik, thank you for your input anyway.

Regards,
Nick
 
H

Hendrik Schober

Nick said:
Comeau doesn't seem to support two overridden virtuals of the same name at
all. I can't see how Comeau's messages can help me.

Usually, if Comeau barks at something,
that means it is not std conforming
code. (This is true up to the extend
that most compiler vendors accept a
"Comeau (doesn't) compile this" as a
proof that their own compiler has a bug.)
Yes, I have heard of a bug in Comeau,
but this was in some of boost's code
that most people (including myself) do
not know which way to hold when they
attempt to read it.
For you and me, if Comeau chokes, it
indicates that the code is not correct.
(www.comeaucomputing.com/tryitout/)
With helper classes, I would need to define two new classes and pass to them
a pointer to Z, since the overriden V() works mainly with Z's members. This
wouldn't be such a nice solution as the code in my original posting.

That's true. Except that the code in
your OP isn't C++.
My code works fine.

What do you know? All you know is, that
the current version of your current
compiler errornously excepts this code
and, in the example you run into this,
seemed to do what you expect it to.
The only problem is that the overridden functions are
inlined. I am just looking for a way to define them outside of the Z class
declaration. [...]

Obviously, VC finds the error if it is
not inline. Good. This way you found the
problem in your code and you are aware
of it. This prevents further trouble for
you when the next version of VC won't
accept the code.


Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
 

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

Top