By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

is object name a pointer?

P: 2
I'm a beginner in C++ and i'm confused about object name.
let's pretend that there's code like this:

Expand|Select|Wrap|Line Numbers
  1. class blaclass{
  2. public:
  3. void dosomething(){cout << "doing something"};
  4. }
and i want to make a function that takes blaclass object as a parameter and returns blaclass object.and i don't know which is the right way to do it.

Expand|Select|Wrap|Line Numbers
  1. blaclass veryrandomfunction(blaclass a){
  2. a.dosomething()
  3. blaclass blaobj2;
  4. return blaobj2;
  5. }

Expand|Select|Wrap|Line Numbers
  1. blaclass& veryrandomfunction(blaclass& a){
  2. a.dosomething()
  3. blaclass blaobj2;
  4. return blaobj2;
  5. }
is way 1 right or is way 2 right? in other terms, is object name(identifier) a pointer?
Feb 24 '07 #1
Share this Question
Share on Google+
2 Replies

P: 2
problem solved
i didn't know the c++ feature that you can pass '&' after the parameter type to use it as a reference.
if what i remember is right, C didn't have that
yea, C++ is kewl.
Feb 24 '07 #2

Expert 2.5K+
P: 3,652
Just for the sake of my own satisfaction...

I'm fairly sure that declaring the object inside a function is not the right way to go. It becomes a local variable that (I think?) will become deallocated after the function ends, so while you may be returning the address of the object, that address will be meaningless after the function, as the memory there is deallocated.

Instead, you should use a pointer to create an object in memory that will NOT be deallocated after the function - you can then return the pointer, or the value of the pointer (i.e. the object), and the object will still exist.
Feb 25 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.