Converting null pointer to object of class (C++)

Hey guys. So I normally don’t post questions, but I’m getting a headache from figuring out how to convert a null pointer to a class. This is a final project for my C++ class and I have everything working the way I want it to, but the application keeps crashing because I have a read access violation error due to the pointer being null.

I could do something like

GameObject *pMyPointer = new GameObject();

However, the GameObject class has 4 pure virtual methods which I don’t want to duplicate (too redundant). I was basically wondering if there was a way to use the original GameObject class without having to re-create multiple pure virtual methods. I believe the current file I’m using doesn’t inherit from the GameObject class, but the parent of this class does inherit from GameObject which is how I am still able to use methods from GameObject.

——

A little bit of note: I can’t post all or a lot of the code since it’s majority of my professor’s. And I don’t think he’ll like his codes being posted online.

Did the instructor teach you how to use casts? Well maybe not, because it is unlikely the instructor said to use a null pointer like this. Use of null pointers for this is not good.

Well, the instructor didn’t really say much about null pointers. He just said that null pointers are basically just something that points to a random spot in memory. The only thing that’s stopping me from finishing is this small little piece. Otherwise, I’d actually remove it and just move on with the project. But this is also a piece of a teammate’s idea. They wanted to be able to switch between different types of weapons when they shoot a specific enemy type. We have to first determine what kind of action was taken on the enemy before we could move on. This is where the null pointer is coming from because the instructor helped my teammate make the changes using the null pointer and now I’m trying to implement what the instructor did into our final project.

It’s currently being created like so

GameObject *pMyPointer;

Which is why it’s a null pointer. The original GameObject object isn’t global either. So I’m stuck with either removing this altogether or having to change those pure virtual methods in the GameObject class. Though I’m not sure if that would be wise since the instructor made those methods pure for a reason.

Sounds like you have a bad instructor. Students that have learned to program that way will get ridicule from fellow programmers when they get work. They might not get work when they program that way.

I am not a C++ and OOP expert so I am unfamiliar with the terminology that experts would use but I think you need a base class and a set of derived classes. I think you know that. The thing that is confusing me is that you say something about an interface and probably you do not need an interface, just a base class with virtual methods that can be overridden in the derived classes.

You’ll need to post some code. As best as I can speculatively guess, you have a GameObject instance somewhere, but maybe you forgot to pass a parameter somewhere or forgot to do an assignment somewhere.

Actually no. He’s a good instructor. The project we have are pretty advanced even for our class. My teammates were struggling because we thought we could figure how to implement stuff by ourselves. We’ve struggled for a good 3 weeks until we finally asked our instructor for help. So we actually learned stuff. He even said that he’s not going to give us the answers. But when it got too hard, he finally helped.

@SamuelCalifornia @Jeff_Mott
My professor is actually helping my teammate now. So I guess the problem is solved. I was just going to help and see what I could do, but I got stuck here. Also, I think I’m getting the this was originally a nullptr read access error because the GameObject reference was originally a null pointer. I’ve looked up a few solutions and came to the conclusion that it was supposed to look like this

GameObject *pGameObject = pOldGameObject;

But I couldn’t access pOldGameObject because it was set to private. I’ll let my professor do the rest since he knows more about C++ than I do. So for now, the problem has been solved.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.