473,795 Members | 3,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Operator ?

Someone can explain me the meanings of the ? operator applied to
declarations like that i found...

private Point? startPoint;

if i delete the ? the compiler gives me some errors but i don't understand
what's the use of ? in that declaration position...
help me please

Jun 27 '08 #1
38 1394
On Sat, 12 Apr 2008 15:18:22 -0700, MBSoftware <mb********@ali ce.itwrote:
Someone can explain me the meanings of the ? operator applied to
declarations like that i found...

private Point? startPoint;

if i delete the ? the compiler gives me some errors but i don't
understand what's the use of ? in that declaration position...
help me please
It's shorthand for "Nullable<Point >". Value type variables (e.g. structs
like Point) cannot normally be set to null. However, you can use a
"nullable" version to allow that.

You'll find that it can be a little inconvenient, due to the fact that to
get back to a regular value type you have to explicitly cast from the
nullable version to the regular version (e.g. "Point point =
(Point)startPoi nt;"). This means without the cast, the "nullable" trait
tends to propagate through your code as you copy values from one variable
to another. :)

But sometimes it makes sense to be able to treat a value type as
nullable. In that case, using the nullable version is just the ticket.

See:
http://msdn2.microsoft.com/en-us/library/1t3y8s4s.aspx
http://msdn2.microsoft.com/en-us/library/2cf62fcy.aspx

Also, a handy operator for resolving null-valued nullable types (e.g. to
provide a default value):
http://msdn2.microsoft.com/en-us/library/ms173224.aspx

Pete
Jun 27 '08 #2
MBSoftware wrote:
Someone can explain me the meanings of the ? operator applied to
declarations like that i found...

private Point? startPoint;

if i delete the ? the compiler gives me some errors but i don't
understand what's the use of ? in that declaration position...
help me please
Point is a structure and therefore a value type. Value types can not be
null. By adding ? to the declaration you make the type nullable. this is
usually used for values with a counterpart in a database which could be
null.

Personally i think it's not smart to delete code that you don't understand.

alain
Jun 27 '08 #3
On Sat, 12 Apr 2008 15:44:52 -0700, Alain Boss <ma******@hotma il.com>
wrote:
[...]
Personally i think it's not smart to delete code that you don't
understand.
Why not?

I mean, you wouldn't want to just delete it and move on. But as part of
trying to understand what the code is doing (which is what the OP was
doing), making changes to the code to see what happens, including deleting
portions of it to see what breaks, can be a very useful part of the
exploration process.

Seems like a very smart thing to do, quite the contrary to your own
thought on the matter.

Didn't you ever take things apart when you were a kid to try to figure out
how they worked?

Pete
Jun 27 '08 #4
Peter Duniho wrote:
On Sat, 12 Apr 2008 15:44:52 -0700, Alain Boss <ma******@hotma il.com>
>Personally i think it's not smart to delete code that you don't
understand.

Why not?

I mean, you wouldn't want to just delete it and move on. But as part of
trying to understand what the code is doing (which is what the OP was
doing), making changes to the code to see what happens, including
deleting portions of it to see what breaks, can be a very useful part of
the exploration process.
If there are close to perfect unit tests it may be OK.

If not then there is the risk of removing some code that was
needed and will cause problems in production.

Removing stuff that causes compile errors is best case not worst case.

Arne
Jun 27 '08 #5
Peter Duniho wrote:
On Sat, 12 Apr 2008 17:43:08 -0700, Arne Vajhøj <ar**@vajhoej.d kwrote:
>If there are close to perfect unit tests it may be OK.

If not then there is the risk of removing some code that was
needed and will cause problems in production.

What makes you think the code is production code in the first place?
How could changing non-production code "cause problems in production"?
>Removing stuff that causes compile errors is best case not worst case.

For any of what you wrote to be relevant, you are making a number of
broad assumptions that you are entirely unjustified in making.

There is absolutely nothing in the original post that justifies saying
that the person who wrote that post or what he did is "not smart".
Neither you nor Alain have nearly enough information about the situation
to pass judgment like that.
The original poster did not say whether it was production code or not.

And then it makes sense to take the conservative approach and
assume that it is production code, because the damage of giving
"production code" advice to "just for fun code" is insignificant
to giving "just for fun code" advice to "production code".
Get off your high horses. The guy's just asking a question, trying to
learn how the language works. There's no reason for you to make a
federal case out of it.
He asked a question. Alan made a good point. You made a problem
out of it.

Arne

Jun 27 '08 #6
On Sat, 12 Apr 2008 18:48:53 -0700, Arne Vajhøj <ar**@vajhoej.d kwrote:
The original poster did not say whether it was production code or not.
That's right, he didn't.
And then it makes sense to take the conservative approach and
assume that it is production code,
No, it doesn't make sense to "assume that it is production code".

If someone wanted to raise that as a possibility, and then provide a
caveat based on and qualified to that possibility, that would be one thing.

But to _assume_ that the OP is doing something stupid is, well...stupid.
As well as insulting to the OP.

Pete
Jun 27 '08 #7
On Sat, 12 Apr 2008 18:03:06 -0700, Jon Skeet [C# MVP] <sk***@pobox.co m>
wrote:
>You'll find that it can be a little inconvenient, due to the fact that
to Â*
get back to a regular value type you have to explicitly cast from the Â*
nullable version to the regular version [...]

I don't think I'd want it to behave in any other way - I certainly
wouldn't want an *implicit* conversion to a non-nullable point.
Nor I. I don't think what I wrote implied that I did, but if so, consider
this a clarification that it's not what I meant to say.

Not everything that's inconvenient is necessary bad.

Pete
Jun 27 '08 #8
On Sat, 12 Apr 2008 20:18:52 -0700, Peter Duniho
<Np*********@nn owslpianmk.comw rote:
[..]
Not everything that's inconvenient is necessary bad.
Or even "necessaril y". :)
Jun 27 '08 #9
Peter Duniho wrote:
On Sat, 12 Apr 2008 18:48:53 -0700, Arne Vajhøj <ar**@vajhoej.d kwrote:
>The original poster did not say whether it was production code or not.

That's right, he didn't.
>And then it makes sense to take the conservative approach and
assume that it is production code,

No, it doesn't make sense to "assume that it is production code".
Since you did not comment on the argument given:

# because the damage of giving
#"production code" advice to "just for fun code" is insignificant
#to giving "just for fun code" advice to "production code".

then I assume that you do not understand the argument.

Do you want me to it explain it in more detail ?
If someone wanted to raise that as a possibility, and then provide a
caveat based on and qualified to that possibility, that would be one thing.

But to _assume_ that the OP is doing something stupid is,
well...stupid. As well as insulting to the OP.
If the original poster plan on a career in software development,
then he better be prepared for that type of comments. There are
no room for "it is probably OK and let us not ask because someone
may feel offended" in software development.

Arne

Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
8034
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so something like this: 1) e = (a, b, c, d); // concatenate a,b,c,d into e 2) (a, b, c, d) = e; // get the bits of e into a,b,c, and d For example, in the second case, assume that a,b,c,d represent 2-bit integers, and e represents an 8-bit...
1
3880
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include "BitString.h"
5
3808
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more complex operator, I get stuck. Here's a brief description what I want to do. I want to simulate a matrix (2D array) from a 1D array. so what I have so far is something like this: class Matrix
0
1836
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should be disabled in a release build, and to do that efficiently, I suppose I need to overload the << operator. My current implementation of the stream in release mode is posted below. Everything works fine for POD type like bool and int, but the...
3
2957
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD FUNCTION!!
6
4430
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
3
18832
by: gugdias | last post by:
I'm coding a simple matrix class, which is resulting in the following error when compiling with g++ 3.4.2 (mingw-special): * declaration of `operator/' as non-function * expected `;' before '<' token <snip> --------------------------------------------------- template <class T> class matriz;
5
2294
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler errors. Some errors claim (contrary to my book) that you cannot use a static function, that you must...
8
2490
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...
3
3284
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj, obj2 ;
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10439
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...
0
10001
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
9043
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...
1
7541
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
6783
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
5437
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
4113
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
3727
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.