CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Dec 2007
    Location
    Lithuania
    Posts
    98

    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.

  2. #2
    Join Date
    Mar 2009
    Location
    Riga, Latvia
    Posts
    128

    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

  3. #3
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    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;
    }
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  4. #4
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    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.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  5. #5
    Join Date
    Dec 2007
    Location
    Lithuania
    Posts
    98

    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.

  6. #6
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    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.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  7. #7
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: class created object name.

    Are you using a Borland/Codegear Compiler?
    - Guido

  8. #8
    Join Date
    Dec 2007
    Location
    Lithuania
    Posts
    98

    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.

  9. #9
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    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!
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  10. #10
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    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

  11. #11
    Join Date
    Dec 2007
    Location
    Lithuania
    Posts
    98

    Re: class created object name.

    To all who responded, big hugs

    As I predicted it's not posible to code.

    To JohnW@Wessex: Lettely I eat too much beens so fogot some pointers

    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.
    Share and always try to give back more.

  12. #12
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    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!
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  13. #13
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: class created object name.

    Quote Originally Posted by ulumulu View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured