Hi, I am trying to create an object inside a function using new operator and the newly created object is returned as a parameter of the function. Unfortunately, after calling the function, the pointer does not point to the newly created object.
void setUp()
{
TestClass* testClass = NULL;
createTestClass(testClass);
// testClass object does not point to newly
created object..
cout << testClass->objectName << endl;
}
};
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I dont understand where is the question of C++ comes into picture. I know a bit of C++ and acc to my knowledge,
void createTestClass(TestClass* testClass)
{
testClass = new TestClass("Object1");
}
must initialize the testClass pointer with a newly created object which can be used by the calling function. This is in C++. Unfortunately, i dont see this behaviour when calling the same function via CppUnit.
Anyways, thanks for your answer.
Could anyone kindly let me know whether passing a pointer to a function as parameter and getting an newly initialized object behaves as it does in normal c++?
Thanks once again.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You don't leave the domain of "normal c++" when you use cppunit. Everything behaves the same way with and without cppunit. Cppunit is just a library, it doesn't change the language.
Try to call your original method yourself and you will see exactly the same behaviour. You should pass a pointer to pointer or a reference to pointer in order to return a pointer from a function.
Pointers to functions work the same way like any other pointers. But you should be very careful because pointers to non-static member functions aren't just pointers to plain functions, and pointers to virtual methods behave differently from pointers to non-virtual member functions.
I'd suggest you to use virtual methods instead of pointers to methods.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If you want to understand the details behind this, you should learn about the difference between pass-by-value and pass-by-reference. Also, as jdubchak pointed out, your original function does not actually return anything (i.e. the return type is void). Personally, I like his solution of returning a newly created object. This seems much cleaner than trying to use pass-by-reference as suggested by nimnul.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I just wanted to fix an error in his code and to make his calling code working without a need to change it. I think sk_krishnan stripped the actual code to create a minimal example, and in the complete code the return value is already used for another purpose.
If createTestClass() is a classic polymorphic factory method, then "return new()" would be the cleanest implementation.
To suggest the best implementation, we need more information. For example, if run time polymorphism isn't needed and the compiler supports Return Value Optimization (GCC4 and MSVC2005 do), it would be better not to use heap at all and just to return TestClass().
Personally, if MemoryClassesUnitTest is a generic test for a group of classes with a common superclass, I'd prefer to have a template MemoryClassesUnitTest<CLASS_UNDER_TEST>. It will make testing less intrusive. For example, the factory methods won't be required and the methods of tested classes won't have to be virtual.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi, I am trying to create an object inside a function using new operator and the newly created object is returned as a parameter of the function. Unfortunately, after calling the function, the pointer does not point to the newly created object.
Any help is higly appreciated.
Thanks,
An example:
class TestClass
{
public:
TestClass(string objectName)
{
this->objectName = objectName;
}
string objectName;
};
class MemoryClassesUnitTest : public
CppUnit::TestCase
{
CPPUNIT_TEST_SUITE( MemoryClassesUnitTest );
CPPUNIT_TEST_SUITE_END();
void createTestClass(TestClass* testClass)
{
testClass = new TestClass("Object1");
}
public:
MemoryClassesUnitTest();
virtual ~MemoryClassesUnitTest();
void setUp()
{
TestClass* testClass = NULL;
createTestClass(testClass);
// testClass object does not point to newly
created object..
cout << testClass->objectName << endl;
}
};
It's a basic C++ question, and not a CppUnit question. So the short answer is "go and learn C++".
One of the possible solutions is to use a reference to a pointer instead of just a pointer:
void createTestClass(TestClass*& testClass)
instead of just
void createTestClass(TestClass* testClass)
I dont understand where is the question of C++ comes into picture. I know a bit of C++ and acc to my knowledge,
void createTestClass(TestClass* testClass)
{
testClass = new TestClass("Object1");
}
must initialize the testClass pointer with a newly created object which can be used by the calling function. This is in C++. Unfortunately, i dont see this behaviour when calling the same function via CppUnit.
Anyways, thanks for your answer.
Could anyone kindly let me know whether passing a pointer to a function as parameter and getting an newly initialized object behaves as it does in normal c++?
Thanks once again.
You don't leave the domain of "normal c++" when you use cppunit. Everything behaves the same way with and without cppunit. Cppunit is just a library, it doesn't change the language.
Try to call your original method yourself and you will see exactly the same behaviour. You should pass a pointer to pointer or a reference to pointer in order to return a pointer from a function.
Pointers to functions work the same way like any other pointers. But you should be very careful because pointers to non-static member functions aren't just pointers to plain functions, and pointers to virtual methods behave differently from pointers to non-virtual member functions.
I'd suggest you to use virtual methods instead of pointers to methods.
Why not simply take the more common approach of returning a properly created and initialized type?
Rather than what you have, why not do it this way:
TestClass* createTestClass()
{
return new TestClass("Object1");
}
Then your call would be:
TestClass* tc = createTestClass();
// ... do anything else with tc here...
HTH,
John
Hi,
My apologies to all. I somehow got confused with the difference in passing pointers with reference and values.
Thanks to "Mr. Andrey Melnikov" to pointing it out. I initially thought i was doing right but then recognized the error.
Thanks once again.
Krishnan
If you want to understand the details behind this, you should learn about the difference between pass-by-value and pass-by-reference. Also, as jdubchak pointed out, your original function does not actually return anything (i.e. the return type is void). Personally, I like his solution of returning a newly created object. This seems much cleaner than trying to use pass-by-reference as suggested by nimnul.
I just wanted to fix an error in his code and to make his calling code working without a need to change it. I think sk_krishnan stripped the actual code to create a minimal example, and in the complete code the return value is already used for another purpose.
If createTestClass() is a classic polymorphic factory method, then "return new()" would be the cleanest implementation.
To suggest the best implementation, we need more information. For example, if run time polymorphism isn't needed and the compiler supports Return Value Optimization (GCC4 and MSVC2005 do), it would be better not to use heap at all and just to return TestClass().
Personally, if MemoryClassesUnitTest is a generic test for a group of classes with a common superclass, I'd prefer to have a template MemoryClassesUnitTest<CLASS_UNDER_TEST>. It will make testing less intrusive. For example, the factory methods won't be required and the methods of tested classes won't have to be virtual.