Thread: How does C++ itself work?

  1. #16
    Registered User
    Join Date
    Dec 2010
    Location
    Shamokin PA (middle of nowhere)
    Posts
    138
    Here would be the link showing how C code such as with CB could be made into a COM object in a dll, which could be used in other languages...

    COM Tutorial #2: COM In Plain C And Plain PowerBASIC (pre-PB9)

  2. #17
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    51
    Freddie,

    Believe it or not, I actually read through all that and understood most of it. This is probably due to the fact that my idea of a relaxing night after work is trying to sift through complex and difficult-to-read C code and on top of that, I've dealt with COM before and have a book on it which I've perused called essential COM by Don Box.

    In any event, it makes total sense and I wanted to extend a huge thank-you for all of this. I can definitely see how obnoxious C syntax gets when trying to deal with "class object" stuff like that and why C++ would be a better choice in this case. I mean, that section in your main with that line which calls
    Code:
    pCB->lpIXVTbl->QueryInterface((IX*)&pCB->lpIXVTbl,&IID_IY,&pIY1);
    Really gets the point across that this stuff starts to run on, and I'm sure it could get a lot uglier than that. Anyway, I do have one question for you (excuse my ignorance). One stuff like this line:

    Code:
        for(i=0; i<2; i++)
        {
            VTbl=(size_t*)pVTbl[i];
    Why is a size_t* cast used? Everything else, believe-it-or-not, makes total sense and you did a phenomenal job explaining/documenting it all.
    Last edited by Asymptotic; 5 Days Ago at 02:20 AM.

  3. #18
    Registered User
    Join Date
    Dec 2010
    Location
    Shamokin PA (middle of nowhere)
    Posts
    138
    Just checked and both VTbl and pVTbl are typed as size_t*, so I expect that could be eliminated.

    On another related issue, when I first started trying to grasp 64 bit C/C++ builds, i.e., moving from 32 bit to 64 bit, I was naturally trying to grasp the variable types. I had a hard time coming up with what I wanted to use for 64 bit signed and unsigned integers. I don't even know if its correct or kosher but I finally settled on size_t and ssize_t. It just seemed to work best. And to some extent I'm still stuck between the 32 bit world and the 64 bit world. When I wrote up all this tutorial material in the early 2000s we were pretty solidly in the 32 bit world. Long about 2012 or so I started to learn 64 bit, and slowly started to try to get my various COM code ported to 64 bit. By using size_t I found I could get anything to build as x86 or x64 as the case may be.

    Yes, C++ removes that middle term in object references, so its clearly cleaner. But of course, its hiding the details of what's going on. That's positive in many aspects, but for deep thinkers such as yourself, you clearly recognize that there's some kind of 'magic' going on in the background, and I do believe that was the source of your original question that started this thread, i.e., "How does C++ actually Work?".

    I might point out that every object C++ creates doesn't follow the exact construction I outlined above. And in fact, none of the C++ standards (at least to my knowledge) require any particular binary layout. All that is proprietory to the compiler vendor. I do believe though that every C++ compiler targeted at Windows will produce that binary layout if a class inherits and implements pure abstract base classes, i.e., what are termed 'interfaces' in the COM\OLE world.

  4. #19
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    51
    Freddie,
    Yeah you figured me out... People often wonder why I like to get so "low-level" in the details but it's because my mind is obsessed with how things work so it's actually easier for me to understand concepts when I know all the implementation details. I suppose we could simplify the C syntax with some preprocessor magic but then isn't that how C++ got started in the first place??
    I must be a weird one because I had a harder time understanding Java data structures than C and the reason why is because to me, the concept of "points to" or "contains the address of" is easier to wrap my head around than "has property" or "has method" etc... Obviously, it's all understandable now, but at the beginning stages, the lower level stuff was actually easier.

    In any event, this has been one of the most interesting threads I've read in a while!
    Last edited by Asymptotic; 4 Days Ago at 02:59 AM.

  5. #20
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    51
    Came up with this after what I learned here:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    struct MyClass_; /* REQUIRED to use in func ptr def */
    typedef int math_operation (struct MyClass_ *this, int a, int b);
    typedef struct MyClass_{
    	int number;
    	char name[50];
    	math_operation *vTable[50];
    } MyClass;
    
    int add(MyClass *this, int a, int b)
    {
    	return this->number = a + b;
    }
    int subtract(MyClass *this, int a, int b)
    {
    	return a - b;
    }
    int divide(MyClass *this, int a, int b)
    {
    	return a / b;
    }
    int multi(MyClass *this, int a, int b)
    {
    	return a * b;
    }
    int main(void)
    {
    	MyClass *test = malloc(sizeof(test));
    	test->vTable[0] = add;
    	test->vTable[1] = subtract;
    	test->vTable[2] = divide;
    	test->vTable[3] = multi;
    	printf("5 + 5 = %d\n", test->vTable[0](test,5,5));
    	printf("35 - 3 = %d\n", test->vTable[1](test,35,3));
    	printf("25 / 5 = %d\n", test->vTable[2](test,25,5));
    	printf("5 * 5 = %d\n", test->vTable[3](test,5,5));
    	printf("And number is: %d", test->number+5);
    	return(EXIT_SUCCESS);
    }

  6. #21
    Registered User
    Join Date
    Dec 2010
    Location
    Shamokin PA (middle of nowhere)
    Posts
    138
    That's the only way to learn, i.e., playing with it and writing code (and doing a lot of nasty casting! ). I expect you see now why structs are usually used to define the VTable (all your member functions have the same signature).

    What I'd really highly recommend you do is try to read as many pages of this as you can get access to....

    Essential COM - Don Box - Google Books

    Its part of a book by Don Box entitled "Essential COM". I think it may be an eye opener for you.

    I think it will demonstrate to you why member functions are seperated into an interface class and not included in the implementation class, i.e., pure abstract base classes are used in C++ for the interface class, and the implementation class contains VTable pointers to the interface classes.
    Last edited by freddie; 3 Days Ago at 12:06 PM. Reason: Add A Thought

  7. #22
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    51
    Freddie, you're reminding me of when I programmed in C# :P

  8. #23
    Registered User
    Join Date
    Dec 2010
    Location
    Shamokin PA (middle of nowhere)
    Posts
    138
    Wish I had time to play with your 'Math' Class example, but unfortunately I don't. Too bad, as I have one almost exactly like yours but based on COM and buildable to a dll. I had worked on that years ago as part of one of my COM books I was studying.

    That Don Box link above - I never read the whole book unfortunately. The first chapter is especially good though, and I've read that many times. It concerns various underlying theories about various object models. Just thought you might be interested.

    Before .NET their was pure COM. That's the part I like, because its all low level binary stuff concerning how objects are actually put together. I became interested in it back in the 90s as Microsoft was wrestling with OOP component architectures. At the time I was a heavy user of pre .NET Visual Basic 4 through 6, which had its own runtime of 2 MB or so but unlike .NET had no massive class framework which needed to be loaded. That technology was COM/OLE based through and through. It became the world's most heavily used programming language because it was so easy to use everybody could become a programmer of sorts. Of course, it hid all the complex details of how it all worked at a low level. I wanted to understand it so I taught myself C and eventually C++, hoping knowledge of those languages would allow me to understand it. To a great extent they did - but not totally. What I found was that C programmers weren't much interested in the whole topic of COM/OLE/ActiveX, and C++ coders weren't much better. They 'cookbooked' the whole thing just like they 'cookbook' everything. That's the C++ mantra; learn the C++ syntax, then when you actually need to work in some specific subject area, find wrapper libraries that hide the underlying details. For COM it just happens to be ATL. There's a whole alphabet soup of three letter libraries for just about everything. They allow the C++ programmer to proceed about the same as the Visual Basic programmer. Its all 'magic' under the hood.

    When .NET hit (2002 or so) I needed a new programming language because I hated interpreters of any form. I considered C as I had learned Windows GUI coding using that with Charles Petzold's famous books. But with C I couldn't create the rich visual interfaces of Visual Basic. Of course, that was mostly a personal problem of mine. It was only I that couldn't do it - not some limitation of the C programming language. But at the time there was simply little or no documentation on how these complex COM/OLE objects (ActiveX Controls) were built using C. Of course, I taught myself C++, but C++ programmers didn't know how to do any of it low level either - they used ATL or MFC which 'cookbooked' it.

    About that time I discovered the PowerBASIC programming language, which is extremely interesting. The compiler is as rock solid and fast as can be imagined. Its actually written in assembly by one of the great assembly coders and compiler writers, who unfortunately is no longer with us. Most of the stuff in C++ class frameworks is part of the core programming language. The exe/dlls it produces are as fast as C. It became my first choice for a programming language after I discovered I had direct access to the full Windows API and could code Win 32 Api Petzold style with it. And there were no runtimes involved and the binaries produced were as small as ones produced by C.

    Unfortunately, there was still the problem of COM/OLE and the fancy visual interfaces one could produce with Visual Basic. I could no more produce these with PowerBASIC than I could with low level C. Then an individual named Jose Roca entered the PowerBASIC community. A lot of folks consider Jose to be a genius. He studied the Microsoft documentation and from that alone put together using PowerBASIC the memory objects necessary to instantiate these complex ActiveX Controls and OLE objects in PowerBASIC, basically using UDTs and memory allocations just as I've shown you here how to do. I learned a lot of that from books and Jose's source code. Its something I studied for years. That's when I became a dedicated PowerBASIC user. I had a programming language which created extremely small and fast executables, in which I could also utilize the high level fancy objects VB and .NET users also used. But nothing would be 'cookbooked'; no 'magic' code behind the scenes which I didn't understand.

    Just thought you might be interested in how I got into all of this

  9. #24
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    626
    Quote Originally Posted by freddie View Post
    Of course, I taught myself C++, but C++ programmers didn't know how to do any of it low level either - they used ATL or MFC which 'cookbooked' it.
    Then an individual named Jose Roca entered the PowerBASIC community. A lot of folks consider Jose to be a genius. He studied the Microsoft documentation and from that alone put together using PowerBASIC the memory objects necessary to instantiate these complex ActiveX Controls and OLE objects in PowerBASIC
    How are these statements any different?
    Some programmers used a bunch of code written by a different programmer but the C++ people are horrid incompetent boobs for not doing everything themselves and the powerbasic people are geniuses for taking advantage? CWindow is literally ATL/WTL for Powerbasic, a bunch of boring glue code that means you can get to actually doing the important stuff rather than crap you'd have to do a million times.

  10. #25
    Registered User
    Join Date
    Dec 2010
    Location
    Shamokin PA (middle of nowhere)
    Posts
    138
    I expressed that poorly. It was more a matter of my bouncing back and forth between studying C code and PowerBASIC code side by side, which expressed the actual in memory foot prints of the objects, that allowed me to achieve the understanding I wanted. To that end, C++ code wasn't much help to me, but Jeff Glatt's articles explaining everything in C did help a lot. But you're right, most PowerBASIC users weren't interested in how the underlying details worked once they had code someone else developed which worked - same as with C++ coders using code generation tools such as MFC or ATL. I personally didn't mind taking the time to understand how the so called 'boilerplate' code worked because it was a simple personal interest. When I could do it in two different languages I felt I finally understood it sufficiently.

    Maybe a lack of searching skills on my part, but I had a rough time figuring the stuff out. Could find very little info on how complex COM/OLE objects were put together without using automated code generation tools (MFC/ATL). At one point I put a lot of effort into studying ATL hoping to reverse engineer it so to speak, to see what it actually did. Unfortunately my knowledge of complex templates was not up to the task and I gave up that endeavor. Eventually I put all the piecies together, but it took a long time. Was fun though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does this work?
    By MacNilly in forum C++ Programming
    Replies: 2
    Last Post: 02-15-2006, 06:41 AM
  2. Why won't this work
    By gooey kablooey in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2004, 09:18 AM
  3. Why does this work?
    By Panopticon in forum C++ Programming
    Replies: 4
    Last Post: 01-07-2003, 08:59 PM
  4. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM

Tags for this Thread