Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ functions
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 :
 
 
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 21-Mar-13 1:35am
hor_313632
Edited 21-Mar-13 1:36am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:
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:
    std::swap (a, b);
or to use the STL string class you would write
    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:
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:
    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.
  Permalink  
Comments
hor_313 at 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 at 21-Mar-13 10:19am
   
number is a static function of class QString, and it takes 3 arguments; hence the commas.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 
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.
#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[^]
  Permalink  
v2
Comments
phil.o at 21-Mar-13 7:01am
   
5'd as I learned something and to counterbalance the 1-vote you got
Matthew Faithfull at 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 at 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,' '));
   
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 at 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?
José Amílcar Ferreira Casimiro at 21-Mar-13 10:04am
   
You use this code -> QString::number((*itrCircles)[2], 'f' , 3) because the method is static, you don't need to create a class instance to use it.
 
This static method (number) returns an instance of QString and you apply .rightJustified(7,' ')) to that instance.
 
Does this makes sense to you?
 
When you create a instance of a class, something like this:
 
QString myinstance;
 
//then you access object methods trough dot operator like this.
 
myinstance.rightJustified(7,' ')
hor_313 at 21-Mar-13 14:44pm
   
This static method (number) returns an instance of QString and you apply .rightJustified(7,' ')) to that instance. Does this makes sense to you?
 
Exactly , my question is about that dot operator . I taught dot operator only uses after the class instances for access to class methods . So , it uses with static methods too . I got it true?
José Amílcar Ferreira Casimiro at 21-Mar-13 17:12pm
   
Yes, makes sense to me.
 
No. You applied the dot operator to the class instance. If you try to use the dot operator in a static method it will be a syntactic error.
hor_313 at 22-Mar-13 0:52am
   
so , Why dot operator in that code used after the number static method
 
number().justified()
   
Because it is applied to an instance of an object.
hor_313 at 23-Mar-13 3:17am
   
yes , now I got it . Thank you dear friend
   
great
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Main user of Scope resolution operator ( :: ) in c++ is as below..
 
1. To access global variable
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
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
}
  Permalink  
Comments
hor_313 at 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)

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 Zoltán Zörgő 99
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 21 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100