Jump to content
  • Advertisement
Sign in to follow this  
Ubermeowmix

Quick C++ question on pointers

This topic is 1331 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can anyone help with pointers in functions?

 

When I declare a pointer in a function outside of main:

void aFunction()
{
    SomethingClass *current = NULL;   
}

int main()
{
    aFunction();
}

When this is called what happens to the pointer, obviously assuming that I have created a class as well.

 

Do I need to delete this pointer, is it left as a dangling pointer on close? Or is it only when the 'new' keyword is used?

Share this post


Link to post
Share on other sites
Advertisement

Following on from that situation, why can't I do the following?

#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

class MyClass
{
    int _health;
    string _name;
public:
    MyClass() : _health(10), _name("John Doe") {}
    MyClass(int h, string n) : _health(h), _name(n) {}
    ~MyClass(){}

    int fReturnHealth() { return _health; }
    string fReturnName() { return _name; }

    void fAlterHealth(int h) { _health = h; }
    void fAlterName(string n) { _name = n; }
};

class ChangeChar
{
public:
    ChangeChar(){}

    void fChangeHealth(MyClass *myChar, int h) { myChar->fAlterHealth(h); }
    void fChangeName(MyClass *myChar, string n) { myChar->fAlterName(n); }
};

int main()
{
    MyClass newChar;
    MyClass newChar2(20, "Paul");

    ChangeChar alterMyChar;

    cout << "newChar: " << newChar.fReturnHealth() << endl;
    cout << "newChar: " << newChar.fReturnName() << endl;

    cout << "newChar2: " << newChar2.fReturnHealth() << endl;
    cout << "newChar2: " << newChar2.fReturnName() << endl;

    alterMyChar.fChangeHealth(newChar2, 15);
    alterMyChar.fChangeName(newChar2, "Timmy");

    cout << "newChar2: " << newChar2.fReturnHealth() << endl;
    cout << "newChar2: " << newChar2.fReturnName() << endl;

    return 0;
}

Share this post


Link to post
Share on other sites

Yeah it's crashing!

 

Just noticed that I wasn't passing a pointer:

 

MyClass* newChar;

 

and that was causing part of the issue, but now I have a random const char* error!?

 

cannot convert 'const char*' to 'MyClass*' in initialization

 

I haven't declared a const char*???

Share this post


Link to post
Share on other sites
It sounds like you changed your code since the last time you posted it. Can you post some piece of code and a clear description of what the problem is?

I'll try to help you, even though your code seems to be affected by the former-Java-programmer disease. smile.png

Share this post


Link to post
Share on other sites

Just glancing throught this thread... but:

 

 

 


cannot convert 'const char*' to 'MyClass*' in initialization

 

 

You have not initialised the pointer.

 

 

Todo so you need to do:

   MyClass * newChar;
   newChar = new MyClass(arg1, "arg2");

//.... Now you can use newChar's members

Edit:

 

Just looked back at your code, I noticed that newChar and NewChar2 are non pointer objects, but you use them as pointers in: ChangeChar  class.  If you change them to pointers (as the code above) it should work.  

 

--Sorry not much time to go indepth, hope this helps/correct

Edited by dsm1891

Share this post


Link to post
Share on other sites

It sounds like you changed your code since the last time you posted it. Can you post some piece of code and a clear description of what the problem is?

I'll try to help you, even though your code seems to be affected by the former-Java-programmer disease. smile.png

 

LOL my roots were in javascript and actionscript so it's been a rocky transition at best, the only advantage I had was some early Visual Basic at college to help me along.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!