I was wondering what exactly is happening if you declare some object and pass it directly to a function such as funct(Object& obj).
If I were to pass my object to a function with the prototype funct(Object obj)
I would really be passing a copy of the object, and therefore the operations would not be applied to the original, is that correct?
Also, if I want to change some value of a class member through a function defined within the objects class, is there a way to use that function on a pointer? Thanks for the help.
Objects and Pointers
Started by Wade Berkn, Jun 09 2007 06:42 AM
5 replies to this topic
#1
Posted 09 June 2007 - 06:42 AM
#2
Posted 09 June 2007 - 02:46 PM
Quote
I was wondering what exactly is happening if you declare some object and pass it directly to a function such as funct(Object& obj).
If I were to pass my object to a function with the prototype funct(Object obj)
I would really be passing a copy of the object, and therefore the operations would not be applied to the original, is that correct?
If I were to pass my object to a function with the prototype funct(Object obj)
I would really be passing a copy of the object, and therefore the operations would not be applied to the original, is that correct?
Yup that's correct. The compiler in the former case just passes a reference to the object (think of references as some kind of pointers !), while in the latter, a copy of the object is created on the application stack, and the function uses that copy. Thus the original object remains unchanged.
The copy is created using the object's copy constructor - be careful here: If no copy constructor is specified, the compiler automatically creates a shallow copy constructor, i.e. one which copies object pointers by value, not by actually allocating new storage. This can lead to quite ugly bugs for more complicated objects.
Quote
Also, if I want to change some value of a class member through a function defined within the objects class, is there a way to use that function on a pointer? Thanks for the help.
What do you mean by that ?
class myclass
{
int mymember;
void ChangeMyMember(int newmymember) { mymember = newmymember }
};
myclass *myinstance = new myclass();
myclass->ChangeMyMember(5);
You can use the -> operator for dereferencing a pointer to a struct or class, no matter what you actually access within the instance.
Hope this helps.
Cheers,
- Wernaeh
Some call me mathematician, some just call me computer guy. Yet, I prefer the term professional weirdo :)
#3
Posted 11 June 2007 - 06:20 PM
Quote
I was wondering what exactly is happening if you declare some object and pass it directly to a function such as funct(Object& obj).
When you pass an object as a to a function that wants a reference it works in the same way as using references normally:
Object a; Object &b = a; // b is now a synonym for a a.x = 1; b.x = 1; // This also changes a
So if instead b was the parameter of a function, everything would still be the same. Any action on b affects a.
You shouldn't pass objects by value into a function since it can be troublesome and also for classes with any more than a few bytes of data it will be slower than passing a pointer or reference. The only time you should pass by value is when you absolutely must, although I can't think of any scenarios where you would.
To access member functions of pointers you just use:
foo *ptr = new foo(); ptr->method(); // Element selection by pointer (*ptr).method(); // This is identical, although the former is preferable
#4
Posted 11 June 2007 - 08:21 PM
poita said:
and also for classes with any more than a few bytes of data it will be slower than passing a pointer or reference.
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.
-
Currently working on: the 3D engine for Tomb Raider.
#5
Posted 12 June 2007 - 12:21 PM
Could you elaborate on that oisyn? I'm not quite sure I'm following you.
#6
Posted 12 June 2007 - 12:49 PM
well, aliasing refers to the fact that different objects can point to the same data in memory. The compiler usually doesn't know whether two references or pointers point to the same memory or not, so if you change the memory via one pointer, it can't assume the value pointed to by the other hasn't changed.
suppose this simple example
The compiler can't assume that n doesn't change - perhaps bar() directly or indirectly has access to the int 'n' refers to and can even change it because it's view of the int referred to is not const. If you passed 'n' by value, this wouldn't be a problem as then the variable is local to the function, and as it isn't passed to bar(), it can assume it never changes.
Another example:
For these reasons, C99 introduced the 'restrict' keyword, which is a hint for the compiler to make clear that the object pointed to is not aliased in any way. Therefore it can assume that any changes to that object does not change any other object, nor that changes to any other object (or function calls from within the current function) change the object pointed to. Some compilers do support this C99 feature in C++, and there have been suggestions within the C++ committee to include it as an official C++ keyword.
suppose this simple example
void foo(const int & n)
{
for (int i = 0; i < n; i++)
bar();
}
The compiler can't assume that n doesn't change - perhaps bar() directly or indirectly has access to the int 'n' refers to and can even change it because it's view of the int referred to is not const. If you passed 'n' by value, this wouldn't be a problem as then the variable is local to the function, and as it isn't passed to bar(), it can assume it never changes.
Another example:
void foo(int & a, int & :P
{
a = 0;
b = 5;
++a;
}
You might be tempted to think a compiler would optimize the operations on 'a' to simply 'a = 1'. However, if a and b point to the same variable, it's value would be 6 rather than 1 after this function has exited. Of course, passing by value is not an option here, but it is important to know that such a function will have to reread 'a' or 'b' from memory every time you change 'b' or 'a', respectively.For these reasons, C99 introduced the 'restrict' keyword, which is a hint for the compiler to make clear that the object pointed to is not aliased in any way. Therefore it can assume that any changes to that object does not change any other object, nor that changes to any other object (or function calls from within the current function) change the object pointed to. Some compilers do support this C99 feature in C++, and there have been suggestions within the C++ committee to include it as an official C++ keyword.
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.
-
Currently working on: the 3D engine for Tomb Raider.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users












