Thread: Can a pointer to a class be null?

  1. #1
    The Negativity. LAURENT*'s Avatar
    Join Date
    May 2012
    Posts
    68

    Can a pointer to a class be null?

    I want some of my pointer to point to NULL but I'm not sure if this is possible. Does anyone know how I can define a pointer to NULL? I kept getting this error somewhere else in my code when I checked for null pointers.

    "Error c2676 binary '==': 'Object' does not define this operator or a conversion to a type acceptable"


    This is how I did my pointer defining. Pointers in my code only get defined in one place no matter what.
    Code:
    int Total_sv_Objects;
    Total_sv_Objects = MAX_ITEM - 50;
    
    
    //Class Object
    Object *item[MAX_ITEM]
    
    for (int i = 0; i < Total_sv_Objects ; i++)
    {
    
    item = new (Something, ect);
     
    }
    
    for (int i = Total_sv_Objects; i <  MAX_ITEM; i++)
    {
    //Not being used
    item = NULL;
    }
    Last edited by LAURENT*; 07-31-2015 at 10:55 AM.
    UPDATE! As of 10/6/2014

    https://www.dropbox.com/s/2sj6qwpfbb...t%201.zip?dl=0
    Just find the application file and double click it. Controls are (Arrow keys) (Z) (Z + arrow key) (Spacebar)
    Don't play this crappy update. Wait for the newest one which is far more impressive.

    Official Sonic character poll hosted by some guy at Sega..... Sega of America. Vote for blaze because she OP.
    http://blogs.sega.com/2015/08/28/the...down-heats-up/

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by LAURENT*
    Can a pointer to a class be null?
    By "pointer to a class" you presumably mean "pointer to an object of class type", in which case the answer is yes.

    Quote Originally Posted by LAURENT*
    Does anyone know how I can define a pointer to NULL?
    Pre-C++11, you would use:
    Code:
    ptr = 0;
    // or:
    ptr = NULL;
    As of C++11, you would use:
    Code:
    ptr = nullptr;
    Quote Originally Posted by LAURENT*
    "Error c2676 binary '==': 'Object' does not define this operator or a conversion to a type acceptable"
    This means that you tried to call operator== on an object of the Object class, but you did not overload operator== for it. The code that you posted does not contain such a mistake, so you may have posted code that is not relevant.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The Negativity. LAURENT*'s Avatar
    Join Date
    May 2012
    Posts
    68
    Quote Originally Posted by laserlight View Post
    By "pointer to a class" you presumably mean "pointer to an object of class type", in which case the answer is yes.


    Pre-C++11, you would use:
    Code:
    ptr = 0;
    // or:
    ptr = NULL;
    As of C++11, you would use:
    Code:
    ptr = nullptr;

    This means that you tried to call operator== on an object of the Object class, but you did not overload operator== for it. The code that you posted does not contain such a mistake, so you may have posted code that is not relevant.

    I just noticed the real problem. It was a problem with a function argument. Apparently I wrote something like this.

    Code:
    Void function (Class *square)
    {
     //Stuff goes here
    }
    Instead of

    Code:
    Void function (Class *square[])
    
    {
    //Stuff, whatever...
    }
    Anyways thanks for clearing up what's possible.
    UPDATE! As of 10/6/2014

    https://www.dropbox.com/s/2sj6qwpfbb...t%201.zip?dl=0
    Just find the application file and double click it. Controls are (Arrow keys) (Z) (Z + arrow key) (Spacebar)
    Don't play this crappy update. Wait for the newest one which is far more impressive.

    Official Sonic character poll hosted by some guy at Sega..... Sega of America. Vote for blaze because she OP.
    http://blogs.sega.com/2015/08/28/the...down-heats-up/

  4. #4
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by LAURENT* View Post
    I just noticed the real problem. It was a problem with a function argument. Apparently I wrote something like this.

    Code:
    Void function (Class *square)
    {
     //Stuff goes here
    }
    Instead of

    Code:
    Void function (Class *square[])
    
    {
    //Stuff, whatever...
    }
    Anyways thanks for clearing up what's possible.

    You might look into doing it like:

    Code:
        void func (const std::vector<Class*>& squares) {}
    Or even:

    Code:
        void func (const std::vector<std::shared_ptr<Class>>& squares) {}
        // You could do a typedef to make declaring it shorter
    Just a thought anyway. I'm currently rewriting a game engine myself, and I'm using the shared_ptr and container classes quite a lot, they are really convenient. Although I want to write some specialized array like containers myself for a "Graph" class (like
    Code:
    Graph<SDL_Texture*>
    or
    Code:
    Graph<std::shared_ptr<Vertice>>
    , or whatever, just to have member functions that can operate/query many things at once).
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Null pointer vs uninitialized pointer
    By Abhishek Kumar in forum C++ Programming
    Replies: 23
    Last Post: 03-05-2014, 12:01 PM
  2. Does static initialization of pointer make it null pointer
    By Saurabh Mehta in forum C Programming
    Replies: 1
    Last Post: 11-23-2012, 12:05 AM
  3. Null pointer
    By venkatsam in forum C Programming
    Replies: 4
    Last Post: 07-21-2012, 06:02 AM
  4. Static class == NULL
    By pixsta in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2005, 11:14 AM
  5. Null pointer
    By MethodMan in forum C Programming
    Replies: 2
    Last Post: 03-11-2002, 06:49 PM