3 Replies - 592 Views - Last Post: 19 January 2009 - 08:48 AM Rate Topic: -----

#1 jorijo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 16-August 06

Where to declare pointers

Posted 17 January 2009 - 05:05 AM

Hi!

I have a class object Interface that only has methods to receive the input from the keyboard and this object is created in main function and I want to use his methods in different objects... my question is where to declare the pointers to the object Interface and the easy way to call the methods whenever I want?

#include "Inicia.h"

int main(){

	Interface interface;

		Inicia i;

	return 0;
}



one method of interface
string Interface::read_string( const string &mensagem )
{
	string temp;

	do 
	{
		cout << mensagem;
		cin >> temp;
	} while( temp.size( ) == 0 );
	return temp;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Where to declare pointers

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1156
  • View blog
  • Posts: 7,171
  • Joined: 07-September 06

Re: Where to declare pointers

Posted 17 January 2009 - 11:14 AM

Just change:
Interface interface;



To something like so:
Interface* interface = new Interface(); // this will require a constructor or default constructor for the Interface class



As for passing the instance of the interface, that can be done like so:
void test(Interface* a){
  //Do something with interface (which is called "a" in this function)
}
int main(){
	Interface* interface = new Interface();
	test(interface);
	return 0;
}


You call object pointer functions like so:

Interface interface = new Interface(); 
interface->read_string(/*correct parameters here*/);



Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#3 jorijo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 16-August 06

Re: Where to declare pointers

Posted 19 January 2009 - 06:22 AM

But anytime I need a function from interface object I need to declare the pointer where I'm using it right?
Was This Post Helpful? 0
  • +
  • -

#4 manzoor  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 468
  • Joined: 07-August 07

Re: Where to declare pointers

Posted 19 January 2009 - 08:48 AM

Not necessarily a pointer though, you can create objects of the class too and call their member functions/methods.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1