Connecting Tech Pros Worldwide Help | Site Map

Dot "." or qualify "::" operator?

  #1  
Old April 22nd, 2007, 06:05 PM
Johs
Guest
 
Posts: n/a
When I declare a string in C++ I type:

std::string mystring = "sdfsdf";

afterwards I can access string methods like:

mystring.

but why is there both :: and . operators and what are the difference?
  #2  
Old April 22nd, 2007, 06:25 PM
Colander
Guest
 
Posts: n/a

re: Dot "." or qualify "::" operator?


On Apr 22, 7:03 pm, Johs <a...@asd.comwrote:
Quote:
When I declare a string in C++ I type:
>
std::string mystring = "sdfsdf";
>
afterwards I can access string methods like:
>
mystring.
>
but why is there both :: and . operators and what are the difference?
:: works on types/namespaces, . works on instances.

std is a namespace, so you use ::
mystring is a variable, so you use .

Class A
{
public:
static int b;
}


// A is an type
A::b;

or

// a is an variable
A a;
a.b;

  #3  
Old April 22nd, 2007, 06:25 PM
Rolf Magnus
Guest
 
Posts: n/a

re: Dot "." or qualify "::" operator?


Johs wrote:
Quote:
When I declare a string in C++ I type:
>
std::string mystring = "sdfsdf";
>
afterwards I can access string methods like:
>
mystring.
>
but why is there both :: and . operators and what are the difference?
The former is used for classes and namespaces, the latter for objects.

  #4  
Old April 22nd, 2007, 08:35 PM
Ron AF Greve
Guest
 
Posts: n/a

re: Dot "." or qualify "::" operator?


Hi,

In addition to the previous answers. If you have for instance a class with a
static function (i.e. independent of a specific object (the 'this' pointer
is not passed)) you could use ::

class example
{
public:
static void StaticFunction()
{
}
void ObjectFunction()
{
}
};


.......
#include <memory>
using namespace std;
....

example::StaticFunction(); // this is ok, no object (this pointer) available
or needed)

auto_ptr<exampleEx( new example);
Ex->ObjectFunction(); // this too, we need a real object here, the this
pointer is needed (invisible, pushed last on the stack)


Regards, Ron AF Greve

http://www.InformationSuperHighway.eu

"Rolf Magnus" <ramagnus@t-online.dewrote in message
news:f0g5j6$8vu$01$1@news.t-online.com...
Quote:
Johs wrote:
>
Quote:
>When I declare a string in C++ I type:
>>
>std::string mystring = "sdfsdf";
>>
>afterwards I can access string methods like:
>>
>mystring.
>>
>but why is there both :: and . operators and what are the difference?
>
The former is used for classes and namespaces, the latter for objects.
>

Closed Thread