a Engineer To Engineer Note EE-112
Technical Notes on using Analog Devices’ DSP components and development tools
Phone: (800) ANALOG-D, FAX: (781) 461-3010, EMAIL: dsp.support@analog.com, FTP: ftp.analog.com, WEB: www.analog.com/dsp
Class Implementation in Analog C++
Contributed by Graham Andrews,
C/C++ Compiler Developer,
DSP Development Tools Product Line
Overview
A "class" is a type in the C++ programming
language. The most simple form of this type is
likened directly to a struct type in C. In fact, the
keyword "class" can be replaced with the
class A{
public: // public access
int x;
double y;
};
All the examples described in this note show
equivalent layouts in C++ and C terms. The
examples demonstrate how an object
representation in C++ translates to C, so there is
a direct correspondence to the generated code.
keyword "struct" with no effect on the layout of
the internal representation of the object. The
difference is that C++ can place restrictions on
the accessibility of its class members. A struct
that is valid in C is valid in C++, and the same
rules of layout and parameter passing exist in
both C and C++. Member functions declared
within a class have no effect on the layout of a
given class object unless the member function is
declared virtual. When inheritance, virtual base
classes, and virtual functions are present, the
layout of C++ classes is more complicated.
A C struct is a valid C++ construct with the
same object layout:
struct A{
int x;
double y;
};
If the keyword "struct" is replaced with "class"
in C++, then the members x and y are implicitly
defined as private . Access specifiers define the
Inheritance
A class can be derived from one or more
classes. The classes from which it is derived are
called base classes. An object of a derived class
inherits all the members of the base classes. A
derived class can have base classes, which
themselves are derived, and each class may have
more than one base class. The layout of class
objects follows a simple pattern. This pattern is
illustrated by the following examples, which
show the underlying class layout in terms of C.
Example 1. Base & Derived Classes
class A{
int a1;
double a2;
};
class B:A{
int b1;
int b2;
};
C layout equivalents:
rules for using these members with the '->' and
'.' operators. The keyword "struct" makes these
members implicitly public in C++.
In C++, the previous example could be equally
written as:
Copyright 2000, Analog Devices, Inc. All rights reserved. Analog Devices assumes no responsibility for customer product design or the use or application of customers’
products or for any infringements of patents or rights of others which may result from Analog Devices assistance. All trademarks and logos are property of their respective
holders. Information furnished by Analog Devices Applications and Development Tools Engineers is believed to be accurate and reliable, however no responsibility is
assumed by Analog Devices regarding the technical accuracy of the content provided in all Analog Devices’ Engineer-to-Engineer Notes.
struct A{
int a1;
double a2;
};
struct B{
struct A base_A;
int b1;
int b2;
};
Example 2. Inheritance Using Classes A & B
from Example 1
class C{
char c1[10];
};
class D : B{
float d1;
short d2;
};
class E: C ,D {
unsigned int e1:2;
unsigned int e2:6;
unsigned e3:17;
};
C layout equivalents:
struct A {
int a1;
double a2;
};
struct B {
struct A __b_A;
int b1;
int b2;
};
struct C {
char c1[10];
};
class object layout to point to the instance of the
base.
In Example 2, class B has class A as a base class,
and class C has class A as a base class. If class D
has both B and C as base classes, then there
would be two class A parts of class D. However,
if class A is declared to be a virtual base of class
B and of class C, then only one A sub-object is
present in a D object.
Example 3. Virtual Base Classes
class Z{
int z1;
int z2;
};
class A: Z{
int a1;
};
class B : virtual A{
int b1;
};
class C : virtual A{
int c1;
};
class D: B, C{
int d1;
};
struct D {
struct B __b_B;
float d1;
short d2;
};
struct E {
struct C base_C;
struct D base_D;
unsigned int e1: 2;
unsigned int e2: 6;
unsigned int e3: 17;
};
Virtual Base Classes
Every class in a hierarchy that specifies a base
class to be virtual, shares a single object of that
base class within a derived object. In terms of
the layout, the compiler adds pointers to the
EN-112 Page 2
Technical Notes on using Analog Devices’ DSP components and development tools
Phone: (800) ANALOG-D, FAX: (781) 461-3010, EMAIL: dsp.support@analog.com, FTP: ftp.analog.com,
WEB: www.analog.com/dsp
C layout equivalents:
struct Z {
int z1;
int z2;
};
struct A {
struct Z __b_Z;
int a1;
};
struct B {
int b1;
struct A *__p_A;
/* pointer to A part of a B
object */
struct A __v_A;
/* instance of A part in B
object */
};