Thread: Questions

  1. #1
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034

    Question Questions

    Thanks in advance

    1) I've been away a while and this has become more widespread... ::std::cout. Why the extra scope operator ( :: ) in front? Is it faster or safer? Is it just a precautionary measure incase for some reason you don't know if the programmer will override "std" so tell the compiler to look up one scope?


    2) Does this exist anymore? It doesn't compile in my version of MinGW (GCC 3.4.5):

    Code:
    class A
    {
    public:
        template<typename T>
        void DoSomething()
        {
            // do something with datatype
        }
    };
    
    int main()
    {
        A a;
    
        a.template DoSomething<int>();
    }
    Should be equivalent to C#'s a.DoSomething<int>().

    Should I try "Testing: gcc-4.3.0-20080502-mingw32-alpha"?


    3) Commonly DLL's will have one and only one method: GetInterface() (or something) which returns a pointer to an object (use GetProcAddress and casting). Would you suggest storing the result as a pointer, pointer to a pointer, or a reference? I guess the advantage of a pointer to a pointer is if the pointer is changed by the DLL, it would carry through to the applications. Is there an advantage to a reference (other than matching syntax)?
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The last question is similar to how we do DLL's with objects in the embedded OS that I work with. It uses a single entry point to get a pointer to an object of a defined base-class.
    Since all user-callable functions are virtual, we don't really need to know the specific type of the class - it has the same interface as any other class derived from this base-class.

    If you want to extend the base-class and make use of such extended features, then obviously, you would need to cast the result of the "GetInterface" call.

    As far as I can see, there is no reason to store the pointer received from GetInterface in anything other than a pointer - pointer-to-pointer makes no sense - if you want the DLL to modify it later on (by calling some member function or some such) then you'd be passing the address of the pointer to get the pointer to pointer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I thought it was a.DoSomething<int>(). (So does gcc.)

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Does this exist anymore?
    Yes. It exists.

    I thought it was a.DoSomething<int>().
    It depends on the circumstances.

    Soma

    Code:
    class A
    {
    public:
        template<typename T>
        void DoSomething()
        {
            // do something with datatype
        }
    };
    
    template<typename T> class B
    {
    public:
        void DoSomething()
        {
            // do something with datatype
            T t;
            t.template DoSomething<int>();
        }
    };
    
    int main()
    {
        A a;
    
        a.DoSomething<int>();
        B<A> b;
        b.DoSomething();
    }

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Dae View Post
    Thanks in advance

    1) I've been away a while and this has become more widespread... ::std::cout. Why the extra scope operator ( :: ) in front? Is it faster or safer? Is it just a precautionary measure incase for some reason you don't know if the programmer will override "std" so tell the compiler to look up one scope?
    I guess they're just being extra paranoid. Starting it with :: means to start looking from the global namespace instead of the current one.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Thanks, thanks, and thanks.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Be careful with the DLLs about ownership of the returned object, and the dreaded cross-DLL allocation problem. If your DLL has a single object and always returns the same, make the return type a reference. Otherwise, make it a pointer and provide a second function, ReleaseObject or something like that, which removes it. Or, if you can accept C++ types in the interface, use a Boost or TR1 shared_ptr.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM