Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old April 22nd, 2007, 06:05 PM
Johs
Guest
 
Posts: n/a
Default Dot "." or qualify "::" operator?

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

 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles