*pointer issue

Nov 23, 2012 at 3:07pm
Hi,
I have following issue, I have pointer object as member variable and I want to return just an object from one function. do I need to rewrite the function or anything else?

//role.h
class Role{
int count;
Manage man; // another class's object
};


//analyze.h
Role *role;
public:
Role getRole() const;

//analyze.cpp

..
..
role = new Role("agent");
..
..
Role Analyze::getRole() const{
if(role)
return ######; ////// I want to return the role object but how to do that as declaration need object but I have *object?
else
return Firm();
}

Nov 23, 2012 at 3:10pm
sry mixed up some stuff

so again:

if you want object but you have *object,
use *object to get object.
Last edited on Nov 23, 2012 at 3:12pm
Nov 23, 2012 at 3:14pm
If I use that it does compiling but while execution its crashing.
Nov 23, 2012 at 3:15pm
closed account (D80DSL3A)
It is just return *role;// dereferencing the pointer gives the object itself

I assume that Firm() also returns a Role. It should
Nov 23, 2012 at 3:43pm
Yes Firm() actually returns object.
I tried using this but no luck...
Fun2code, I think I have some other issue :)
thanks for your help... I was doubting on my dereferencing concept.
Nov 23, 2012 at 3:47pm
If you return an actual object, then you are creating a copy of the object that's being returned. Depending on how your compiler optimises the executable code it creates, there might well be temporary intermediate copies of the object being created too.

I suspect the reason for the crash is something to do with what happens when you create copies of your Role object, or the Man object it contains.

The obvious question is: why do want to return a copy of the object, rather than a pointer to it? What do you want to achieve that you can't achieve using a pointer?
Last edited on Nov 23, 2012 at 4:34pm
Topic archived. No new replies allowed.