469,913 Members | 2,017 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,913 developers. It's quick & easy.

writing MyClass::Operator unsigned long()

Is it possible to write such an operator function? watch that there is
a space in between unsigned and long.
Does this mean I have to write two operator function one for long and
another for unsigned?

Regards,

Jun 8 '06 #1
8 3038
> Is it possible to write such an operator function? watch that there is
a space in between unsigned and long. Yes, It is possible to write such an operator. Does this mean I have to write two operator function one for long and
another for unsigned?


No, You will not have to write to different operator functions for
overloading (unsigned long)

Akhilesh

Jun 8 '06 #2
CodeCracker wrote:
Is it possible to write such an operator function? watch that there is
a space in between unsigned and long.
Does this mean I have to write two operator function one for long and
another for unsigned?


struct s {
operator unsigned long() { return 42UL; }
};

int main() {
unsigned long ul = s();
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 8 '06 #3

"Victor Bazarov" wrote:
struct s {
operator unsigned long() { return 42UL; }
};

int main() {
unsigned long ul = s();
}


Hi Victor,

that's an interesting syntax. Took me a few to figure out what I was
looking at. It seems odd to call a function via the class name (instead of
an object name), when it's not a static function.

Let me know if I've got this (guess) correct: It's creating a temporary
object of type s, and then (implicitly?) calling operator unsigned long on
that temporary object?

My testing shows it's the same as either of the following:

s s1;
unsigned long ul1 = s1.operator unsigned long();
std::cout << ul1 << std::endl;

s s2;
unsigned long ul2 = unsigned long(s2);
std::cout << ul2 << std::endl;

Is my analysis (guess) as to its meaning correct? Does this syntax have a
name, so I could look it up in my book (The C++ Programming Language;
Stroustrup)?

Thanks,
-Howard

Jun 8 '06 #4

Howard wrote:
"Victor Bazarov" wrote:
struct s {
operator unsigned long() { return 42UL; }
};

int main() {
unsigned long ul = s();
}


Hi Victor,

that's an interesting syntax. Took me a few to figure out what I was
looking at. It seems odd to call a function via the class name (instead of
an object name), when it's not a static function.

Let me know if I've got this (guess) correct: It's creating a temporary
object of type s, and then (implicitly?) calling operator unsigned long on
that temporary object?


correct.

Jun 8 '06 #5
Howard wrote:
"Victor Bazarov" wrote:
struct s {
operator unsigned long() { return 42UL; }
};

int main() {
unsigned long ul = s();
}


[...correct analysis...]

Is my analysis (guess) as to its meaning correct? Does this syntax
have a name, so I could look it up in my book (The C++ Programming
Language; Stroustrup)?


Which one? :-)

In the statement

unsigned long ul = s();

the 's()' part is called "explicit type conversion (functional notation)",
and there is nothing between the parentheses (*). This expression yields
a *value-initialised* rvalue (a temporary) of type 's'.

The part '= s()' is called an initializer, and the process that involves
the whole thing is called "copy-initialization". Another syntax for that
is

unsigned long ul((s()));

(notice double parentheses around 's()', they are required because this:

unsigned long ul(s());

is a function declaration (but that's a different story).

(*) The parentheses can contain a comma-delimited expression list, or
a single expression. The expression[s] become arguments to the
constructor of the 's' type, if there is a constructor that accepts
it/them (determined by overload resolution).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 8 '06 #6

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:e6**********@news.datemas.de...
Howard wrote:
"Victor Bazarov" wrote:
struct s {
operator unsigned long() { return 42UL; }
};

int main() {
unsigned long ul = s();
}

[...correct analysis...]

Is my analysis (guess) as to its meaning correct? Does this syntax
have a name, so I could look it up in my book (The C++ Programming
Language; Stroustrup)?


Which one? :-)

In the statement

unsigned long ul = s();

the 's()' part is called "explicit type conversion (functional notation)",
and there is nothing between the parentheses (*). This expression yields
a *value-initialised* rvalue (a temporary) of type 's'.

That's the info was looking for.
(*) The parentheses can contain a comma-delimited expression list, or
a single expression. The expression[s] become arguments to the
constructor of the 's' type, if there is a constructor that accepts
it/them (determined by overload resolution).


Thanks, guys!
-Howard


Jun 8 '06 #7
Thanks guys for all the responses.
One more point I would like to know is that how come the function name
have a space in between. How compiler handles this. OR is this a
special case and I can't have a function with a space in between.
Thanks in advance.
CC
Howard wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:e6**********@news.datemas.de...
Howard wrote:
"Victor Bazarov" wrote:

struct s {
operator unsigned long() { return 42UL; }
};

int main() {
unsigned long ul = s();
}
[...correct analysis...]

Is my analysis (guess) as to its meaning correct? Does this syntax
have a name, so I could look it up in my book (The C++ Programming
Language; Stroustrup)?


Which one? :-)

In the statement

unsigned long ul = s();

the 's()' part is called "explicit type conversion (functional notation)",
and there is nothing between the parentheses (*). This expression yields
a *value-initialised* rvalue (a temporary) of type 's'.

That's the info was looking for.

(*) The parentheses can contain a comma-delimited expression list, or
a single expression. The expression[s] become arguments to the
constructor of the 's' type, if there is a constructor that accepts
it/them (determined by overload resolution).


Thanks, guys!
-Howard


Jun 9 '06 #8

"CodeCracker" <sa********@gmail.com> wrote in message
news:11**********************@f6g2000cwb.googlegro ups.com...
Thanks guys for all the responses.
One more point I would like to know is that how come the function name
have a space in between. How compiler handles this. OR is this a
special case and I can't have a function with a space in between.
Thanks in advance.
CC


Hi CC,

when responding to posts, please put your responses at the bottom (or
interspersed with what you're responding to, if appropriate). Putting your
responses at the top messes up the order people normally read (from top to
bottom). It's called "top-posting", and it's frowned upon on usenet.

But regarding your question, yes, that's a special case. You can't use
spaces within an identifier (i.e., a function or variable name). But the
compiler knows that "operator", "unsigned" and "long" are reserved words
which will never be identifier names, so it's free to string them together
where appropriate. It does the same thing with type declarations, such as
"unsigned long MyLongInt;": the type is "unsigned long". But you can't do
"unsigned long My Int;".

-Howard

Jun 9 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by cppaddict | last post: by
8 posts views Thread by Floogle | last post: by
4 posts views Thread by bor_kev | last post: by
5 posts views Thread by Rob | last post: by
8 posts views Thread by valerij | last post: by
4 posts views Thread by Ja NE | last post: by
12 posts views Thread by dimstthomas | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.