|
-
June 29th, 2009, 06:01 AM
#1
class created object name.
Good day.
I dont know if it is possible. But maybe there is a solution to such kind of situation.
Lets say. I have class which creates a button. and I create it.
Code:
TButton button1 = new TButton();
TButton button2 = new TButton();
and i have a method which sets name
Code:
button1->setName("button1");
button2->setName("button1");
Is it some kind of solution to make it like this
Code:
button1->setName();
And it should set button1 name as button1.
In other words. How to get the same name as instance ? OC setName() is a method, but what kind of code there should be I cant figure out 
Thankyou in advice.
Last edited by ulumulu; June 29th, 2009 at 06:10 AM.
Share and always try to give back more.
-
June 29th, 2009, 06:39 AM
#2
Re: class created object name.
Create static field in the class TButton, increase it in a constructor every time you create an object. In a more complex case (destroying buttons), use a kind of a set (also static) to store used numbers.
'static' means the field is not dependent of a particular instance of a class, say it's 'global', like/similar to a 'static' variable in a function
-
June 29th, 2009, 06:41 AM
#3
Re: class created object name.
Code:
#include <iostream>
#include <string>
#define STRINGIFY(x) (#x)
class Button
{
public:
Button() {}
void Set_Name(const std::string& name)
{
this->name = name;
std::cout << name << std::endl;
}
private:
std::string name;
};
int main()
{
Button a_button;
a_button.Set_Name(STRINGIFY(a_button));
return 0;
}
-
June 29th, 2009, 06:48 AM
#4
Re: class created object name.
It's more code than actual. Try:
Code:
#define SetNameMacro(x) x.SetName("#x")
Which will take only one parameter, will expand to variable name, as well would produce string.
-
June 29th, 2009, 06:56 AM
#5
Re: class created object name.
Hmm. Well I tell you maybe first whay I am doing so.
it's because I may have 100 objects. Buttons, Labels, Scrollboxes and so on. And to set everytime name is a bit overkill.
Like
:
Code:
button1->setName("button1");
................. 100 buttons ...............
Label4->setName("label4");
................. 10 labels ..................
You see button1 -> "button1"; and so on and so on.
So I just wanted to make it like:
button1->setName();
...............................
Label4->setName();
Well it's may look not a big deal, wny not to write. But I see allready that such stuff I will need for other things, so it would be good to figure it out now 
Thankyou for help
Share and always try to give back more.
-
June 29th, 2009, 07:19 AM
#6
Re: class created object name.
I don't think there is a way to do it without passing the name of the object. Also, if you put the objects into a container (to automate the process), you lose their names, so it's not really possible to do it that way, either.
-
June 29th, 2009, 07:21 AM
#7
Re: class created object name.
Are you using a Borland/Codegear Compiler?
- Guido
-
June 29th, 2009, 07:24 AM
#8
Re: class created object name.
To GNiewerth :
Well I am usein GCC 4.x compiler.
To Mybowlcut :
Somehow I knew it. But hoped that someone knows some magic tricks
Share and always try to give back more.
-
June 29th, 2009, 07:30 AM
#9
Re: class created object name.
Hmm. Well I tell you maybe first whay I am doing so.
it's because I may have 100 objects. Buttons, Labels, Scrollboxes and so on. And to set everytime name is a bit overkill.
Like
It would then say: It's bad bad programming! You should (must) not name objects and their captions like that. It's not practical for user to see "Button1" to do some operation. Your code becomes more of clutter than readable code, and is less maintainable!
-
June 29th, 2009, 07:55 AM
#10
Re: class created object name.
Code:
TButton button1 = new TButton();
TButton button2 = new TButton();
That looks a very 'Java like' way of declaring them.
Unless TButton is typedef'd as some sort of pointer, the above should look like this anyway...
Code:
TButton *button1 = new TButton();
TButton *button2 = new TButton();
On the other hand, the normal C++ declaration would be...
Code:
TButton button1;
TButton button2;
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
June 29th, 2009, 08:17 AM
#11
-
June 29th, 2009, 09:42 AM
#12
Re: class created object name.
Lettely I eat too much beens  so fogot some pointers
And now mixing beans with beens!
To Ajay Vijay: Maybe you are right, in final product button1 or label4 does not say much, right ?
But since it's in development, for testing it's more then OK
Not okay! This creates bad programming style in you, which would be hard to remove from your blood! 
Once I was working project in my institute, with my batch-mates. It was in Turbo C++, which did not support auto-indent feature. She was putting all of code in column 1. I asked to intend it properly, she said she'd do it later, as she was in "development" phase.
You be the judge!
-
June 29th, 2009, 10:42 AM
#13
Re: class created object name.
 Originally Posted by ulumulu
But hoped that someone knows some magic tricks 
The magic trick is already in post #4, even though there is a small errror in it. Working version:
Code:
#include <iostream>
#include <string>
#define SetSelfReflectiveName(x) (x).setName(#x)
class Button
{
public:
Button() {}
void setName(const std::string& n)
{
name = n;
std::cout << name << std::endl;
}
private:
std::string name;
};
int main()
{
Button a_button;
Button b_button;
SetSelfReflectiveName(a_button);
SetSelfReflectiveName(b_button);
}
BTW, why are you not using arrays if you are creating 100 buttons?
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|