c++ and 'single colon'

Y

yvinogradov

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;
}
 
R

Rolf Magnus

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

Jonathan Mcdougall

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
 
U

ulrich

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top