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.
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
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;
}
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.
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 :)
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.
Re: class created object name.
Are you using a Borland/Codegear Compiler?
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 :D
Re: class created object name.
Quote:
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!
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;
Re: class created object name.
To all who responded, big hugs :D
As I predicted it's not posible to code.
To JohnW@Wessex: Lettely I eat too much beens :D so fogot some pointers :blush:
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 :)
Cheers.
Re: class created object name.
Quote:
Lettely I eat too much beens :D so fogot some pointers
And now mixing beans with beens!
Quote:
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! :cool:
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! :rolleyes:
Re: class created object name.
Quote:
Originally Posted by
ulumulu
But hoped that someone knows some magic tricks :D
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?