Re: New to class why am I having sooo much trouble accessing privates

From: Karl Heinz Buchegger (kbuchegg_at_gascad.at)
Date: 11/14/03


Date: Fri, 14 Nov 2003 10:28:07 +0100


sparks wrote:
>
> Ok so I am not totally blown away here.
>
> in my header file
> declare the function in public as
>
> void test::FormatARY()
> this will allow the function to access the private variables?
>

This is a *member* function
It is part of the class test and since it is a member of
that class it has access to all the internals of that class.

> in the cpp file
>
> void FormatARY() or void test::FormatARY() << which is correct?

the second one.

void FormatARY() freestanding function which doesn't have
                              any connection to class test. It is just
                              a function.

void test::FormatARAY() Member function of class test. It is part
                              of that class and thus can use everything
                              inside that class.

> and in main to initialize the array
> its just
> FormatARY() ?

No. You need an *object* of type class test!
Member functions work on objects, they are part of that
object.

int main()
{
  test MyObj; // <- this is the object

  MyObj.FormatARY(); // and now tell the MyObj object
                            // to invoke its member function FormatARY
}

A class is just a blueprint. It tells the compiler: whenever you have
an object of that type, this is what it looks like. A class declaration
by itself is a dead thing without much use, just like a blueprint of a
house is a dead thing. Only when you instantiate an object of such
a class type, that object comes to live and can do things. It is the
object that is the active part in your program, not the class. You
give request to that object ( MyObj.FormatARAY() <- Dear MyObj
object, I want you to execute your FormatARY() member function )

-- 
Karl Heinz Buchegger
kbuchegg@gascad.at


Relevant Pages

  • Re: creating thread in C++
    ... and you can't declare static member this way. ... OS/2 for instance uses a different calling convention for class members which cannot be changed. ... The work around is to use a friend instead of a static class member function. ... But I am unsure if a static function declaration within an extern "C" block is guaranteed. ...
    (comp.programming.threads)
  • Re: Object Initialization in C++
    ... > In this case I want to call the Description Member Function depending ... Declare Description to be a virtual function in Vehicle. ...
    (comp.lang.cpp)
  • Re: what static fuction mean in c#
    ... >> to acces but in c# i have to but it. ... in both languages you have to declare a member function as static if you want to call that ... > you need an instance to call a nonstatic member function. ... The difference is that in C++ you can declare functions outside of any class. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problem with __cdecl
    ... > If i declare the function as ... you can't pass a member function where a non-member ... these callback mechanisms often provide a means to pass "user ... but the first "void*" parameter may be ...
    (comp.lang.cpp)
  • VC8 template base class member function visibility bug
    ... A class Test with multiple base classes, ... to access a member function of BaseTemplatefrom Test by ... VC7.1 and Intel 9.1 compiler complain that the template ... land and modifies some of the other base classes' data. ...
    (microsoft.public.vc.language)