Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Extra libraries, Code snippets, applications etc. » C++ language problems and code snippets » Constructor with parameter
Constructor with parameter [message #9516] Mon, 14 May 2007 07:56 Go to next message
michael is currently offline  michael
Messages: 153
Registered: May 2007
Location: Germany
Experienced Member
I have a class that creates a Dialog:

class EditDialog : public TopWindow
{
	Button b;
	
	void DoClose()
	{
		Close();
    }
	
	public :
	
	typedef EditDialog CLASSNAME;
	
	EditDialog()
	{
    	SetRect(0, 0, 200, 50);
    	Title("Eintrag editieren");
    	Add(b.SetLabel("Close EditDialog").SizePos());
    	b <<= THISBACK(DoClose);
	}
};


And i have to constructors:

EditDialog createDlg;
EditDialog editDlg;


How can i use variables when creating a new EditDialog? For example:

I want to create a new EditDialog newDlg with a different Title? Can i deliver a String with the constructor like this?

EditDialog newDlg(String Title);


Michael
Re: Constructor with parameter [message #9517 is a reply to message #9516] Mon, 14 May 2007 10:07 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
class EditDialog : public TopWindow
{
	Button b;
	
	void DoClose()
	{
		Close();
    }
	
	public :
	
	typedef EditDialog CLASSNAME;
	
	EditDialog(const char *title)
	{
    	SetRect(0, 0, 200, 50);
    	Title(title);
    	Add(b.SetLabel("Close EditDialog").SizePos());
    	b <<= THISBACK(DoClose);
	}
};


The question is whether it is worth the trouble.

In most cases, calling the Title method is as complicated as this.

Mirek
Re: Constructor with parameter [message #9518 is a reply to message #9516] Mon, 14 May 2007 10:57 Go to previous messageGo to next message
michael is currently offline  michael
Messages: 153
Registered: May 2007
Location: Germany
Experienced Member
If if use your class example, how does my constructor has to look like?

What i want to do?

I want to use the class EditDialog for editing existings database entries and for creating new database entries.

Therefor i want to construct two objects of my class EditDialog, one for example with the title "Create" and one with the title "Edit".

What does my Class and my constructors has to look like do realize this?

Michael
Re: Constructor with parameter [message #9519 is a reply to message #9518] Mon, 14 May 2007 11:32 Go to previous messageGo to next message
Zardos is currently offline  Zardos
Messages: 62
Registered: April 2007
Member
I guess in the example from luzr you can not construct the two dialogs with different titles as members directly:

class MainDialog : public TopWindow
{
        ....

        // Does not work!
	EditDialog createDlg("Neuen Eintrag erstellen");
        EditDialog editDlg("Eintrag editieren");
        ....
}
};


Instead I would just call the Title function in the constructor in MainDialog:

class MainDialog : public TopWindow
{
        MainDialog()
        {
               createDlg.Title("Neuen Eintrag erstellen");
               editDlg.Ttile("Eintrag editieren");
        }
        ....

	EditDialog createDlg;
        EditDialog editDlg;
        ....
}
};

[Updated on: Mon, 14 May 2007 11:32]

Report message to a moderator

Re: Constructor with parameter [message #9520 is a reply to message #9516] Mon, 14 May 2007 11:50 Go to previous messageGo to next message
michael is currently offline  michael
Messages: 153
Registered: May 2007
Location: Germany
Experienced Member
Ok, that works.

Is there a way to get the Title of the Dialog after is was created?

I want to use this to do some if-selections with the title.

For example:

if(title == "create") do this
if(title == "edit") do that

Michael
Re: Constructor with parameter [message #9521 is a reply to message #9520] Mon, 14 May 2007 13:01 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
michael wrote on Mon, 14 May 2007 05:50

Ok, that works.

Is there a way to get the Title of the Dialog after is was created?

I want to use this to do some if-selections with the title.

For example:

if(title == "create") do this
if(title == "edit") do that

Michael


Well, this does not sound as a good idea to me at all, but if you insist, you can use GetTitle method (just beware, it returns WString - you can convert back to String by calling ToString - dlg.GetTitle().ToString()).

The correct solution would be either to use virtual methods, or some int member variable with enum (depends on situation).

Mirek
Re: Constructor with parameter [message #9524 is a reply to message #9516] Mon, 14 May 2007 14:28 Go to previous messageGo to next message
michael is currently offline  michael
Messages: 153
Registered: May 2007
Location: Germany
Experienced Member
Thanks Mirek,

you're right. Looks very ugly to me, too. I will think about a better solution for my problem. Maybe i should just create a separate class for my two dialogs...

Michael
Re: Constructor with parameter [message #9532 is a reply to message #9516] Mon, 14 May 2007 21:46 Go to previous message
mr_ped is currently offline  mr_ped
Messages: 825
Registered: November 2005
Location: Czech Republic - Praha
Experienced Contributor
I case you go for quick hack I would rather test the pointer of object with &createDlg memory address, than title strings.

But where do you need these tests? Maybe you can avoid this completely (even with same class for both dialogs, or with very simple two wrapper classes derived from original class with just few functions overloaded).

Usually every time you finish writing some code, you can immediately write "second" version which will be much better. Smile
Previous Topic: Unable to compile with Irrlicht
Next Topic: How to access other classes variables?
Goto Forum:
  


Current Time: Fri Mar 29 06:17:40 CET 2024

Total time taken to generate the page: 0.01062 seconds