473,762 Members | 8,115 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 3249
> 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
2144
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
2584
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
17771
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
1462
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
2487
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
1918
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
5766
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
9554
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...
1
9927
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
9812
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...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5268
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...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3914
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
3
3510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.