Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Dot "." or qualify "::" operator?

Reply
Thread Tools

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

 
 
Johs
Guest
Posts: n/a
 
      04-22-2007
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?
 
Reply With Quote
 
 
 
 
Colander
Guest
Posts: n/a
 
      04-22-2007
On Apr 22, 7:03 pm, Johs <(E-Mail Removed)> wrote:
> 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;

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      04-22-2007
Johs wrote:

> 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.

 
Reply With Quote
 
Ron AF Greve
Guest
Posts: n/a
 
      04-22-2007
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<example> Ex( 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" <(E-Mail Removed)> wrote in message
news:f0g5j6$8vu$01$(E-Mail Removed)-online.com...
> Johs wrote:
>
>> 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.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: what is the difference between the vb dot net and asp dot net Nathan Sokalski ASP .Net 11 08-14-2009 08:22 PM
Dot Net Project Execution without Dot Net and Framework.... krishnan ASP .Net Building Controls 0 01-07-2006 08:51 AM
Dynamically resize visible text and append dot dot dot (like gmail subject lines.. ) how to question? jawolter@gmail.com Javascript 6 12-12-2005 04:09 PM
volatile struct in dot h vs dot c Christopher M. Lusardi C Programming 3 05-15-2004 12:17 AM
To dot or not to dot? SamuŽl van Laere HTML 8 10-16-2003 02:55 PM



Advertisments