473,698 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c++ and 'single colon'

I have a qustion about the attached piece of code. It compiles on gcc
version 3.4.1 (Cygwin special). The line in question has a single colon
instead of double. With double colon everything works as expected and
the base class implementation of foo() is called. With the single colon
the derived class implementation of foo() keeps calling itself
recursively. What does this single colon mean in this case?

Thanks.
-------------------------------------------------------------
#include <iostream>

class A {
public:
virtual void foo() {
std::cout << "A::foo()" << std::endl;
}
};

class B : public A {
public:
void foo() {
std::cout << "B::foo()" << std::endl;
A:foo(); // <----------------------------- Line in question
}
};

int main() {
B b;
b.foo();
return 0;
}

Jul 23 '05 #1
6 9660
yv*********@gma il.com wrote:
I have a qustion about the attached piece of code. It compiles on gcc
version 3.4.1 (Cygwin special). The line in question has a single colon
instead of double. With double colon everything works as expected and
the base class implementation of foo() is called. With the single colon
the derived class implementation of foo() keeps calling itself
recursively. What does this single colon mean in this case?


The single colon means that the name before it is the name of a label that
you can jump to with goto.
Jul 23 '05 #2
> I have a qustion about the attached piece of code.
It compiles on gcc version 3.4.1 (Cygwin special).
The line in question has a single colon instead of
double. With double colon everything works as
expected and the base class implementation of
foo() is called. With the single colon the derived
class implementation of foo() keeps calling itself
recursively. What does this single colon mean in
this case?
Nothing.
#include <iostream>

class A {
public:
virtual void foo() {
std::cout << "A::foo()" << std::endl;
}

};

class B : public A {
public:
void foo() {
std::cout << "B::foo()" << std::endl;
A:foo(); // <-------- Line in question
Replace 'A' by 'my_label' and put 'foo();' on another
line. Blink, laugh and hit your forehead on the desk.
}
};


Jonathan

Jul 23 '05 #3
On 11 May 2005 14:20:11 -0700, yv*********@gma il.com
<yv*********@gm ail.com> wrote:
I have a qustion about the attached piece of code. It compiles on gcc
version 3.4.1 (Cygwin special). The line in question has a single colon
instead of double. With double colon everything works as expected and
the base class implementation of foo() is called. With the single colon
the derived class implementation of foo() keeps calling itself
recursively. What does this single colon mean in this case?
doesn't the single colon make the A there to a possible jump target for a
goto-statement?
ulrich
Thanks.
-------------------------------------------------------------
#include <iostream>

class A {
public:
virtual void foo() {
std::cout << "A::foo()" << std::endl;
}
};

class B : public A {
public:
void foo() {
std::cout << "B::foo()" << std::endl;
A:foo(); // <----------------------------- Line in question
}
};

int main() {
B b;
b.foo();
return 0;
}


Jul 23 '05 #4
ben
Isn't it redundant because there's no goto in the program

"ulrich" <ua********@aon .at> wrote in message
news:opsqnh31uo n2mgp5@innsbruc k-neu...
On 11 May 2005 14:20:11 -0700, yv*********@gma il.com
<yv*********@gm ail.com> wrote:
I have a qustion about the attached piece of code. It compiles on gcc
version 3.4.1 (Cygwin special). The line in question has a single colon
instead of double. With double colon everything works as expected and
the base class implementation of foo() is called. With the single colon
the derived class implementation of foo() keeps calling itself
recursively. What does this single colon mean in this case?


doesn't the single colon make the A there to a possible jump target for a
goto-statement?
ulrich
Thanks.
-------------------------------------------------------------
#include <iostream>

class A {
public:
virtual void foo() {
std::cout << "A::foo()" << std::endl;
}
};

class B : public A {
public:
void foo() {
std::cout << "B::foo()" << std::endl;
A:foo(); // <----------------------------- Line in question
}
};

int main() {
B b;
b.foo();
return 0;
}

Jul 23 '05 #5
Thanks everyone - it was enlightening :)

Jul 23 '05 #6
ben wrote:
Isn't it redundant because there's no goto in the program


Yes, it is. And if you switch warnings on, g++ will detect it and warn about
it.

Jul 23 '05 #7

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

Similar topics

0
1563
by: Cindy Mueller | last post by:
--F0.5BD08B6_ABBB95C Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html> <body> <div align=3D"center"> <center> <table border=3D"0" cellspacing=3D"1" width=3D"100%"> <tr>
0
461
by: Maurice Mclain | last post by:
--7FC.__338__A.F0643A1 Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html> <body> <div align=3D"center"> <center> <table border=3D"0" cellspacing=3D"1" width=3D"100%"> <tr>
0
549
by: Lola Ervin | last post by:
--7F90A0.__A71 Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html> <body> <div align=3D"center"> <center> <table border=3D"0" cellspacing=3D"1" width=3D"100%"> <tr>
0
1592
by: Carl Frey | last post by:
--E.3EC5.46C.DE_FF_ Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html> <body> <div align=3D"center"> <center> <table border=3D"0" cellspacing=3D"1" width=3D"100%"> <tr>
0
1339
by: Cecile Hilton | last post by:
--E.3EC5.46C.DE_FF_ Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html> <body> <div align=3D"center"> <center> <table border=3D"0" cellspacing=3D"1" width=3D"100%"> <tr>
0
1282
by: Desiree Cho | last post by:
--__9_.C6_2.F04.5.E.4__B4_ Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html> <body> <div align=3D"center"> <center> <table border=3D"0" cellspacing=3D"1" width=3D"100%"> <tr>
0
1459
by: Scottie Wheeler | last post by:
--_.D97.0B.DC3_EB Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html> <body> <div align=3D"center"> <center> <table border=3D"0" cellspacing=3D"1" width=3D"100%"> <tr>
0
1480
by: Felix Tilley | last post by:
--.B29F53._ECD. Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html> <body> <div align=3D"center"> <center> <table border=3D"0" cellspacing=3D"1" width=3D"100%"> <tr>
0
1354
by: Terrie Parham | last post by:
--ED86E44..EE Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html> <body> <div align=3D"center"> <center> <table border=3D"0" cellspacing=3D"1" width=3D"100%"> <tr>
0
9021
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...
1
8892
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8860
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...
0
7712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
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
4361
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...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3038
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
3
1998
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.