473,770 Members | 7,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

writing MyClass::Operat or 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 3251
> 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.********@com Acast.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.********@com Acast.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

"CodeCracke r" <sa********@gma il.com> wrote in message
news:11******** **************@ f6g2000cwb.goog legroups.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
2145
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 problematic attempt to implement a subscript operator that returns a const reference. The dubious code is marked at the end. <code>
22
2587
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 use operator new/delete, the ctor and dtor get called as expected, but malloc and free do not.. But Herbal Sutter mentioned in his greate book 'exceptional c++' about free storage
8
17772
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
4779
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. don't know why....
5
1463
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 properties that need to be copied from the rhs to the lhs. Since the rhs parameter is "const MyClass", you get an error C2662 "'MyClass::prop::get' : cannot covert 'this' pointer from 'const MyClass' to 'MyClass %'" if you have code inside like
5
1892
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 BaseClass { BaseClass(); virtual ~BaseClass(); virtual void baseFunction();
8
2489
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 something to do with the compiler calling the destructor twice. Could someone point out where I go wrong? P.S.: The error it gives is "Debug Assertion Failure ....." (at run time) P.P.S: Everything else works just fine (without the use of...
4
1919
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 ugly on screen: using this two functions substr(chunk_split($diff, 3, '.'), 0, -1); I'm lucky with my site: 277142273 seconds = 277.142.273 but with myself: 1257865147 seconds = 125.786.514.7 so I would like to echo 1.257.865.167 ...can...
12
5767
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 operator to use in this case? bool MyClass::operator<(const MyClass &other) const { if (*this == other) return false; return (this < &other);
0
9592
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10230
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10004
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9870
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7416
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.