0

Here's what I have done: I've got a simple class:

class Person{
  public:
    Person();
}

And in my main:

int main() {
  Person myPer = NULL;
}

This is impossible since C++ does not allow that, however:

int main() {
  Person* perPtr = NULL;
  Person myPer = *perPtr; // corrected, it was &perPtr(typo error) before answers
}

This compiles fine and as I see I did able to have a NULL object. So isn't it violating the rule that only pointers can be null in C++? Or is there such a rule in C++? 2nd one is after I wrote this code, I added a if statement checking whether myPer is NULL or not but that gave me error. So does it show that C++ does not really like the NULL object idea no matter what you do to make objects NULL...

  • 2
    `Person myPer = &perPtr;` is a type error. Did you mean `Person myPers = *perPtr;`? –  Oct 28 '11 at 17:02
  • 5
    Welcome to Stack Overflow! If you're learning C++, I'll give you this most important tip: just because it compiles it does not mean it's valid or that it should work. And similarly just because it compiles and runs as you expected it doesn't mean it's correct either. Yes, C++ is a very harsh language :( I'll recommend [a good introductory C++ book](http://stackoverflow.com/q/388242/46642). – R. Martinho Fernandes Oct 28 '11 at 17:03
  • 3
    "This compiles fine". No, it doesn't. Please copy-paste (not retype) the exact program you compiled. – Robᵩ Oct 28 '11 at 17:03
  • 2
    @Blastfurnace: Um...objects can't be `null` in Java, either. Object *references* can be `null`. – T.J. Crowder Oct 28 '11 at 17:04
  • 1
    You should also think seriously about *why* you want an "object to be null". Often (not always, but often) this is a symptom of missing the goal and getting fixated on something that *you* believe is a step towards it, while the actual problem possibly has a much more elegant solution. – Kerrek SB Oct 28 '11 at 17:07
  • @T.J. Crowder: Thank you for the correction. – Blastfurnace Oct 28 '11 at 17:08
  • Yes, I meant Person myPers = *perPtr; and I have great answers,thanks all... – John Gunduz Oct 28 '11 at 17:16

7 Answers7

8

Objects cannot be null, only pointers can. Your code is incorrect and does not compile, since its trying to initialize a Person from a pointer to a pointer to Person. If you were to change your code to

Person* perPtr = NULL;
Person myPer = *perPtr;

then it would be trying to initialize a Person out of a dereferenced null pointer to a Person, which is undefined behavior (and most likely a crash).

If you need to use the idioms where an object could be in a NULL state, you could use Boost.Optional:

boost::optional< Person > myPer = boost::none;
if( myPer )
{
    myPer->do_something();
}

It's a generalization of what is usually done with pointers, except it does not use dynamic allocation.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
4

This is undefined behaviour. C++ references cannot be legally set to NULL. If you want a "nullable reference", use a pointer.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

This is called undefined behavior. Unexpected results may happen when you attempt to dereference NULL or get the address of NULL.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
0

References are basically syntactically nicer way of saying pointers.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • There's a hair more than just syntax, but only a hair. A reference cannot legally be NULL, nor can it be reset. – Mooing Duck Oct 28 '11 at 17:03
  • I was just cautious, since there's a lot of people who think that references are pointers with syntactic sugar, and then end up dereferencing NULL pointers all over. – Mooing Duck Oct 28 '11 at 17:08
  • I see, the point of my answer was provide an intuitive understanding of what has *actually happened*, not how to use references. – Michael Krelin - hacker Oct 28 '11 at 17:15
0

You can make a class that has "NULL" state.

E.g. a class that owns something else, such as a file handle or a window handle or anything else, such that it can be empty or not.

You can see this with any string class or with any container class.

if (x.empty()) ...

But the concept of "is null" is limited to pointers and smart pointers (or any class that you override to support such use cases.

Mordachai
  • 9,412
  • 6
  • 60
  • 112
0

You can not have a NULL object in C++. Your first attempt is trying to set an object equal to a pointer, and thus fails.

You can have a NULL pointer, and references are simply pointers with slightly different syntax.

You can de-reference a NULL pointer (as in the compiler will let you), but that's undefined behavior. If you're lucky, dereferencing NULL will crash, so you know what's going on.

Michael Kohne
  • 11,888
  • 3
  • 47
  • 79
0

I would not say the following is impossible since C++ does not allow it:

int main() {
  Person myPer = NULL;
}

It is possible, and C++ does allow it. It all depends on how you've defined the classPerson. For example, if the class Person has a constructor as shown below:

class Person
{
  public:
    Person(char *) {}
};

then Person myPer = NULL will compile just fine : http://www.ideone.com/586Pf

Now how much useful such class can be is up to you. One may exploit the above fact, and may come up with cool and useful (utility) class.

Nawaz
  • 353,942
  • 115
  • 666
  • 851