operator ":" new for me in C++
-
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 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 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; }
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 labelgoto 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!
-
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.
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);
-
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);
It's initialising the object
a
of classA
. In each case it is merely calling the object's constructor, and for primitive types (such asint
) 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.
-
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; }
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
-
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; }