*pointer issue

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();
}

sry mixed up some stuff

so again:

if you want object but you have *object,
use *object to get object.
Last edited on
If I use that it does compiling but while execution its crashing.
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
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.
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
Topic archived. No new replies allowed.