Question

Following is an extract taken from chapter 7.6 of the <C++ primer (3rd)> by Stanley B. Lippman Josée Lajoie.

A function specified as inline is expanded "in line" at each point in the program in which it is invoked. For example,

int minVal2 = min( i, j );

is expanded during compilation into

int minVal2 = i < j ? i : j;

The run-time overhead of making min() a function is thus removed. min() is declared inline by specifying the inline keyword before the function's return type in the function declaration or definition:

inline int min( int v1, int v2 ) { /* ... */ }

Note, however, that the inline specification is only a recommendation to the compiler. The compiler may choose to ignore this recommendation, because the function declared inline is not a good candidate for expansion at the point of call. A recursive function, such as rgcd(), for example, cannot be completely expanded at the point of call (although its first invocation can). A 1,200-line function is also likely not to be expanded at the point of call. In general, the inline mechanism is meant to optimize small, straight- line, frequently called functions. It is of primary importance in the support of information hiding in the design of abstract data types, such as the IntArray class introduced in Section 2.3 with its size() inline member function.

Can someone please explain the sentence marked as bold ?

Was it helpful?

Solution

I don't have the full text of the 3rd edition here, but from the context you've posted, it's clear what he's saying.

Imagine a kind of 'vector' class.

class Vector {
private: 
  int * data; // pointer to data

There are several ways it could provide access to the vector size:

Choice 1: a public member variable:

public:
  int datasize; 

Choice 2: a private variable with a public accessor

private:
  int datasize; 

public:
  int getSize(); // body can be hidden in CPP, but would be { return datasize;};

Choice 3: as 2, but with an inline accessor

private:
  int datasize; 

public:
  inline int getSize() { return datasize; };

Now, #1 has poor data hiding, but great performance. #2 has great data hiding, but poor performance. #3 has good data hiding, and possibly great performance. (remember inline is just a hint)

In my view the data hiding of #3 is slightly inferior to #2 because the function code is revealed in the header - even if you can't access the member. So, I'd rewrite his statement along the lines of...

Inline Functions provide a good compromise between the requirements of Data Hiding versus Performance.

I wouldn't agree that it's of "primary importance", as it's just a hint to the compiler.

OTHER TIPS

  • "In the abstract data type design, it has played a major role in supporting of information hiding"

Data Type: You are probably familiar with this term. It can be an integer, a string, etc. or some more complex type like a B-Tree, a complex number, etc.

Abstract Data Type: These are data types that represent some real-life object.

e.g.

struct SCustomer
{
   string name;
   string address;
   int    id;
   enum CreditRanking { Good, Poor, Bankrupt };
   CreditRating rating;
};

CreditRating is an abstract data type, as is SCustomer.

Information Hiding: By encapsulating information inside the abstract data type, a designer can think about operations performed on/with a customer without the actual implementation of the SCustomer cluttering up their thought process.

The information is not really hidden, they can look inside the definition of the data type if they want to, but in many cases it is easier to just concentrate on the high (abstract) level and not think about the small details.

You can think of it like this:

  • Think about low-level details and build a set of objects.
  • Ignore the low-level details and combine the objects into bigger objects or into an application.

The above is over-simplified a bit, but I hope it gives you the flavour.

Does that help?

Regarding the late appearance of "inline functions".

Calling an inline function is as fast as putting the code "in line". Some programmers might hesitate to encapsulate their logic in functions due to the overhead in speed and size, but using an inline function minimised any overhead to (usually) a trivial amount.

Think of this code:

int customer_code = get_code(customer_name);
int product_code  = get_id(product_name);
bool credit_ok    = check_credit(customer_code);
int order_number  = create_order(customer_code, credit_ok, 
                                 product_code, quantity);
print_order(order_number, customer_code, customer_name, 
            product_code, product_name, quantity);

An information hiding version might look like this.

order ord = cust.make_order(product, quantity);
order.print();

Of this forms part of a much bigger program you can make the high-level code a great deal easier to undrstand. Somebody still needs to design the objects customer, order, product, etc. but they only need to concentrate on one thing at a time and then the bojects can be used throughout the program.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top