3 Replies - 672 Views - Last Post: 07 October 2009 - 12:58 PM Rate Topic: -----

#1 BlackPhoenix  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 152
  • Joined: 11-July 09

How can I use a function of a class in any other class?

Posted 07 October 2009 - 12:29 PM

How can I use a function of a class in any other class without creating an object and passing in an instance of that object to EACH FUNCTION in EACH CLASS?

I have a class called Debug

Debug.h
#ifndef _DEBUG_H_
#define _DEBUG_H_

#include <iostream>
#include <fstream>
using namespace std;

class Debug
{
	public:
		
		Debug(); //constructor

		void Write(char* message); //write to file
};

#endif



Debug.cpp
#include "Debug.h"

Debug::Debug()
{
}

void Debug::Write(char *message)
{
	ofstream debug;
	debug.open("Debug.txt", ios::out | ios::app);
	debug << message << endl;
	debug.close();
}




In my Main.cpp file, I create an object of class Engine, which has various functions for use during my program.

I want to be able to call the Write function of Debug from within any other class. An example would be this function from class Engine

bool Engine::Init()
{
	if (SDL_Init(SDL_INIT_EVERYTHING) < 0 )
	{
		return 1;
	}
	
	SDL_WM_SetCaption("Game", NULL);

	if ((screen = SDL_SetVideoMode(640, 480, 32, SDL_DOUBLEBUF | SDL_HWSURFACE)) == NULL )
	{
		return 1;
	}

	running = true;

	[b][color=#CC0000]Debug::Write("Game Initialized Successfully");[/color][/b]
	return 0;
}


This is illegal, because I am supposed to be calling Debug by using an object of class Debug.

I'd rather not do this. How can I make this work so that I have a log system working in my soon-to-be large program?

Is This A Good Question/Topic? 0
  • +

Replies To: How can I use a function of a class in any other class?

#2 BlackPhoenix  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 152
  • Joined: 11-July 09

Re: How can I use a function of a class in any other class?

Posted 07 October 2009 - 12:37 PM

View PostBlackPhoenix, on 7 Oct, 2009 - 11:29 AM, said:

How can I use a function of a class in any other class without creating an object and passing in an instance of that object to EACH FUNCTION in EACH CLASS?

I have a class called Debug

Debug.h
#ifndef _DEBUG_H_
#define _DEBUG_H_

#include <iostream>
#include <fstream>
using namespace std;

class Debug
{
	public:
		
		Debug(); //constructor

		void Write(char* message); //write to file
};

#endif



Debug.cpp
#include "Debug.h"

Debug::Debug()
{
}

void Debug::Write(char *message)
{
	ofstream debug;
	debug.open("Debug.txt", ios::out | ios::app);
	debug << message << endl;
	debug.close();
}




In my Main.cpp file, I create an object of class Engine, which has various functions for use during my program.

I want to be able to call the Write function of Debug from within any other class. An example would be this function from class Engine

bool Engine::Init()
{
	if (SDL_Init(SDL_INIT_EVERYTHING) < 0 )
	{
		return 1;
	}
	
	SDL_WM_SetCaption("Game", NULL);

	if ((screen = SDL_SetVideoMode(640, 480, 32, SDL_DOUBLEBUF | SDL_HWSURFACE)) == NULL )
	{
		return 1;
	}

	running = true;

	[b][color=#CC0000]Debug::Write("Game Initialized Successfully");[/color][/b]
	return 0;
}


This is illegal, because I am supposed to be calling Debug by using an object of class Debug.

I'd rather not do this. How can I make this work so that I have a log system working in my soon-to-be large program?



PROBLEM SOLVED FOR THOSE WHO WISH TO LEARN:


All I had to do was change the function Write() of class Debug to a static member function, which allows me to call it from another class by using the syntax

 Debug::Write("my message");


Note that this will not work until you do

#include "Debug.h"


in each class that is going to use Debug
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: How can I use a function of a class in any other class?

Posted 07 October 2009 - 12:37 PM

You could make the class static (or a static object of it for the entire program to access rather), then you can reference it where ever you want.


edit: haha you ninja'ed me

This post has been edited by KYA: 07 October 2009 - 12:38 PM

Was This Post Helpful? 0
  • +
  • -

#4 BlackPhoenix  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 152
  • Joined: 11-July 09

Re: How can I use a function of a class in any other class?

Posted 07 October 2009 - 12:58 PM

View PostKYA, on 7 Oct, 2009 - 11:37 AM, said:

You could make the class static (or a static object of it for the entire program to access rather), then you can reference it where ever you want.


edit: haha you ninja'ed me



"Here at DIC we be post-stealin ninjas!" :snap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1