Re: C++ in ternms of C

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



A class and a structure are effectivly the same in terms of code once
compiled.

A class has its member variables,

Class A
{
public:
int b, c, d;
private:
double e, f;
};

struct tagA
{
int b, c, d;
double e, f;
};


Each of these is represented in memory as

Class [b][c][d][ e ][ f ]
Struct [b][c][d][ e ][ f ]

OOP is implimented by using the same kind of offsets as those used when
accessing the stack for a function.

You begin with the start point, in this case the first byte of 'b' in both
cases. We consider this the base.

If we want to access c, we access the memory location [base + 4] where 4 is
the sizeof(int), likewise to access d we do [base + 8].

In the case of a class, we have functions that are 'owned' by the class,
this is basically the same as saying: we are going to have a set of
functions, only accessable through a certain namespace, and for each of
these functions we are going to always pass it the base. As we saw above the
[base] can then be used to access the variables regarding a certain instance
of that class.

So if we have

class A
{
public:
int b, c, d;
int total( ) {
return b + c + d;
}
};

When we call total we actually call it along the lines of:
int total(&instance)

total then becomes:
return *[instance] + *[instance + 1] + *[instance + 2];

Using pointer arithmatic we know that this is actually instance, instance +
4 and instance + 8, due to the sizeof int being 4.

---------------------------

To consider the other aspects of OOP, we obviously have public, private and
protected; these are to cause code to be more secure, prevent the fiddling
by other classes and to impliment a much more powerful and descriptive
structure to a class. These are of course, completle nonsense to the linker,
the compiler performs these checks to tell us that our code conforms to the
design specification of the class (for example, not changing the member
variable 'int m_Count' directly when we should have to call the public 'int
IncrementCount( )'.

You can consider though, when exporting DLL's and functions, the linker will
utilise these to know which to add to the exports table and which not to.

There is also the consideration of virtual functions, for these the calling
function needs to know which of the class methods (which in assembly code
are all accessable anyway) to call, for this we need a special lookup
table - this is stored elsewhere in the programs data for each individual
class (not instance of the class).

----------------------------

In short, imagine the compiler ripping all of the functions out of a class,
and making them global prefixed by your class name, say:

class CApple
{
private:
int munchy;
int colour;

public:
int GetMunchy( ) { return munchy; }
};

The compiler would turn this into CApple_GetMunchy, or, more specifically
something along the lines of:

CApple::Get@mu@chy, a mangled name. This would be global, so if we went:
CApple apple;
/* something extra to set muchyness */
printf( "%d", apple.GetMunchy( ) );

We actually call the global function CApple::Get@mu@chy, passing it the
perameter &apple.

Of course, at linker stage, CApple::Get@mu@chy just becomes an address,
unless kept in the export table.

Thats my best effort anyway, I HOPE it is correct :)

--
- Mark Randall
http://zetech.swehli.com

"DeltaOne" <DeltaOne.1ntz5w@xxxxxxxxxxxxxxxxxxxxx> wrote in message
news:DeltaOne.1ntz5w@xxxxxxxxxxxxxxxxxxxxxxxx
>
> Hi, I am planning to study how we can implement things that we implement
> in c++ in c and using a bit asm also. First thing i want to know is
> about class and struct difference. How is the private and public part
> implemented in the c++ exactly or how is the internal representation
> done. I tried to analise the ASM file produ ced my the VC++ compil er.
> But there i wrote the following code class t{ private: __int8 i;
> public: void valF(); }; int main(){ t dat; t dat1; t p; dat.valF();
> p.valF(); dat1. valF(); return 1; } void t::v alF(){ i=3; return ; } in
> the above class "t" i changed the specif ier of "i" from private to
> public. but in both cases th e ASM code generated was same. Even the
> exe file was same.Then how in the ru n time it knows which data is
> private and which data is public. Is is implem ented by the runtime
> libraries? or how? is there any site or book from which i can ge t more
> info on it? Please let me know this first case.Hos can i im plement this
> private and public thing in C and if required some ASM inline a lso. I
> want to use a pure C compiler for this not a C++ compiler.How is it r
> epresented by the c++ compiler in translation to intermediate stage.
>
>
>
> --
> DeltaOne
> ------------------------------------------------------------------------
> Posted via http://www.codecomments.com
> ------------------------------------------------------------------------
>
>
>


.



Relevant Pages

  • random number code
    ... Will it really compile exactly the same as mingw compiler used ... // initialization of static private members ... MSBs of the seed affect only MSBs of the array ... // constructor with 32 bit int as seed ...
    (comp.lang.cpp)
  • Re: It Pays to Enrich Your C Skills
    ... Check if you can score a perfect 10 (without using a compiler). ... int main{ ... struct bitfield { ... out if it is a negative integer constant or a constant expression ...
    (comp.lang.c.moderated)
  • OT: Re: Perl Peeves
    ... I see the result of a test being used as an int. ... the compiler just assumed you knew what you were doing ... introduced to the language later, so void * was unheard of in most code. ... This didn't mean bool was special, declaring it just signaled to the ...
    (comp.lang.perl.misc)
  • Re: OT: Re: Perl Peeves
    ... when I see the result of a test being used as an int. ... compiler just assumed you knew what you were doing and would ... This didn't mean bool was special, declaring it just signaled to the ... What "normalization of bool results is built into the compiler"? ...
    (comp.lang.perl.misc)
  • Re: [CodeGallery] MFC MD5 Calculator
    ... Then when they added types, internally, the compiler still thought they were int values, ... ANSI standard began to emerge that the language design ...
    (microsoft.public.vc.mfc)