Originally Posted by
CornedBee
Don't use relational operators on pointers. Use !=.
But that's probably not the problem. Code?
By the way, in Java, all variables of object type are pointers (Java calls them references). In C++, variables are objects unless you explicitly make them pointers or references.
Code:
class Test
{
private:
std::string name;
Test* test;
public:
Test(std::string n)
{
Test( n, NULL );
}
Test(std::string n, Test* other): name( n )
{
test = other;
//This says it is zero when I call other constructor, so it's right
if ( test == 0 ) printf( "null in constructor" );
}
const char* print()
{
//This does NOT print out, somehow it's no longer 0
if ( test == 0 ) printf( "null in print" );
return n;
}
}
int main()
{
Test t( "name" );
//This says it's not null
t.print();
return 0;
}
EDIT: OK, why is this?
Code:
If I change the first constructor to this:
Test(std::string n) :name( n );
{
test = NULL;
}
then it works!! Why? What's the difference?