Passing Objects

Mar 23, 2012 at 12:15pm
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?
Mar 23, 2012 at 12:25pm
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 Mar 23, 2012 at 12:26pm
Mar 23, 2012 at 12:28pm
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.
Mar 24, 2012 at 12:12pm
Thanks for the comments guys...one other thing I noticed the similarity in functionality but what bothers me is there any performance difference??
Mar 24, 2012 at 1:36pm
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.
Mar 24, 2012 at 2:24pm
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.
Mar 29, 2012 at 5:50am
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.