Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have seen many type of this statement ( :: ). I know it use for declaring a class member function out of the class( The left point must be a Class , and the right point must be a function , true? ) . But I have seen many different types of that . I don't know what are those means

e.i :

C++
std::vector<cv::Vec3f>::iterator itrCircles;

OR

QString::number((*itrCircles)[2], 'f' , 3).rightJustified(7,' '));


At the end I want know , when we don't define the function or class type ( void , int , etc for func and public , private , etc for class) what type will assign as default ?


Thank you in advance !
Posted
Updated 21-Mar-13 0:36am
v2

Both answers given so far have not fully explained the examples given in the question. Therefore, let me add a third answer here.

The :: is used in C++ as scope separator. In plain English that means, it separates the names of classes, structs, and namespaces. As you have mentioned in your question, you have already understood the role in separating class names and struct names from member names, for example:
C++
int MyClass::MyFunction()
{
    ...
}

The other common usage is with namespaces. Many libraries declare all their classes and functions inside a namespace to avoid conflicts with other libraries and user code. A well known example is the STL standard library, which encloses all its names in a namespace called "std". So, to use for example STL's swap function you would write:
C++
std::swap (a, b);

or to use the STL string class you would write
C++
std::string myString;

If there is no risk of name conflicts you can make your life a little easier and tell the compiler to also look into namespace "std" whenever it searches for a name by saying:
C++
using namespace std;

at the beginning of a source file. Then you can omit the std:: prefix in the abour examples.

Note that namespaces can be nested, so don't be surprised to see:
C++
BigLib::SubSection::SuperSmartClass myClass;

Your last question was unrelated to that subject: What is the default type the compiler assigns in the case of absence of a type definition?

Answer: In the C language that used to type "int" in the old days; but new code should not make use of that. In C++ there is no longer a default type. The compiler will issue an error message if you forget to specify the type -- for a good reason. Many programming errors occurred by simply forgetting about the return type of a function and assuming it was int or by the ambiguous use of int and bool.
 
Share this answer
 
Comments
hor_313 21-Mar-13 10:03am    
Really nice and perfect answer ! Thank you

Just one question : Why used (.) operation after number in this code

QString::number((*itrCircles)[2], 'f' , 3).rightJustified(7,' ');

number is a method or a class? If it is a method , then what is the (.) after it?
nv3 21-Mar-13 10:19am    
number is a static function of class QString, and it takes 3 arguments; hence the commas.
Scope resolution operator :: (C++ only)
The :: (scope resolution) operator is used to qualify hidden names so that you can still use them. You can use the unary scope operator if a namespace scope or global scope name is hidden by an explicit declaration of the same name in a block or class. For example:

C++
int count = 0;

int main(void) {
  int count = 0;
  ::count = 1;  // set global count to 1
  count = 2;    // set local count to 2
  return 0;
}

The declaration of count declared in the main function hides the integer named count declared in global namespace scope. The statement ::count = 1 accesses the variable named count declared in global namespace scope.

You can also use the class scope operator to qualify class names or class member names. If a class member name is hidden, you can use it by qualifying it with its class name and the class scope operator.

In the following example, the declaration of the variable X hides the class type X, but you can still use the static class member count by qualifying it with the class type X and the scope resolution operator.
C++
#include <iostream>
using namespace std;

class X
{
public:
      static int count;
};
int X::count = 10; // define static data member

int main ()
{
      int X = 0; // hides class type X
      cout << X::count << endl; // use static member of class X
}
</iostream>


Source: http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr175.htm[^]
 
Share this answer
 
v2
Comments
phil.o 21-Mar-13 7:01am    
5'd as I learned something and to counterbalance the 1-vote you got
Matthew Faithfull 21-Mar-13 7:08am    
Excellect answer. Ignore the random abuse of the phantom downvoter if you can. They will eventually be caught and justly humiliated.
hor_313 21-Mar-13 7:32am    
Is this true?


class A(
int sum(int,int);
);

class B() ;

B::A.sum(int a,int B){return a+b;}


if it is false , what is this:

QString::number((*itrCircles)[2], 'f' , 3).rightJustified(7,' '));
José Amílcar Casimiro 21-Mar-13 7:44am    
it's false.

QString::number((*itrCircles)[2], 'f' , 3).rightJustified(7,' '));

number is a static method inside QString class where the first parameter (*itrCircles)[2] is the third number of itrCircles array, the second parameter is the format and the last parameter is the precision.

The number static method returns a QString object.

See class definition: http://qt-project.org/doc/qt-4.8/qstring.html#static-public-members
hor_313 21-Mar-13 9:51am    
My question is about this part :

.rightJustified(7,' '))

You said number is a method but it used like a class (I know we can access to class methods with (.) operator)
Why it used (.) after number method?
Main user of Scope resolution operator ( :: ) in c++ is as below..

1. To access global variable
C++
int a=50;

int main()
{
    int a=10; // local variable 'a'
    ::a=100; // accessing global varable 'a'
    cout << ::a << endl; // print global variable
}


2. To access members of class
C++
Class MyClass
{
  int n1, n2;
  public:
  {
     void func1(); //Function Declaration
  }
};

public void MyClass::func1() //Use of Scope Resolution Operator to write function definition outside class definition
{
               // Function Code
}
 
Share this answer
 
Comments
hor_313 21-Mar-13 7:39am    
Is this true?


class A(
int sum(int,int);
);

class B() ;

B::A.sum(int a,int B){return a+b;}


if it is false , what is this:

QString::number((*itrCircles)[2], 'f' , 3).rightJustified(7,' '));

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900