473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 9648
yv*********@gmail.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*********@gmail.com
<yv*********@gmail.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:opsqnh31uon2mgp5@innsbruck-neu...
On 11 May 2005 14:20:11 -0700, yv*********@gmail.com
<yv*********@gmail.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
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%">...
0
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"...
0
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
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%">...
0
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%">...
0
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"...
0
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%">...
0
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...

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.