Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. operator ":" new for me in C++

operator ":" new for me in C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aesthetic crazy
    wrote on last edited by
    #1

    i want to know what is ":" operator in C++ called. i know how it is used in access specifiers but i saw something new for me. destructor.hpp class A { int i; public: A(); // A(int a);// ~A(); // }; destructor.cpp #include "destructor.hpp" #include <iostream> using namespace std; A::A() : i(0) // WHAT IS ": i(0)" ?, WHAT IS ":" MEAN HERE ? :confused: { cout << "default constructor of A" << endl; } A::A(int a) : i(a) { cout << "constructor of A(" << i << ")" << endl; } A::~A() { cout << "Destructor of A (i=" << i << ")" << endl; }

    L I A M 4 Replies Last reply
    0
    • A aesthetic crazy

      i want to know what is ":" operator in C++ called. i know how it is used in access specifiers but i saw something new for me. destructor.hpp class A { int i; public: A(); // A(int a);// ~A(); // }; destructor.cpp #include "destructor.hpp" #include <iostream> using namespace std; A::A() : i(0) // WHAT IS ": i(0)" ?, WHAT IS ":" MEAN HERE ? :confused: { cout << "default constructor of A" << endl; } A::A(int a) : i(a) { cout << "constructor of A(" << i << ")" << endl; } A::~A() { cout << "Destructor of A (i=" << i << ")" << endl; }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      This is just a shorthand way of initialising variables in the constructor, and is equivalent to coding:

      A::A()
      {
      i = 0;
      ...

      I must get a clever new signature for 2011.

      A 1 Reply Last reply
      0
      • A aesthetic crazy

        i want to know what is ":" operator in C++ called. i know how it is used in access specifiers but i saw something new for me. destructor.hpp class A { int i; public: A(); // A(int a);// ~A(); // }; destructor.cpp #include "destructor.hpp" #include <iostream> using namespace std; A::A() : i(0) // WHAT IS ": i(0)" ?, WHAT IS ":" MEAN HERE ? :confused: { cout << "default constructor of A" << endl; } A::A(int a) : i(a) { cout << "constructor of A(" << i << ")" << endl; } A::~A() { cout << "Destructor of A (i=" << i << ")" << endl; }

        I Offline
        I Offline
        Iain Clarke Warrior Programmer
        wrote on last edited by
        #3

        The : is called a colon by me - and many other people It does different jobs in different places. Ie:

        switch (iValue)
        {
        case Bob: // Here is a label

        goto finished;
        ....
        finished: // also a label
        return bIUseGotos;
        }

        bSuccess = (pFunction != NULL) ? pFunction->DoSomething () : false; // Here is something else - I think part of a ternary

        And in your case, I'll pick a more common example:

        class A
        {
        public:
        A (int i);
        };

        class B : public A
        {
        public:
        A ();
        }

        Now we make a constructor...

        B::B ()
        {
        }

        This will not compile - you will get a complaint about A not getting having a constructor with no parameters. So, we need:

        B::B ()
        : A (7) // we're calling the A's constructor with a parameter of 7
        {
        }

        The principle is exactly the same for member variables. In your example, i's constructor is being called with 0. In the case of basic variables, there's an implicit constructor for this. It's not a real one. As for what you'd call it, I have no idea. Inheritance operator? Iain.

        I am one of "those foreigners coming over here and stealing our jobs". Yay me!

        1 Reply Last reply
        0
        • L Lost User

          This is just a shorthand way of initialising variables in the constructor, and is equivalent to coding:

          A::A()
          {
          i = 0;
          ...

          I must get a clever new signature for 2011.

          A Offline
          A Offline
          aesthetic crazy
          wrote on last edited by
          #4

          ok, then what is going on here please? class A { public: A() { cout << "(1)"; } A(A &a) { cout << "(2)"; } }; class B { A a; public: B() : a() { cout << "(3)"; } :confused: }; … B b; B b2(b);

          L 1 Reply Last reply
          0
          • A aesthetic crazy

            ok, then what is going on here please? class A { public: A() { cout << "(1)"; } A(A &a) { cout << "(2)"; } }; class B { A a; public: B() : a() { cout << "(3)"; } :confused: }; … B b; B b2(b);

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            It's initialising the object a of class A. In each case it is merely calling the object's constructor, and for primitive types (such as int) that just means setting them to the value in parenthesis. Take a look at your C++ notes or manual for further discussion of instantiation, inheritance etc.

            I must get a clever new signature for 2011.

            1 Reply Last reply
            0
            • A aesthetic crazy

              i want to know what is ":" operator in C++ called. i know how it is used in access specifiers but i saw something new for me. destructor.hpp class A { int i; public: A(); // A(int a);// ~A(); // }; destructor.cpp #include "destructor.hpp" #include <iostream> using namespace std; A::A() : i(0) // WHAT IS ": i(0)" ?, WHAT IS ":" MEAN HERE ? :confused: { cout << "default constructor of A" << endl; } A::A(int a) : i(a) { cout << "constructor of A(" << i << ")" << endl; } A::~A() { cout << "Destructor of A (i=" << i << ")" << endl; }

              A Offline
              A Offline
              Aescleal
              wrote on last edited by
              #6

              In the case you're asking about the colon marks the start of an initialiser list. These describe how to initialise a class's base class or member variables. Each element of the initialiser list has the syntax:

              A( x, ... )

              where A is either the name of the base class or the name of the member variable to be initialised and x,... is the list of arguments to the constructor of the base class or member. I'd suggest grabbing a copy of "The C++ Programming Language" by Bjarne Stroustrup. He describes the initialisation of member objects and base classes in loads of detail in part 2 of the book. And explains why you'd prefer to write initialisers rather than making assignments in the body of a constructor. Cheers, Ash

              1 Reply Last reply
              0
              • A aesthetic crazy

                i want to know what is ":" operator in C++ called. i know how it is used in access specifiers but i saw something new for me. destructor.hpp class A { int i; public: A(); // A(int a);// ~A(); // }; destructor.cpp #include "destructor.hpp" #include <iostream> using namespace std; A::A() : i(0) // WHAT IS ": i(0)" ?, WHAT IS ":" MEAN HERE ? :confused: { cout << "default constructor of A" << endl; } A::A(int a) : i(a) { cout << "constructor of A(" << i << ")" << endl; } A::~A() { cout << "Destructor of A (i=" << i << ")" << endl; }

                M Offline
                M Offline
                moemass
                wrote on last edited by
                #7

                all i can recall is that the ":" is the resolution operator in c++ which basically mean that A() is a method in the A namespace google it or buy yourself a good c++ book

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups