Passing Objects

This is quite hard for me to understand

Say that there is a class A. And I have created an object as " A *objA = new A"
So this creates a pointer to objA of type A, right?

Okay so pointers are variables that store the address of a data type which the pointer is defined. And the address of the object could be taken as "&objA".

Now I want to pass this object to a function

so I checked and did the following two.

1) void function(A *objA); -> function call : function(*objA);
2) void function_1(A &objA); -> function call : function(objA);

I need to know what is the difference between the two?
Say that there is a class A. And I have created an object as " A *objA = new A"
So this creates a pointer to objA of type A, right?

objA is a pointer to an object of type A.


1) void function(A *objA); -> function call : function(*objA);
2) void function_1(A &objA); -> function call : function(objA);

You must have swapped the function call order. Here is how it should be

1) void function(A *objA); -> function call : function(objA);
2) void function_1(A &objA); -> function call : function(*objA);

The first takes a pointer as argument and the second takes a reference as argument.
Last edited on
Dammit, Peter, you're quick today.

I was just about to post about the parameters being passed in being wrong. Thought I'd best refresh first and, sure enough, there's a post!

Functionally, they should behave exactly the same.
Thanks for the comments guys...one other thing I noticed the similarity in functionality but what bothers me is there any performance difference??
I'd asked about this earlier as well. There is no performance difference.

The real only difference is that method 2 is not supported by C.
But there is a difference in the creation
1
2
A *ptr = new A;
A obj;
Bjarne Stroustrup wrote:
Code that creates an object using new and then deletes it at the end of the same scope is ugly, error-prone, and inefficient.
Alright.... I am dealing with a lot of data. The object created has two maps (STL).

So passing such large objects to each and every method may result in inefficiency. That's why I asked this question. If there is any other thoughts on this please let me know.

Thanks a lot everyone.
Topic archived. No new replies allowed.