operator :

Hi, could someone explain what the ':' operator does. I know it's used in inheritance and know how to do that however, i've seen it used on a class's constructor with no inheritance which doesn't make any sense to me; what does it do/mean?

Microsoft provides a definition of it, as follows:

operator :

segment : expression
Overrides the default segment of expression with segment. The segment can be a segment register, group name, segment name, or segment expression. The expression must be a constant.

...but I don't think it really explains a lot. Could someone expand on it; perhaps with an explained example.

Thanks for any help.
(Sorry if there has already been a similar post)
Last edited on
The colon is used in many situations, what you are describing is the initializer list of a constructor:
1
2
3
4
5
6
7
8
9
10
11
class BaseClass
{
     public:
        BaseClass ( /*some parameters*/ );
};
class MyClass: public BaseClass
{
     public:
         int some_member;
         MyClass ( ) : BaseClass ( /*some arguments*/ ), some_member ( 123 ) {}
};

On line 10 the constructor of MyClass is calling BaseClas constructor and initializing some_member

An example without inheritance:
1
2
3
4
5
6
7
class MyClass
{
         int some_member;
         std::string some_other_member;
     public:
         MyClass ( ) :  some_member ( 123 ), some_other_member( "Hello" )  {}
};

[edit] The following refers to programming with C and C++ [/edit]

It should be noted that : is not technically an operator.

It is a lexical element, yes, but not an operator.


[edit] The following refers to programming with MASM and other like-minded x86 Assemblers [/edit]

That old MS documentation (which, btw, looks like it came out of a MASM manual) refers to the way in which assemblers override the default segment with a chosen one. For example, anything working with SI is typically working in the DS segment. However, you may wish to point SI at something in the ES segment. Hence, [SI] is equivalent to [DS:SI], but there is only one way to express [ES:SI]. (An alternate notation is ES:[SI].)

Hope this helps.
Last edited on
It should be noted that : is not technically an operator.

It can be part of the conditional operator
Part of a cigar is not a cigar. (Sorry Freud.)
[IQ=0]

What about :: ?
The first : is half operator, the second : is half operator.
So, being two halves of an operator it's an operator

Other important use of the colon:
:-)

[/IQ=0]
LOL. :-D
Sometimes a token is just a token.
And sometimes it is a sign of a serious parent/child relationship issue.
Topic archived. No new replies allowed.