Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Tom78

classes in c++

This topic is 5560 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 a problem with c++. I have two classes. One console class that outputs text on the screen and handles input. The other is a class for OpenGL objects. they both run very well, but now my problem : In Main i create an object for the console class. Then i create an object for the OGLobject class. now if i want to pass text (arrays of chars) from the GLObject class to the console class i have to pass a pointer from console class to main and then from the main class to the OGLobject class, so that the OGLobject class can fill the array that was allocated for the console. Hope you got it. Now my question : Is there any other possibility to pass the text, beside passing the Pointer from here to there. Of course i could include the console class in the oglobject class, but if use 1000 Objects, i would also have 1000 ConsoleObjects. This sucks. Can i refer from this 1000 OGLObject classes to ONE console class without this pointer passing stuff ? Hope you got my problem. cu Tom

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Well if you only need one console class, then you could hide the constructors(make them private) and just implement a public static function to generate an instance of the console class


//not checked the code via compilation

class Console
{
public:
static Console& theConsole()const
{
static Console Cons;
return Cons;
}
void printtexttoconsole(const char* text)const
{
std::cout<<text;
}
private:
Console();
Console(const Console&);
};


Since the only way to construct the console is through the "theConsole" member function, and since that can only create one copy of it(which is created when it is first called), you can only have one console object.

To call the printtexttoconsole function you wouold simply use the following

Console::theConsole().printtexttoconsole(array in here);

Which you could call from anywhere, including your other class functions.

ope that helps

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!