Sign in to follow this  

1 object and 2 class in C++

This topic is 2574 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have an object 'x':
x = new object();




and use it within my class where the program works,
But there comes a certain point I need to use another class and continue to use my object 'x', thought of using Singleton, but in my case I can't.

class ClassX
{
public:
ClassX();
~ClassX();

//...


private:
object* x;
}




class ClassY
{
public:
ClassY();
~ClassY();

//...


private:
// I can not create another object* x
// I need to continue using the same object that was the classX
}




I thought the classY create a parameter that receives the object, would work? ie everything that has been created within the object x in classX continue working?
something like:
class ClassY
{
public:
ClassY(object *obj_x);
~ClassY();

private:
//...
}




please help-me :(

Share this post


Link to post
Share on other sites
Hey there,

Do you need to do this because class Y has different functionality than class X?

If so, have you thought about combining class X and class Y into a single class?

Or perhaps using class inheritance so that class Y derives from X, and when you create an object you create an object of type Y, use the type X's interface til you need type Y's then use that?

Or possibly having an object Y within object X?

Its really hard to guess what specifically you are trying to accomplish with all of this... could you give some more info about what those classes actually do and why you need to switch from using class X to class Y?

Share this post


Link to post
Share on other sites
I was not thinking about inheritance :P

I need to change class, since I'm using Qt and not found another way to close the current window and open a new window without making the exchange of classes (each window is a class)

look:
class DedicatedServer : public QMainWindow
{
Q_OBJECT

public:
DedicatedServer(QWidget *parent = 0, Qt::WFlags flags = 0);
~DedicatedServer();

private:
Ui::DedicatedServerClass ui;
bool password;
server_control* s_control;
Server* server; //this is my "object x"

public slots:
void CreateServer();
void Cancel();

void Password_Yes();
void Password_No();
};


class server_control : public QWidget
{
Q_OBJECT

public:
server_control(QWidget *parent = 0);
~server_control();

private:
Ui::server_control ui;

char *text_map;
std::ofstream m_stream;
};


within the object X is created an instance of RakNet and needed to continue using that instantiates the object X in another window

Share this post


Link to post
Share on other sites
I'd say what you suggested should work, e.g. give the pointer to x as a parameter for the constructor of class Y. Just make sure that class X doesn't delete your object when it's closing.

Unless I'm misunderstanding your question.

Share this post


Link to post
Share on other sites
Quote:
Original post by sjaakiejj
I'd say what you suggested should work, e.g. give the pointer to x as a parameter for the constructor of class Y. Just make sure that class X doesn't delete your object when it's closing.

Unless I'm misunderstanding your question.


worked perfectly, thanks friend :D

Share this post


Link to post
Share on other sites
"I need to continue using the same object that was the classX"

you're saying you "need" to have the object, did you consider using a reference instead? Now you can just create a ClassY while passing a NULL object, does this break stuff?

Share this post


Link to post
Share on other sites

This topic is 2574 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this