locked
c++ class in class RRS feed

  • Question

  • class buttonClass {
        HWND hwnd;
        class TextClass {
            const int TextClassMaxLen = 100;
        public:
            operator string () { 
                string windowText(TextClassMaxLen);
                GetWindowTextA(hwnd, (char*)windowText, TextClassMaxLen);
                return windowText;
            }
        } ;
    public:
        TextClass Text;
        buttonClass(HWND parent) {
            HINSTANCE moduleHandle = GetModuleHandle(0);
            hwnd = CreateWindowA("BUTTON", "temp", WS_CHILD | WS_VISIBLE, 1, 1, 150, 30, parent, NULL, moduleHandle, NULL);
        }
    
    };

    1) It throws me error upon compilation. It says that hwnd in GetWindowTextA cannot be reached.

    How to get hwnd?

    2) Is there not any: friend string& operator=(string& paramString, TextClass& paramTextClass);

    I do

    buttonClass button(parent);

    string myString=button.Text;

    I assume there is not operator= so I do it by conversion to string.

    What is best way to do it?

    Friday, November 27, 2020 10:46 PM

All replies

  • Hello,

    Since this forum is for C# and not C++ use the following forum for C++.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    My GitHub code samples
    GitHub page

    Check out:  the new Microsoft Q&A forums

    Friday, November 27, 2020 11:40 PM
  • Class nesting in C++ is only for visibility.  The inner class does not know anything about the outer class.  If you want the TextClass objects to have access to a buttonClass object, then you must pass it in, probably in the constructor.

    Overall, in this case, I don't think a nested class is really the right solution.  Note that properties are not normally used in C++ this way, although Visual C++ does have an extension that provides them.  You probably want to add getText and setText methods, and then use the "property" extension to define a Text property.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Sunday, November 29, 2020 7:26 AM
  • Class nesting in C++ is only for visibility.  The inner class does not know anything about the outer class.  

    I'm not sure that's an entirely accurate statement.

    nested classes
    https://en.cppreference.com/w/cpp/language/nested_types

    Excerpt:

    "The name of the nested class exists in the scope of the enclosing class,
    and name lookup from a member function of a nested class visits the
    scope of the enclosing class after examining the scope of the nested
    class. Like any member of its enclosing class, the nested class has
    access to all names (private, protected, etc) to which the enclosing
    class has access, but it is otherwise independent and has no special
    access to the this pointer of the enclosing class.

    Declarations in a nested class can use only type names, static members,
    and enumerators from the enclosing class. (until C++11)

    Declarations in a nested class can use any members of the enclosing class, 
    following the usual usage rules for the non-static members. (since C++11)"

    - Wayne

    Sunday, November 29, 2020 7:53 AM