using new in functions, pointers passed still NULL after called?!

I

_ivan

Hello,

So I think the answer to this question is going to be rather simple.

Basically I have a pointer to a class, and I want to have a function
to call to initialize it.

After this function is called, the pointer is still NULL.

Here is a simplified version, but basically the same:

void main(){
MyClass *ptr = NULL;
createMyClass(ptr);
}

createMyClass(MyClass *ptrToClass){
ptrToClass= new MyClass();
}

Any reason why ptr would still be NULL after createMyClass is called?

Thanks,
_ivan
 
I

Ivan Vecerina

Hi Ivan,

| void main(){
| MyClass *ptr = NULL;
| createMyClass(ptr);
| }
|
| createMyClass(MyClass *ptrToClass){
| ptrToClass= new MyClass();
| }
|
| Any reason why ptr would still be NULL after createMyClass is called?

By default in C/C++, function parameters *copies* of values
that are passed.

In the code above, changing the value of 'ptrToClass' will
not affect 'ptr' in main.

A way to achieve what you expect is to pass the parameter
by reference. What you need to do is change the function's
signature as follows:
createMyClass( MyClass*& ptrToClass )

This will behave as you expect...


hth
 
M

Mike Wahler

_ivan said:
Hello,

So I think the answer to this question is going to be rather simple.

Basically I have a pointer to a class, and I want to have a function
to call to initialize it.

A class type object should be initialized with a
constructor, not by calling a function after the fact.
After this function is called, the pointer is still NULL.

Here is a simplified version, but basically the same:

void main(){

int main() {
MyClass *ptr = NULL;
createMyClass(ptr);
}

createMyClass(MyClass *ptrToClass){
ptrToClass= new MyClass();
}

Any reason why ptr would still be NULL after createMyClass is called?

Yes. Look up 'pass by value' vs. 'pass by reference'.

#include <iostream>

class MyClass {};

void createMyClass(MyClass *&ptrToClass)
{
ptrToClass = new MyClass;
}

int main()
{
MyClass *ptr = 0;
std::cout << "Before: " << ptr << '\n';
createMyClass(ptr);
std::cout << "After : " << ptr << '\n';
delete ptr;
return 0;
}

But why not just create the object directly in main(),
and don't fool with 'new'.?

int main()
{
MyClass mc;
return 0;
}

Or if you must use 'new' for whatever reason, why not
just call it directly instead of making a separate
function?

int main()
{
MyClass *ptr = new MyClass;
/* etc */
delete ptr;
return 0;
}

-Mike
 
R

Ron Natalie

Ivan Vecerina said:
By default in C/C++, function parameters *copies* of values
that are passed.
With the exception of the apparent (but not in reality) passing of arrays.
 
H

Hafiz Abid Qadeer

Mike Wahler said:
A class type object should be initialized with a
constructor, not by calling a function after the fact.


int main() {


Yes. Look up 'pass by value' vs. 'pass by reference'.

#include <iostream>

class MyClass {};

void createMyClass(MyClass *&ptrToClass)
{
ptrToClass = new MyClass;
}

int main()
{
MyClass *ptr = 0;
std::cout << "Before: " << ptr << '\n';
createMyClass(ptr);
std::cout << "After : " << ptr << '\n';
delete ptr;
return 0;
}

But why not just create the object directly in main(),
and don't fool with 'new'.?

int main()
{
MyClass mc;
return 0;
}

Or if you must use 'new' for whatever reason, why not
just call it directly instead of making a separate
function?

int main()
{
MyClass *ptr = new MyClass;
/* etc */
delete ptr;
return 0;
}

-Mike


You can also use the pointer to pointer like this
#include <iostream>
class MyClass
{
};



void createMyClass(MyClass **ptrToClass)
{
*ptrToClass= new MyClass();
std::cout<<"Value in Function:"<<*ptrToClass<<std::endl;
}


int main()
{
MyClass *ptr = NULL;
createMyClass(&ptr);
std::cout<<"Value in main:"<<ptr<<std::endl;
return 0;
}
in this way change reflects in the actual pointer when you return from the function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top