473,395 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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?
Apr 22 '07 #1
3 1565
On Apr 22, 7:03 pm, Johs <a...@asd.comwrote:
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;

Apr 22 '07 #2
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.

Apr 22 '07 #3
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" <ra******@t-online.dewrote in message
news:f0*************@news.t-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.

Apr 22 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it...
10
by: Piotr Wyderski | last post by:
Hello, is it possible to reuse a friend operator which is defined inside a class? I'd like to obtain the following behaviour: class integer { integer operator +(signed long int v) const...
5
by: Teddy | last post by:
Hello all consider the class Date declaretion below: class Date { public: Date(); Date(int year, int month, int day); Date(const string&); int getYear() const;
3
by: gugdias | last post by:
I'm coding a simple matrix class, which is resulting in the following error when compiling with g++ 3.4.2 (mingw-special): * declaration of `operator/' as non-function * expected `;' before '<'...
14
by: Richard Thornley | last post by:
Hello, I realize that this is a very basic question but is there an advantage to using Me.ControlName.Text over just using ControlName.Text? I have been leaving the Me. off but would include it...
4
by: kelvSYC | last post by:
As we all know, iostreams cannot be assigned or copied. This gives me a problem: suppose we have class Foo { std::istream in; public: Foo::Foo(std::istream& in_) : in(in_.rdbuf()) {} }; ...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
13
by: hn.ft.pris | last post by:
Hi: I have the following simple program: #include<iostream> using namespace std; int main(int argc, char* argv){ const double L = 1.234; const int T = static_cast<const int>(L); int arr;
1
by: Nikhil.S.Ketkar | last post by:
Hi, Does the following construct qualify as overloading on return type ? If not, in what way ? I overloaded the type conversion operator and used pointers to member functions. Its pretty...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.