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

Discussion in 'C++' started by Johs, Apr 22, 2007.

  1. Johs

    Johs Guest

    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?
     
    Johs, Apr 22, 2007
    #1
    1. Advertising

  2. Johs

    Colander Guest

    On Apr 22, 7:03 pm, 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?


    :: 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;
     
    Colander, Apr 22, 2007
    #2
    1. Advertising

  3. Johs

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Apr 22, 2007
    #3
  4. Johs

    Ron AF Greve Guest

    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" <> wrote in message
    news:f0g5j6$8vu$01$-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.
    >
     
    Ron AF Greve, Apr 22, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. karim
    Replies:
    5
    Views:
    2,984
    David Waz...
    Jul 11, 2003
  2. Replies:
    0
    Views:
    315
  3. Evert Wiesenekker

    Qualify/Reference datagrid cells by name

    Evert Wiesenekker, Jun 1, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    125
    Eliyahu Goldin
    Jun 1, 2004
  4. SHIGETOMI, Takuhiko
    Replies:
    9
    Views:
    133
    why the lucky stiff
    Sep 12, 2005
  5. Replies:
    6
    Views:
    270
    Thomas 'PointedEars' Lahn
    Dec 12, 2005
Loading...

Share This Page