6 Replies - 482 Views - Last Post: 04 May 2009 - 08:37 AM Rate Topic: -----

#1 programmer_temae  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 01-May 09

What is the diference betwen the operator :: and :

Posted 03 May 2009 - 08:38 PM

Hello, when is necesary to use the operator :: and when is necesary to use the operator :

Thanks.
Is This A Good Question/Topic? 0
  • +

Replies To: What is the diference betwen the operator :: and :

#2 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1155
  • View blog
  • Posts: 7,167
  • Joined: 07-September 06

Re: What is the diference betwen the operator :: and :

Posted 03 May 2009 - 08:44 PM

:: is used when calling a static class method, and I am not sure what the : operator is used for... maybe nothing?...

You use :: like so:
std::cout


Hope that makes sense.
Was This Post Helpful? 0
  • +
  • -

#3 programmer_temae  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 01-May 09

Re: What is the diference betwen the operator :: and :

Posted 03 May 2009 - 08:48 PM

the : (unary resolution operator) is used when i pass call a base-class constructor but i'm not sure when the binary resolution operator(:: ) is called. but thanks.

This post has been edited by programmer_temae: 03 May 2009 - 08:49 PM

Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: What is the diference betwen the operator :: and :

Posted 04 May 2009 - 05:13 AM

You are confused about the unary resolution operator ( :: ) and the scope resolution operator( :: ).
Not at all sure where you got ( : ) from.
Maybe a typing error somewhere?
Read this
http://publib.boulde...ref/cplr175.htm

EDIT - put in some whitespace to get rid of unintended smileys

This post has been edited by janotte: 04 May 2009 - 01:43 PM

Was This Post Helpful? 0
  • +
  • -

#5 programmer_temae  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 01-May 09

Re: What is the diference betwen the operator :: and :

Posted 04 May 2009 - 07:14 AM

View Postjanotte, on 4 May, 2009 - 04:13 AM, said:

You are confused about the unary resolution operator (::) and the scope resolution operator(::).
Not at all sure where you got (:) from.
Maybe a typing error somewhere?
Read this
http://publib.boulde...ref/cplr175.htm

thanks :rolleyes:
Was This Post Helpful? 0
  • +
  • -

#6 BlakeJustBlake  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 26
  • View blog
  • Posts: 441
  • Joined: 15-February 09

Re: What is the diference betwen the operator :: and :

Posted 04 May 2009 - 07:21 AM

: is used for a lot of things, but I don't think it would really be considered an operator. The closest place it'd probably come to being an operator would be in inheritance:

class base {
private:
int privatedata;
protected:
virtual int protected_abstract_method();
public:
base();
void public_method();
}

class child : public base {
private:
int protected_abstract_method();
public:
child();
}


Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: What is the diference betwen the operator :: and :

Posted 04 May 2009 - 08:37 AM

As Blake pointed out the : symbol is used for a variety of things...
  • As part of a class declaration line to show in inheritance.
  • As part of a constructor to start the initialization list.
  • As part of a the conditional operator.
  • As part of the switch-case statement.
  • Line Labels
  • Class member visibility modifiers

I would not be surprised if I left off one or two...
#include <iostream>

class Foo {
	//Here the ':' is used to determin visability...
	public:
	int fooVar;
	
	Foo(int arg) : fooVar(arg) { }
	
	void doFoo() {
		std::cout << "Foo at: " << fooVar << std::endl;
	}
};
	
//Here the ':' is ued to specify the super class.	
class Bar : public Foo {
	public:
	int barVar;
	
	Bar(int arg1, int arg2) : barVar(arg1), Foo(arg2) { }
	
	void doFoo() {
		switch(Foo::fooVar) {
			//here used in the switch-case blocks
			case 1:
				std::cout << "First Foo, Bar at: " << barVar << std::endl;
				break;
			default:
				std::cout << "Foo at " << Foo::fooVar << ", Bar at: " << barVar << std::endl;
		}
	}
};

int main() {
	Foo foo(1);
	Bar bar(1, 2);
	//here used to make a line label
	begin:
		foo.doFoo();
		bar.doFoo();
		//here used in the conditional operator
		bar.barVar += (foo.fooVar++ < 4) ? foo.fooVar  :  bar.barVar;
		if (bar.barVar < 20) goto begin;
	
	return 0;
}


The :: operator is used to resolve naming. It resolves names from inside of namespaces, or from within classes. For example in the above code to access the "fooVar" from inside of the Bar class I used "Foo::" to resolve the name (tell the compiler where to look for the variable).
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1