C++ Structure Article Index for
C++
Website Links For
Structure
 

Information About

C++ Structure




A C++ structure is a datatype in the C++ programming language which, in addition to the properties of its C counterpart, can have its own member functions and operator overloads. It is identical to a C++ class except that its member accessibility defaults to public instead of private. As C++ structures are declared by the struct keyword, C++ structures are also known as '''structs''', and many programmers use "struct" as a short form for "structure".

The C++ struct declaration is incompatible by its C counterpart.


DECLARATION AND USAGE


C++ structures are declarated with the struct keyword. Members of a structure include variables (including other structs), functions (specific identifiers or overloaded operators), constructors and destructors.


Basic declaration and member variables

An example of a struct declaration follows:

struct people
{
string name;
int age;
};

Note that the Semicolon behind the closing parenthesis cannot be omitted.

name and age above are called member variables of the people datatype.

After this declaration, the main function we can directly utilize people as a Constructor to initialize some variables of people datatype:

#include
#include

  • struct declaration should be here ---/


int main ()
{
people a, b;
a.name = "Wales";
b.name = "Jones";
a.age = 30;
b.age = 20;
cout << a.name << ": " << a.age << endl;
cout << b.name << ": " << b.age << endl;
return 0;
}

The above code will print
Wales: 30
Jones: 20


Member functions

One of the most important features of the C++ structure is member functions. Each datatype can have its own built-in functions. Take the above people type as an example again.

struct people
{
string name;
int age;
//of course, member variables must be present

void print ()
{
cout << name << age;
  • we don't have to mention what "name" and "age" are,

  • because it automatically refers back to the

  • /

  • }

};

With the member function print(), the printing job can be greatly simplified into:

a.print();
b.print();

a and b above are called Sender s, and each of them will refer to their own member variables when the print() function is executed.


Overloaded operators


  • /, can be overloaded to suit the needs of programmers. These operators are called overloadable operators.


  • integer the sum, instead of the product, of the integers might be returned:


struct integer
{
int i;
integer (int j = 0) : i (j) {};
  • (const integer &k) const

  • {

return integer (i + k.i);
}
};

The code above made use of a constructor to "construct" the return value. For clearer presentation (although this will decrease efficiency of the program), the above code can be rewritten as:

  • (const integer &k) const

  • {

integer m;
m.i = i + k.i;
return m;
}

Programmers can also put a prototype of the operator in the struct declaration and define the function of the operator in the global scope:

struct integer
{
int i;

integer (int j = 0) : i (j) {};

  • (const integer &k) const;

  • };


  • (const integer &k) const

  • {

return integer (i + k.i);
}

i above represents the sender's own member variable, while k.i represents the member variable from the argument variable k.

The const keyword appears twice in the above code. The first occurrence, the argument const integer &k, indicated that the argument variable will not be changed by the function. The second incidence at the end of the declaration promises the Compiler that the sender would not be changed by the function run.

In const integer &k, the Ampersand (&) means "pass by reference". When the function is called, pointer to the variable, instead of the variable itself, will be passed into the function.

Note that Arity , Associativity and Precedence of operators cannot be changed.


Binary overloadable operators

Binary (lit. ''have two'') operators are overloaded by declaring a function with an "identifier" ''operator (something)'' which calls one single argument. The variable on the left of the operator is the sender while that on the right is the argument.

integer i = 1;
  • we can initialize a structure variable this way as

  • if calling a constructor with only the first

  • /

  • integer j = 3;

  • variable names may conflict with the names of the

  • /

  • j;

  • cout << k.i << endl;


'4' would be printed.

The following is a list of binary overloadable operators.



  • / %

OperatorGeneral usage
Arithmetic calculation
  <tr><td>+ -= = /= %= ^= &= = <<= >>=</td><td>Compound assignation</td></tr>



The '=' (assignation) operator between two variables of the same structure type is overloaded by default to copy the entire content of the variables from one to another. It can be overwritten with something else, if necessary.

Operators must be overloaded one by one, in other words, no overloading is associated with one another. For example, < is not necessary the opposite of >.


Unary overloadable operators

While some operators, as specified above, takes two terms, sender on the left and the argument on the right, some operators have only one argument - the sender, and they are said to be "unary". Examples are the negative sign (when nothing is put on the left of it) and the "logical NOT " ( Exclamation Mark , !).

Sender of unary operators may be on the left or on the right of the operator. The following is a list of unary overloadable operators.








OperatorGeneral usagePosition of sender
+ -Positive / negative signright
Dereferenceright
! ~Logical / bitwise NOTright
++ --Pre-increment / decrementright
++ --Post-increment / decrementleft


The syntax of an overloading of a unary operator, where the sender is on the right, is as follows:
:return_type operator@ ()

When the sender is on the left, the declaration is:
:return_type operator@ (int)

@ above stands for the operator to be overloaded. Replace return_type with the datatype of the return value (int, bool, structures etc.)

The int parameter essentially means nothing but a convention to show that the sender is on the left of the operator.

const arguments can be added to the end of the declaration if applicable.


Overloading brackets

The square bracket {Link without Title} and the round bracket () can be overloaded in C++ structures. The square bracket must contain exactly one argument, while the round bracket can contain any specific number of arguments, or no arguments.

The following declaration overloads the square bracket.
:return_type operator {Link without Title} (argument)

The content inside the bracket is specified in the argument part.

Round bracket is overloaded a similar way.
:return_type operator() (arg1, arg2, ...)

Contents of the bracket in the operator call are specified in the second bracket.



Constructors

Sometimes engineers may want their variables to take a default or specific value upon declaration. This can be done by declaring Constructor s.

people (string N, int A)
{
name = N;
age = A;
}

For convenience, the above code can be rewritten into a shrunken form, with utilization of a colon, to facilitate reduction in typing work, more efficient coding and time reduction in debugging:

people (string N, int A) : name (N), age (A) {};

Note that the parantheses cannot be omitted, although it consists of nothing.

A default value can be given to the arguments to help initializing default values. A constructor with no arguments will also do the same.

people (string N = "", int A = 0) : name (N), age (A) {};

people () : name (""), age (0) {};

The declaration of a constructor looks like a function with the name as the same as the datatype. In fact, we can really call the constructor in form of a function call. In that case a people type variable would be the return value:

int main ()
{
people r = people ("Wales", 40);
r.print ();
}

The above code can also be rewritten as:

int main ()
{
people r ("Wales", 40);
r.print ();
}

Specific program actions, which may or may not relate to the variable, can be added as part of the constructor.

people ()
{
cout << "Hello!" << endl;
}

With the above constructor, a "Hello!" will be printed in case a people variable with no specific value is initialized.


Destructors

A destructor is the reverse of a constructor. It will be called upon emptying of the memory location storing the variable. It must be used in compound with pointers.

Most structures do not have destructors as it has relatively little utility in comparison with the other features of a structure.

Initialization of a destructor is similar as that of a constructor, except a tilde (~) is put before the variable name.

~people ()
{
cout << "I'm deleting " << name << "with age" << age << endl;
}


TEMPLATE STRUCTURES


C++ allow structures to be declared as templates. In other words, constituent members of a structure do not necessary come from a specific type. For example, the following structure stores two variables of a user-specific type and has an overloaded plus operator.

template struct twothings
{
A one;
B two;
twothings operator+ (const twothings &arg) const
{
twothings new;
new.one = one + arg.one;
new.two = two + arg.two;
return new;
}
};

After that, in the main function, we can use the twothings structure to store variables of any two datatypes as we want to.

twothings twonumber;
twothings bigmap;
twonumber.one = 16;
twonumber.two = 1.667;
bigmap.one = "Hallo";
bigmap.two = 19;

All of the above statements are valid because the datatypes match correctly. In addition, as in above, a structure or class datatype string (a library class) is used as the type of the constituent variable. This shows structures themselves can be substituted as templates.

The implementation of templates in the early versions of C++ has contributed to the birth of the Standard Template Library .


PROPERTIES

The Optimization of C++ tries to let every aspect of a structure look like that of the basic datatypes. Therefore, overloaded operators let structures to be operated just like integers and floating-points do; structures can be Array ed by the square-bracket declaration (some_structure variable_name {Link without Title} ); and dereferencing of structures are declared the same way as in built-in datatypes.


Memory consumption

The memory consumption of a structure is equal to the sum of the memory sizes of its constituent variables. Take the twonums structure below as an example.

struct twonums
{
int a;
int b;
};

The structure consists of two integers. In C++ integers are set to be 32-bit Integers by Default , so each of the member variables consume four bytes of memory. The entire variable, therefore, consume eight bytes of memory.

As structures may make use of pointers and arrays to Declare and initialize its member variables, memory consumption of structures is not necessary Constant . Another example of non-constant memory size is template structures.


Pass by reference


Many programmers prefer to use the ampersand (&) to declare the arguments of a Function involving structures. This is because by using the dereferencing ampersand only four bytes of data are needed to be passed into the function, namely the memory location to the variable.

By this memory location, the machine can retrieve data more conveniently without retrieving the entire content of the variable every time the function is called and run time can be fantastically reduced.

The pass by reference is not confined to the case in structures. Normal datatypes may also use the pass by reference technique, although because of a different reason: When the function calls a variable by reference, the argument variable can be changed by the function call if the const keyword is not present.


The ''this'' keyword

See Also: This (computer science)



To facilitate efficient use of structures, C++ implements the this keyword for function calls, constructors and destructors of a structure to refer to its own location.

Datatype of this is a pointer to structure.

The this keyword is especially important for member functions with the structure itself as the return value:

fraction operator+= (const fraction f)
{
  • this = ---this + f;

  • this;

  • }


  • ) dereferencing Sign to retrieve the value of the structure for Calculation .


Note that the const keyword appears only once in the first line. This is because the sender is changed by the function, while the parameter argument is not.

When, in case, the sender variable is changed by the function call and an initialized variable is returned, the ampersand may also be used in the return type declaration:

fraction& operator += (const fraction f)


CONTROVERSY

Although the implementation of structure in C++ created great convenience for programmers and users of C++ program, it has its own controversy, particularly on the overloaded operators.


The "insertion" and "extraction" double-arrows (<< >>)

The standard input and output of C++, cin and cout classes, make use of the overloaded bitwise shift-left and shift-right operators to extract and output data into and out of the console, and calls the operators "insertion operator" and "extraction operator" respectively.

As such act twisted the original meaning of the shift-left and shift-right operators, many Programmers blame the ANSI and GNU for letting such misuse of overloaded operators exist in standard C++, and demanded them to be removed and replaced with some other operators.

However, pro-arrow programmers argued that the double-arrows are the best operators to describe the action of input and output, and no other operators can have similar resemblance. The debate basically ended with no consensus, with the overloaded operators kept.


Extra keywords

To help implement the structures and classes, C++ introduced many new keywords in addition to the Keyword s inherited from C . This made many identifiers which are available in C, such as new and this, unavailable in C++, and partially violated the promise that C source code would be compatible with C++.


String operators

Overloaded operators enable strings to be processed in C++ with overloaded operators, such as =, + and +=. This gives beginning programmers a false feeling that one can "add" a string to another using the arithmetic "plus", instead of thinking the operation as an act of Concatenation .


SEE ALSO



REFERENCES

# Cplusplus.com tutorial lesson 5.2 , accessed in January 2006
#Chan, S. "Tutorial on C++ and STL", Hong Kong, 2006
# Cplusplus.com tutorial lesson 2.5 , accessed in February 2006