CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    [RESOLVED] [C++11] - can avoid the class use the array operator?

    imagine these simple class:
    Code:
    class test
    {
       public:
              void write(string a)
              {
                    cout << a;
               }
    };
    (these class wasn't tested... it's for these question)
    can avoid the class use the array operator('[]')?

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: [C++11] - can avoid the class use the array operator?

    can avoid the class use the array operator('[]')?
    Can you expand upon this so that we might have some idea as to what you are referring
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [C++11] - can avoid the class use the array operator?

    Quote Originally Posted by 2kaud View Post
    Can you expand upon this so that we might have some idea as to what you are referring
    imagine that label class that i'm doing... it's a control.
    normaly you can do: label a[2];
    can i avoid that? maybe doing the overloading empty?

    Code:
    void operator[](label &labelinstance)
    {
        //do nothing
    }
    i don't want use arrays with my class label

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: [C++11] - can avoid the class use the array operator?

    can i avoid that?
    No.
    Quote Originally Posted by Cambalinho View Post
    i don't want use arrays with my class label
    But what if I want to use it in arrays? What if I have 25 labels, and I want to supply some sort of attribute to each one? Are you saying I must create 25 separate variables??

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [C++11] - can avoid the class use the array operator?

    Quote Originally Posted by Paul McKenzie View Post
    No.But what if I want to use it in arrays? What if I have 25 labels, and I want to supply some sort of attribute to each one? Are you saying I must create 25 separate variables??

    Regards,

    Paul McKenzie
    i get your point
    seems that i must see what i can do for arrays
    thanks for all

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: [RESOLVED] [C++11] - can avoid the class use the array operator?

    Sorry for the off-topic, dear Cambalinho, but in the most of the threads you started you look like (for me!) an inventor of perpetual motion machine.
    Victor Nijegorodov

  7. #7
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [RESOLVED] [C++11] - can avoid the class use the array operator?

    Quote Originally Posted by VictorN View Post
    Sorry for the off-topic, dear Cambalinho, but in the most of the threads you started you look like (for me!) an inventor of perpetual motion machine.
    lol maybe sometimes.... lol
    but i need some help, i don't know everything... the books never tell us everything.
    but honestly i must say thanks to all, inclued you, for try help me so much. and i have a big thanks to 2kaud, he help me more than my thanks
    i start C++ by me and not by school(only Turbo Pascal and then i found that i love programming). but i have found that C++ can be more easy than locks. maybe you ask: why so many libraries? then i answer: why boost? what i mean is that i can share my libraries without a problems and will be great even for win32(VictorN i'm doing again... sorry).
    it's a hobbie, but i love it and help me very in psicologic way(like music)
    thanks to all

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: [C++11] - can avoid the class use the array operator?

    Quote Originally Posted by Cambalinho View Post
    imagine that label class that i'm doing... it's a control.
    normaly you can do: label a[2];
    can i avoid that? maybe doing the overloading empty?
    if you make a class like
    Code:
    class label
    {
        // more stuff here
    };
    then you cannot prevent someone from doing
    label a,b; // create 2 label objects
    or
    label a[2]; // create 2 label objects (which happen to be in an array, but the label class doesn't know this)

    you can make the constructor private which would prevent any instantiation other than instantiation inside the class members itself. This is the technique behind a "singleton" (a design pattern than allows you to create only a single instance of a particular class). Is that what you're after ?

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