function member in class , to return object

i need to write function in class com which create new object of class rev
then return it back .

so when i can use any object of class com to create new object of class rev

is it possible ?how ?
here are my classes down
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef _CCOMMITTEE_H_
#define _CCOMMITTEE_H_
#include <iostream>
#include <string>
#include "User.h"
#include "Author.h"
#include"Proposal.h"
#include"Reviewer.h"
#include"Assignedproposal.h"
using namespace std;

class Ccommittee
{
public:
	static int Admin;
	Reviewer* reviewer;
	Proposal* proposal;
	User* user;

public:
	Ccommittee();
    void Assignedproposal(Proposal& , Reviewer& );
	void Creatreviewer(User&);////here ????????

};
#endif 


cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include"Ccommittee.h"
Ccommittee::Ccommittee()
{
	Admin=2012;
}

int Ccommittee::Admin = 2012;
void Ccommittee::Assignedproposal(Proposal &newProposal ,Reviewer &newreviewer)
{
	proposal = &newProposal;
	reviewer = &newreviewer;
}
void Ccommittee::Creatreviewer(User &newuser)
{
	user=&newuser;
	Reviewer R(newuser);
	cout<<" creat R in function\n"<<R.R_ID<<"inside cpp"<<endl;//this work and it create the new object ,
// but my problem that i cant get the object in main 
//, so i need the function to return object , how can i try to change void into Reviewer but it didnt work , i think there is special way 
//, which i search about it but i didnt find .
}
Last edited on
help pls ,
Of course it's possible.

Just create a member function of com into which you would dynamically allocate rev objects on heap (using new).

Keep track of the pointer returned since you should delete them afterward (when rev goes out of scope or com or rev needed to be deleted etc). You can do this by adding them to a vector for example.

Make sure you delete the object allocated before destroying com (into it's destructor delete first all rev objects)
actually i have another class , i can create new object , and it already work , but the problem that its destructed and i couldnt get it in the main program .
this is the class for the object which i create
rev:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef _REVIEWER_H_
#define _REVIEWER_H_
#include <iostream>
#include <string>
#include "User.h"
#include "Proposal.h"


using namespace std;
class Reviewer
{
public:
	static int nextID;
	int R_ID;
	User* user;
    Reviewer* reviewer;
	Proposal* proposal;
public:
	Reviewer();
    Reviewer( User&);
	Reviewer(Reviewer&,Proposal&);
    void acceptReview(Proposal&);
	void rejectReview(Proposal&);
};

#endif 

and rev.cpp is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include"Reviewer.h"
Reviewer::Reviewer()
{
}
Reviewer::Reviewer( User &userNew)
{
	R_ID= nextID;
	user= &userNew;
	nextID++;
	cout<< R_ID << " inside rev.cpp\n";
}
Reviewer::Reviewer(Reviewer &reviewerNew , Proposal &proposalNew)
{
	R_ID= nextID;
	reviewer= &reviewerNew;
	proposal=&proposalNew;
	nextID++;
	cout<<"creat Reviewer\n";

}
void Reviewer::acceptReview(Proposal& proposalNew)
{
	proposal= &proposalNew;
}
void Reviewer::rejectReview(Proposal& proposalNew)
{
	proposal=&proposalNew;
}
int Reviewer::nextID = 200;



when i use the function in main class
1
2
3
User U7(50,"dr.ayman","malaysia");
				C.Creatreviewer(U7);
				cout<<C.reviewer->R_ID<<" in main class .cpp \n";


the result as below:

203 inside rev.cpp
create R in function
203 inside com cpp


the last cout statement in main class cant be execute , i think coz the object destructed , how can i keep this object until it reach the main class , then i will push it in vector .
"Ccommittee::reviewer" should hold a copy of the Reviewer object that was created in the member function "Creatreviewer(...)" instead of a reference to it.
thank u all
eypros , computergeek01

i deleted creatviewer function , and i use the object member directly from the main menu as below":
C.reviewer= new Reviewer(U7);
it works correctly now .
thank u cplupsplus forum members .
¿What was wrong with returning the object?
1
2
3
4
5
Reviewer Ccommittee::Creatreviewer(User &newuser)
{
	user=&newuser;
	return Reviewer R(newuser);
}


1
2
3
User U7(50,"dr.ayman","malaysia");
C.Creatreviewer(U7); //as a side efect C.user will be changed. Nothing more
cout<<C.reviewer->R_ID<<" in main class .cpp \n";


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Reviewer
{
public:
	static int nextID;
	int R_ID;
	User* user;
	Reviewer* reviewer; //please explain this link
	Proposal* proposal;
public:
	Reviewer();
	Reviewer( User&); //there is no initialized here
	Reviewer(Reviewer&,Proposal&);
	void acceptReview(Proposal&);
	void rejectReview(Proposal&);
};

You have variables unitialized (acording to which constructor you use).
Topic archived. No new replies allowed.