what is the use of single colon in c++.

i do not know what is the use of single colon in c++ and how to use it and where to use it. may one please tell me about it. I am placing a single line of code where a single colon is used . Please tell me what does it do.

 
 complexNumbers() : real(0), img(0)
Well since this is a class constructor what you have is the constructor with an initialization list (signified by the colon). The class member variables shown are initialized with the value of zero.

That is only a part of the definition of that constructor; the rest of body of the function ({}) is missing.

See http://en.cppreference.com/w/cpp/language/initializer_list
In your example, it is what the above questions have told you, however, it has 1 more use.
The single colon could also signify class inheritance, if the class declaration is followed by : and a class name, it means the class derives from the class behind the single colon.

1
2
3
4
class InheritedClass : public BaseClass
{
    // code
};


In my example, InheritedClass would be able to use methods and properties from BaseClass freely, and so would objects created from InheritedClass be able to do as well.

If you want want to learn more about inheritance and how constructors can pass arguments over to their base class's constructor, see link:
https://www.ibm.com/support/knowledgecenter/en/SSQ2R2_8.5.1/com.ibm.tpf.toolkit.compilers.doc/ref/langref_os390/cbclr21020.htm
Last edited on
As per keskiverto's link, in something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class ComplexNumbers
{
public:
    ComplexNumbers() :
        real_(0),
        imaginary_(0)
    {
        // Other stuff happens after direct initializer
        instance_counter++;
    }
    static int instance_counter;

private:
    double real_;
    double imaginary_;
    
};
int ComplexNumbers::instance_counter = 0;


The real_ and imaginary_ variables are initialized first, before the constructor body. Static variables can't be default-initialized in this way.

You asked about that particular colon context, but I can think of a few more places where the colon is used in C++:
1. As a label, e.g. after_loop: in the following. Since the keyword break breaks you out of your current loop, some people have utilized goto and labels to break out of a nested loop.
Now this is not very readable and not recommended -- if possible try to redesign the function so that you can return out of a nested loop, or for readability set a flag to true, break out, and in the outer loop also break out if the flag had changed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
bool Complicated(int& answer)
{
    bool found = false;
    int possible_answer;
    for (int i=1; i<=100000; ++i)
    {
        for (int j<=1; j<=100000; ++j
        {
            if ( possible_answer = WhatImLookingFor(i,j) != -1)
            {   
                found = true;
                goto after_loop;
            }
        }
    }

after_loop:
    // Additional processing within function
    if (found && ThisAnswerIsGood(possible_answer))
    {
        answer = possible_answer;
        return true;
    }
    answer = -1;
    return false;
}


2. Ternary statements -- Something ? truecase : falsecase. Looks especially nice when assignment to a variable is involved.

This snippet
1
2
3
4
5
6
7
8
9
string s;
if ( IsDone() )
{
    s = "done";
}
else
{
    s = "not done";
}

can be rewritten as string s = IsDone() ? "done" : "not done";

3. Switch statements. In some ways similar the colons in labels. I'm sure you've seen many colons in switch statements, which often have cases consisting of constants or enums:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
enum { RED, GREEN, BLUE };
void MyFunction(int i)
{
    switch (i)
    {
        case RED:
            myvar_ = "red";
            break;

        case GREEN:
            myvar_ = "green";
            break;

        case BLUE:
            myvar_ = "blue";
            break;

        default:
            myvar_ = "";
            break;
    }
}


4. Marking access in classes/structs (public/private/protected):
1
2
3
4
5
6
7
8
9
10
11
class MyClass
{
public:
    MyClass();

protected:
    int share_this_with_children;

private:
    int dont_touch_;
};

Topic archived. No new replies allowed.