Jump to content
  • Advertisement
Sign in to follow this  
Maverick Programmer

Making a function to construct a class pointer object and returning it... problems.

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

I'm trying to construct a function that takes care of the constructing methods of a pointer. I know this sounds like I'm being lazy, but I have decent reasons for wanting to achieve this. So this is what I have:
class A
{
protected:

int m_iNumber;
float m_fNumber;
bool m_bBool;
void SetBoolean( bool tf ) { m_bBool = tf; }

public:

friend void BuildObject( A * a_t, int iValue, float fValue )
{
     if( a_t != NULL )
     {
          delete a_t;
     }

     a_t = new A( iValue, fValue );

     /*...*/

     bool bValue = false;
     if( iValue == fValue ) bValue = true;

     a_t->SetBoolean( bValue );
}

friend void PrintObject( A * a_t )
{
     if( a_t == NULL )
     {
          cout << "The A object is NULL. PrintObject() is ignoring.\n";
          return;
     }
 
     cout << "\nInteger is" << a_t->m_iNumber << "\n";
     cout << "Floating point is" << a_t->m_fNumber << "\n";
     cout << "Boolean is";
     if( m_bBool ) { cout << "true\n"; } else { cout << "false\n"; }
}

A( int integer, float floatingpoint )
{
m_iNumber = integer;
m_fNumber = floatingpoint;
m_bBool = false;
}

~A() {}

};


Now in main.cpp:

int main( int argc, char** argv )
{

A* objectA;
BuildObject( objectA, 3, 5.21f );
PrintObject( objectA );

/* All went well so exit */
return 0;

}


In theory, being that the first argument is a pointer reference, I thought that whatever happened to the variable in the function would return it to objectA and everything be fine. But it returns to be NULL. I discovered, however, that the a_t pointer is not NULL, but why is it not returning back to objectA? Can someone please help with this problem? I hope I made myself and the problem clear. Thank you all in advanced! ~PCN

Share this post


Link to post
Share on other sites
Advertisement
you are passing a pointer, which is copied into the function and inside you build function the copied pointer is modified, which makes it useles it should be a A**.

And do like *object = new blabla.

--
main() {
A* object;
build(&object);
}


build(A** object) {
*object = new A;
}

Share this post


Link to post
Share on other sites
Your first argument is not a reference, rather, its just a pointer. You have several problems in your code:


int main( int argc, char** argv )
{
A* objectA; // <-- OBJECT NOT INITIALIZED, POINTER HAS GARBAGE DATA!!!!
BuildObject( objectA, 3, 5.21f ); // <-- DOES NOT PASS IN POINTER REFERENCE
PrintObject( objectA );

/* All went well so exit */
return 0;
}



First initialize that pointer to NULL, else when you pass it in, it will be pointing to garbage. If you try to delete garbage, heap corruption errors will ensue. Secondly, you need to rewrite your builder function to something like this:


void BuildObject( A ** a_t, int iValue, float fValue )
{
A *aPtr = *a_t;
if(aPtr)
{
delete aPtr;
aPtr = NULL;
}

aPtr = new A( iValue, fValue );
aPtr->SetBoolean( bValue );
}

int main( int argc, char** argv )
{
A* objectA = NULL; // <-- INITIALIZED
BuildObject(&objectA, 3, 5.21f ); // <-- PASS IN ADDRESS OF POINTER (A **)

// And so on...
}


Share this post


Link to post
Share on other sites
Ah, thank you. I hope it does the trick. you see, I was trying to make it so I could avoid initializing objectA entirely so I could do this and get away with it:


A* objectA;
BuildObject( objectA ) /* initializes and constructs object
without any objectA = NULL before it's passed */




But if that's all, I suppose it'll suffice. thank you all once again!

Share this post


Link to post
Share on other sites
Also, you need not check for null before deleting an object. Operator delete works just fine on null pointers (it just doesn't do anything in that case).

Share this post


Link to post
Share on other sites
any particular reason why you need to pass a in a pointer?
friend A* BuildObject( int iValue, float fValue )
{
A * a_t = new A( iValue, fValue );

/*...*/

bool bValue = false;
if( iValue == fValue ) bValue = true;

a_t->SetBoolean( bValue );
return a_t;
}

int main( int argc, char** argv )
{
A* objectA = BuildObject( 3, 5.21f );
PrintObject( objectA );

/* All went well so exit */
return 0;
}

Share this post


Link to post
Share on other sites
In idiomatic C++ (which has this strange habit of being surprisingly short), it looks rather more like this:


class A {
// As will only be constructed via the friend function.
A(int i, float f, bool b) : i(i), f(f), b(b) {}

protected:
int i;
float f;
bool b;

public:
friend std::auto_ptr<A> Create(int i, float f) {
return std::auto_ptr<A>(new A(i, f, i == f));
}

friend std::ostream& operator<<(ostream& os, const A& a) {
return os << "\nInteger is" << a.i << "\nFloating point is" << a.f << "\nBoolean is" << (a.b ? "true" : "false") << "\n";
}
};

int main( int argc, char** argv ) {
std::auto_ptr<A> a = A::Create(3, 5.21f);
std::cout << (*a);
}





Of course, this ignores the question of why you actually need a factory function in the first place.

Share this post


Link to post
Share on other sites
Pure syntactic evil, but it should be exactly what you were looking for. Note the 'reference to pointer' in the function declaration (where I have commented):
#include <iostream>

class A
{
public:
float f;

A() : f(5.0) {}
};

void BuildA(A* &a) // reference to pointer to A
{
a = new A();
}

int main(int argc, char *argv)
{
A *a = 0;

BuildA(a);

std::cout << '<' << a << "> : " << a->f << std::endl;
}

Slightly off topic, but can anyone enlighten me as to why 'NULL' is a define (which must be included from a system header), rather than a keyword (under libstdc++)? Is it actually a keyword in C++, and if not, why is it used so often?

Share this post


Link to post
Share on other sites
That did exactly what I needed swiftcoder! Thanks a ton!

I'm not sure why NULL is used often but it should become a keyword like "true" and "false" became it's own type after so many uses like this:


/*Before "bool" was invented*/
#define TRUE 1
#define FALSE 0



But if it became it's own type, what would it be? An integer? Maybe something different?

Anyway, thanks for all of your help everyone!

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!