Sign in to follow this  

Quick C++ question on pointers

This topic is 1115 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

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

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.  

 

 

You totally cracked it, it was due to the fact that the pointer was uninitialized.

 

You rock thanks.

Share this post


Link to post
Share on other sites

Just been working on it a bit more and realized that the original code only needed to be tweaked ever so slightly.

 

In the class ChangeChar, the following needed to be a reference to the class rather than a pointer to a pointer.

 

void fChangeHealth(MyClass &myChar, int h) { myChar.fAlterHealth(h); }
void fChangeName(MyClass &myChar, string n) { myChar.fAlterName(n); }

 

This is obviously not great as working with pointers is a better way round, but for anyone searching I thought it best to illustrate that both pointers and reference are possible outcomes.

Share this post


Link to post
Share on other sites

Why do you need a class specifically for modifying another? Why don't you want the original class modifying itself?


It's not a class, really. It's just a bunch of free functions that have been unfortunately packed into a class for no apparent reason. But yes, they don't seem to provide anything beyond what members of the class they take as first argument already provide. Edited by Álvaro

Share this post


Link to post
Share on other sites

Thank you Ravyne that's a great explanation.

 

The reason I packed some useless functions into classes for no reason was because it wasn't working somewhere else and I wanted to be sure that I was doing it correctly.

 

Thanks for the help everyone.

Share this post


Link to post
Share on other sites

This topic is 1115 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this