473,385 Members | 1,630 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,385 software developers and data experts.

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 3230
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my...
22
by: BekTek | last post by:
Hi.. I'm so sorry about that I've postes so many questions recently.. :) I'm still confused about the differences between malloc and operator new.. I know that when we work with class object and...
8
by: Floogle | last post by:
how do i create a virtual == operator. I've tried the following but it's incorrect... class Interface { ... public: virtual bool operator==(const Interface& rhs)const=0;
4
by: bor_kev | last post by:
Hi! What's the syntax to overload the operator= under Microsoft Visual C++ .NET 2005 in a managed class. I tried : static Myclass^ op_Assign (Myclass^, Myclass^){} but it doesn't work. ...
5
by: Rob | last post by:
To follow up on the "copy constructor clarification thread"... The assignment operator syntax shown previously: MyClass% operator=(const MyClass%); seems to have problems if you have member...
5
by: tom | last post by:
Hi, I'm overriding my operator new and operator delete for two classes, one inherited from the other, so I can use my own memory pool. A simplified version of what I have is below: class...
8
by: valerij | last post by:
Yes, hi How to write "operator +" and "operator =" functions in a class with a defined constructor? The following code demonstrates that I don't really understand how to do it... I think it has...
4
by: Ja NE | last post by:
for no particular reason at this very moment, I would like to echo numebrs of seconds since my site is born. or since I'm born. this isn't problem ($diff = time() - mktime($begining) but it looks...
12
by: dimstthomas | last post by:
I have a class that already has == and != operators that I want to use in a map that uses my class (not a pointer to the class) as the key. To do this I need a < operator. Is this a valid less than...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.