writing MyClass::Operator unsigned long()

C

CodeCracker

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,
 
A

akhilesh.tiwari10

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
 
V

Victor Bazarov

CodeCracker said:
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
 
H

Howard

Victor Bazarov said:
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
 
N

Noah Roberts

Howard said:
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.
 
V

Victor Bazarov

Howard said:
Victor Bazarov said:
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 become arguments to the
constructor of the 's' type, if there is a constructor that accepts
it/them (determined by overload resolution).

V
 
H

Howard

Victor Bazarov said:
Howard said:
Victor Bazarov said:
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 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
 
C

CodeCracker

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

Victor Bazarov said:
Howard said:
:

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 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
 
H

Howard

CodeCracker said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top