GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Jul-2006, 02:52
Ahas Ahas is offline
New Member
 
Join Date: Jul 2006
Posts: 2
Ahas is on a distinguished road

Passing an object as argument to a function?


Hi,

I'm new to programming. I'm learning Cppand am coding my first program with Qt . I made some Dialog classes and objects from the classes. I then wrote a function for each dialog that will bring the dialog on top and set it as the active window. Up untill now I created a specific function for each dialog. I know it should be possible to do this with just one function though.

This is an example of the functions:
CPP / C++ / C Code:
void MainWindow::showfindDialog()
{
    findDialog->show();
    findDialog->toTop();
    findDialog->setActiveWindow();
}

In this function findDialog is an object from the FindDialog class. I created similar functions for dossierDialog from the DossierDialog class etc...

These functions work, but i need one for each dialog.

I then thought it should be possible to write just one function that can do this for each dialog. So I tried passing the name of the dialog as a string:

CPP / C++ / C Code:
void MainWindow::showDialog(QString dialogname)
{
    dialogname->show();
    dialogname->toTop();
    dialogname->setActiveWindow();
}

This ofcourse didn't work because dialogname is a string and not the object with wich you can do ->show()...

Now in the past I wrote a few scripts for websites in Javascript where I could do something like this: eval(dialogname + "->show()");
This would turn dialogname into the right object.
Is there something similar for Cpp or do I have to pass the object itself as a parameter for the function?
And how would I go about that? Isn't it a problem that the objects are of different classes then?

Thanks,
Ahas
  #2  
Old 21-Jul-2006, 08:45
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 924
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Passing an object as argument to a function?


Quote:
Originally Posted by Ahas
Now in the past I wrote a few scripts for websites in Javascript where I could do something like this: eval(dialogname + "->show()");
This would turn dialogname into the right object.

C++ isn't a scripting language, and something like this would never work; unfortunately. It is strongly typed, like most imperative languages I know, so you need to pass the actual object to your function.

Now, how can you do that? I appologyze for my lack of experience with QT. However, I am almost sure there is a base type all these dialogs have, and you can promote your specific dialogs to that type, provided the base class has the minimum API interface required for your function (meaning, it has show, toTop and setActiveWindow defined).

Here is a simple example of what I mean:

animal.h
CPP / C++ / C Code:
#include <iostream>

class Animal
{
public:
	Animal();
	~Animal();
	
	virtual void Talk() 
	{
		std::cout << "What do you want from me!?" << std::endl;
	}
	virtual void DoSomeCrazzyStuff()
	{
		std::cout << "Give me a break..." << std::endl;
	}
	
};

class Cat : public Animal
{
public:
	void Talk()
	{
		std::cout << "Meow" << std::endl;
	}
	void DoSomeCrazzyStuff()
	{
		std::cout << "Licking my paws..." << std::endl;
	}
};

class Dog : public Animal
{
public:
	void Talk()
	{
		std::cout << "Bow-wow, master" << std::endl;
	}
	void DoSomeCrazzyStuff()
	{
		std::cout << "Playing dead..." << std::endl;
	}
};

main.cpp
CPP / C++ / C Code:
#include "animal.h"

void Execute(Animal * pAnimal)
{
	pAnimal->Talk();
	pAnimal->DoSomeCrazzyStuff();
}

void ExecuteByRef(Animal & rAnimal)
{
	rAnimal.Talk();
	rAnimal.DoSomeCrazzyStuff();
}

int main()
{
	Dog dog;
	Cat cat;
	/*
	Execute(& dog);
	Execute(& cat);
	*/
	ExecuteByRef(dog);
	ExecuteByRef(cat);
	return 0;
}

I am hoping there is a class like QDialog all your other Dialogs are derived from; you could use this as the parameter to the function, and call the show and the other functions like I did with to produce my "Meows".

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
Last edited by LuciWiz : 25-Jul-2006 at 09:19.
  #3  
Old 25-Jul-2006, 09:11
Ahas Ahas is offline
New Member
 
Join Date: Jul 2006
Posts: 2
Ahas is on a distinguished road

Re: Passing an object as argument to a function?


Thanks,

That's the solution I'm looking for.

All dialogs are indeed derived of a QDialog class so this should work.

I kind of expected I couldn't do it with the string.

This will make things a lot easier for me.

Thanks again,
Ahas
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 2) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
[Tutorial] Function Pointers aaroncohn C++ Forum 4 17-Feb-2006 11:33
array function argument pixienick C++ Forum 2 02-Sep-2005 04:34
Trouble passing STL into a function Clive73 C++ Forum 5 10-May-2005 18:00
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 04:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:05.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.