1 object and 2 class in C++

Started by
5 comments, last by Murdocki 13 years, 4 months ago
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 :(
http://mateusvitali.wordpress.com/
Advertisement
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?
I cannot understand what you are asking, maybe someone else can. Please, restate your problem. There doesn't appear to be anything wrong with the code that you have posted.
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.
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_OBJECTpublic:	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_OBJECTpublic:	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
http://mateusvitali.wordpress.com/
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.
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
http://mateusvitali.wordpress.com/
"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?

This topic is closed to new replies.

Advertisement