473,498 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Two virtuals of the same name

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

Nov 17 '05 #1
5 1390
Nick <no****@nowhere.out> wrote:
[...]
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
--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 17 '05 #2

"Hendrik Schober" <Sp******@gmx.de> wrote in message
news:uG**************@TK2MSFTNGP09.phx.gbl...
Nick <no****@nowhere.out> wrote:
[...]
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.
Nov 17 '05 #3
Nick <no****@nowhere.out> wrote:
"Hendrik Schober" <Sp******@gmx.de> wrote in message
news:uG**************@TK2MSFTNGP09.phx.gbl...
Nick <no****@nowhere.out> wrote:
[...]
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

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 17 '05 #4
"Hendrik Schober" <Sp******@gmx.de> wrote in message
news:uh**************@TK2MSFTNGP11.phx.gbl...
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
Nov 17 '05 #5
Nick <no****@nowhere.out> wrote:
"Hendrik Schober" <Sp******@gmx.de> wrote in message
news:uh**************@TK2MSFTNGP11.phx.gbl...
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.


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/)
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.


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

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
63607
by: stewart.midwinter | last post by:
No doubt I've overlooked something obvious, but here goes: Let's say I assign a value to a var, e.g.: myPlace = 'right here' myTime = 'right now' Now let's say I want to print out the two...
2
2722
by: Ravi | last post by:
My XML looks like: <abc> <def type="apple"> 1 </def> <def type="peach"> 2 </def> <def type="orange"> 3 </def> <def type="banana"> 4 </def> <def type="plum"> 5 </def> </abc>
1
2614
by: discomiller | last post by:
Mario Mueller: Hello *, radiobuttons belong to other radiobuttons by the "name="any_value"" attribut. Thats a fakt. I got the following XML:...
21
11335
by: TheKeith | last post by:
I heard that the name attribute is deprecated in html 4.01 strict. Is it recommended that you use the ID attribute for images along with the getElementById method instead of the old way? Thanks.
4
1663
by: Ole Nielsby | last post by:
I'm puzzed by this: /***code begin***/ class X {}; class Y : public X {}; class A { public: virtual void m(X x) {std::wcout << L"A\n";} }; class B : public A {
1
5980
by: ivanet | last post by:
Hello everyone, I am trying to use the following Schema but I get the error "src- resolve: Cannot resolve the name 'ValuesList' to a(n) 'element declaration' component." at line 144. I have...
14
1559
by: Jo | last post by:
Hi, Is there a generic way to access the (virtual) functions in the base class of a class? Like in: if (!this->Foo(something)) return(((Base*)this)->Foo(something)); // for a normal member...
6
2136
by: greek_bill | last post by:
Hi, I'm interested in developing an application that needs to run on more than one operating system. Naturally, a lot of the code will be shared between the various OSs, with OS specific...
0
7121
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6993
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7162
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7197
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6881
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4584
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.