473,623 Members | 3,345 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 1394
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_EVALUATI ON_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.d e> wrote in message
news:uG******** ******@TK2MSFTN GP09.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.d e> wrote in message
news:uG******** ******@TK2MSFTN GP09.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.d e> wrote in message
news:uh******** ******@TK2MSFTN GP11.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.d e> wrote in message
news:uh******** ******@TK2MSFTN GP11.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
63662
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 vars, along with their names. I could easily do this: print "myPlace = %s, myTime = %s" % (myPlace, myTime)
2
2730
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
2621
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
11356
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
1676
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
5991
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 been reading http://www.w3.org/TR/xmlschema-1/#src-resolve, but I don't get what is wrong. Can anyone give me a hint? Thanks in advance.
14
1569
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 function if (!this->Foo(something)) return(this->Base::Foo(something)); // for
6
2142
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 functionality being kept separate. I've been going over the various approaches I could follow in order to implement the OS-specific functionality. The requirements I have are as follows :
0
8221
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8662
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8603
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8463
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6104
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4067
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2593
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1468
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.