473,320 Members | 2,193 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,320 software developers and data experts.

Test if this is NULL

1
Hi,

some time it would make code easier to check if this is NULL.
For instance, instead of :
Expand|Select|Wrap|Line Numbers
  1. class foo
       {
  2.    int x ;
  3.    void bar () { cout << "my x is " << x ;} // crashes if this is null !
  4.    } ;foo * f = ... ;
  5. if (f) f->bar() ; // so check before to call !
  6.  
It would be nice to write :
Expand|Select|Wrap|Line Numbers
  1. class foo
    {
  2.    int x ;
  3.    void bar () { if (this) cout << "my x is " << x ;} // won't crash if this is null
  4.    } ;foo * f = ... ;
  5. f->bar() ; // call and don't care !
  6.  
Actually, I use this trick already, but my question is :

Will it always work ?
What if the method 'bar' is virtual ?
Is it compiler dependant ?
Is there some thing about this in some holly book of c++ ?

thanks !

Gaspos
Mar 20 '07 #1
1 1207
DeMan
1,806 1GB
I'm not sure I can see how "this" can be NULL and the "cout" exists at the same time

I believe (and am sure to be corrected if I'm wrong), that NULL is simply a pointer to a magical destination in memory. While I would imagine this is the same on any compiler, it is a good habit (for clarity as much as anything else) to be very explicit with what you are testing and would recommend using something like
if(this == NULL){}
Mar 20 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Tim Haughton | last post by:
I've just released an article on using Test Driven Development with C# and Windows Forms. GUI's are often difficult to test, so I thought it might be of interest. The article along with the...
34
by: Andrew | last post by:
Is there anyway to test if a pointer points to allocated memory or not? For example if I have a pointer such as char *p is there a standard way to test whether an assignment such as the following...
3
by: Mark Oliver | last post by:
Hi, When I test my user defined class type passed parameter variable for null it throws an exception. If I use class "string" the null test doesn't through an exception. What could I be...
9
by: Steve Sargent | last post by:
Hi: I'm trying to debug the following code, and it keeps looping on the if statement: public static bool operator == (OnlineMemberNode first, OnlineMemberNode second) { if(first == null) {
5
by: Little | last post by:
I have this program and I need to work on the test portion, which tests if a Val is in the list. It returns false no matter what could you look at the part and see what might need to be done to fix...
2
by: Netkiller | last post by:
#!/usr/bin/python # -*- coding: utf-8 -*- """ Project: Network News Transport Protocol Server Program Description: 基于数据库的新闻组,实现BBS前端使用NNTP协议来访问贴子...
176
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
2
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
4
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
4
by: istillshine | last post by:
I have a function foo, shown below. Is it a good idea to test each argument against my assumption? I think it is safer. However, I notice that people usually don't test the validity of...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.