CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2002
    Location
    none
    Posts
    13

    Global Functions(Newbee Q)

    Hi,
    I'm getting an error that a global function cannot be called from within a class. How do I ensure that this class has acess to all gobal fuctions.
    The global function is in a .cpp

  2. #2

    Re: Global Functions(Newbee Q)

    Imposible, Global function are always avalible from Members, or from other global functions, may be you cpp did not know name of this function because you do not declare it purpoly:
    [ccode]
    // .h file
    void MyFn(int);

    // .cpp file
    void MyFn(int iID)
    {
    // ToDo somethink
    }
    [/ccode]

  3. #3
    Join Date
    Jun 2002
    Location
    none
    Posts
    13
    Well I dont declare the global fuction in the header, but does this make a difference?
    For example if I had a main.cpp with a bunch of globals and no corresponding main.h couldnt I still acess all the global functions.
    Anyway the global function are in a .cpp and are just defined as normal.

    returntype functionName(parameters)
    {
    //function body
    }

  4. #4
    Join Date
    Oct 2001
    Location
    Dublin, Eire
    Posts
    880
    As said, to access your global function, the class needs to know it. You do it by either defining the function in the same .cpp as the class and access it from the members of the class that are defined in that same .cpp, OR you define all your global functions in a .h file and implement them in a .cpp file and then include this .h file in your class header file.

    the example given by Plastelin seemed good.
    Elrond
    A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
    -- George Steiner

  5. #5
    Join Date
    Jun 2002
    Location
    San Rafael, Ca, USA
    Posts
    28
    Use a "this->" pointer for global functions within a class.

    It is not applicable in all cases though. Check the help files by doing an index search under "this" and select C++ library entry.

    "The this pointer is a pointer accessible only within the nonstatic member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer.

    this
    this->member-identifier
    An object's this pointer is not part of the object itself; it is not reflected in the result of a sizeof statement on the object. Instead, when a nonstatic member function is called for an object, the address of the object is passed by the compiler as a hidden argument to the function. For example, the following function call:

    myDate.setMonth( 3 );
    can be interpreted this way:

    setMonth( &myDate, 3 );
    The object's address is available from within the member function as the this pointer. Most uses of this are implicit. It is legal, though unnecessary, to explicitly use this when referring to members of the class. For example:

    void Date::setMonth( int mn ) {
    month = mn; // These three statements
    this->month = mn; // are equivalent
    (*this).month = mn;
    }
    The expression *this is commonly used to return the current object from a member function:

    return *this;
    The this pointer is also used to guard against self-reference:

    if (&Object != this) {
    // do not execute in cases of self-reference
    Note Because the this pointer is nonmodifiable, assignments to this are not allowed. Earlier implementations of C++ allowed assignments to this.
    Occasionally, the this pointer is used directly — for example, to manipulate self-referential data structures, where the address of the current object is required.

    Example
    // this_pointer.cpp
    // compile with: /EHsc
    #include <iostream>
    #include <string.h>

    using namespace std;

    class Buf {
    public:
    Buf( char* s );
    Buf& operator=( const Buf & );
    void Display() { cout << buffer << endl; }
    private:
    char* buffer;
    };

    Buf::Buf( char* s ) {
    buffer = new char[ strlen( s ) + 1 ];
    strcpy( buffer, s );
    }
    Buf& Buf:perator=( const Buf &otherbuf ) {
    if( &otherbuf != this ) {
    delete [] buffer;
    buffer = new char[ strlen( otherbuf.buffer ) + 1 ];
    strcpy( buffer, otherbuf.buffer );
    }
    return *this;
    }
    void main() {
    Buf myBuf( "my buffer" );
    Buf yourBuf( "your buffer" );
    myBuf.Display();
    myBuf = yourBuf;
    myBuf.Display();
    }
    From Microsoft SDK help file


    Hope that helps.

    - Sturmritter
    Last edited by Sturmritter; June 17th, 2002 at 12:49 PM.

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